TIC-80
TIC-80 copied to clipboard
remap callback argument error on Ruby
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
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.
This is what I get with latest mruby in tic80: wrong number of arguments (given 0, expected 3) (ArgumentError)
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:
- Passing the name of the method as a string or symbol. Example:
before_create :your_method_name_here - Passing a lambda or proc. Example:
before_create -> { do_thing; do_another_thing } - 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.
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
I'm concerned that I can't receive tile variables as arguments.
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
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)
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