TIC-80 icon indicating copy to clipboard operation
TIC-80 copied to clipboard

remap callback argument error on Ruby

Open hadamak opened this issue 1 year ago • 2 comments

How to use the remap callback on Ruby?

# script:  ruby

def remap(t,x,y)
	t
end

def TIC
	cls 13
	map 0,0,30,17,0,0,-1,1,remap
end

'remap': wrong number of arguments (0 or 3) (ArgumentError) user code:3:in remap user code:9:in TIC user code:7

hadamak avatar Sep 25 '24 08:09 hadamak

Some detective work: the message "wrong number of arguments (X for Y)" was changed in mruby 3 years ago: https://github.com/mruby/mruby/commit/c4bca7cbb3eda883c7b09b6c0568a90fb8a85a5d

Another commit from around the same time which could be relevant: https://github.com/mruby/mruby/commit/16e388863afef7616c7272d779f8cb7abc478992

Maybe mruby just needs an update, here.

anescient avatar Sep 28 '24 15:09 anescient

This is what I get with latest mruby in tic80: wrong number of arguments (given 0, expected 3) (ArgumentError)

Miguel-hrvs avatar Oct 07 '24 08:10 Miguel-hrvs

This is because Ruby is executing remap when it appears in the arguments of map. Surprise! Can't directly reference a method by its handle. In Ruby on Rails, callbacks are usually passed in one of two ways:

  1. Passing the name of the method as a string or symbol. Example: before_create :your_method_name_here
  2. Passing a lambda or proc. Example: before_create -> { do_thing; do_another_thing }
  3. Haven't tested that this one works in mRuby, but you can do method(:your_method_name_here) to get a reference to the method that can be executed like a proc.

I'm not sure how TIC-80 handles this.

jamesgecko avatar Oct 20 '24 22:10 jamesgecko

Now it can be used in this example

# script:  ruby

def remap(x,y)
  # tile=mget(x,y)
  tile=1
  flip=0
  rotate=1
  [tile,flip,rotate]
end

def TIC
  cls 13
  map 0,0,3,7,0,0,-1,1,&:remap
end

video1

I'm concerned that I can't receive tile variables as arguments.

hadamak avatar Oct 21 '24 06:10 hadamak

The first and second arguments received by the remap callback method contain the x and y coordinates, respectively. However, I am unable to receive the value of the tile.

# script:  ruby

def remap(x,y)
  print "#{x},#{y}",0,20
end

def TIC
  cls 13
  map 4,5,1,1,0,0,-1,1,&:remap
end

video2 When defining the method as remap(tile, x, y) with three arguments, I encounter the following error:

'remap': wrong number of arguments (2 or 3) (ArgumentError)

hadamak avatar Oct 21 '24 06:10 hadamak

I found that when using Proc.new or lambda instead of a method definition, I am able to receive three variables. Now it can be used in this example

# script:  ruby

# lambda or Proc.new
$remap = lambda do |tile,x,y|
  print "#{tile},#{x},#{y}",0,10
  tile
end

def TIC
  cls 13
  map 3,4,1,1,0,0,-1,1,&$remap
end

video3 video4

hadamak avatar Oct 21 '24 06:10 hadamak