Hey @dustin! Hope all your stuff is going well! I just tried the Signal and State libraries for the first time, and while they appear to mostly-work out of the box, I had some trouble with the State subscription functions. I worked out that they're receiving more arguments than the example suggested; where you have function(old_value, new_value)
, the function is actually receiving nil(not sure where this is coming from?), key, old_value, new_value
. Could you tell me if that's expected, or if I've hooked up something wrong? It's usable as-is, but I'll just need to call the function with (_, _, old_value, new_value)
.
Anyway, here's a little project that illustrates the issue: press left or right to increment or decrement the number GameState.wall (because I'm tracking which wall I have selected in our game), with console output printing the (...)
args passed into the subscribed function.
splendorr-state-test.zip (15.3 KB)
And the main.lua for reference, which just imports the Signal and State libraries as written in your post above:
import 'CoreLibs/graphics'
import 'Signal'
import 'State'
GameState = State()
GameState.wall = 1
print('GameState.wall starts at '.. tostring(GameState.wall))
local gfx = playdate.graphics
function playdate.update()
gfx.clear()
if playdate.buttonJustPressed('left') then
print('Pressed Left! Subtracting 1')
GameState.wall = GameState.wall - 1
end
if playdate.buttonJustPressed('right') then
print('Pressed Right! Adding 1')
GameState.wall = GameState.wall + 1
end
gfx.drawTextAligned(GameState.wall, 200, 120, kTextAlignment.center)
end
--[[ The provided example has 2 args:
GameState:subscribe("score", self, function(old_value, new_value) end
but the usage below receives 4, which are: unknown, key, old_value, new_value
My question is, what's the first nil (implicit self?) and is the key expected, even though the example doesn't have it?
]]--
GameState:subscribe('wall', self, function(...)
print('Subscription args:')
print(...)
print('GameState.wall is '.. tostring(GameState.wall))
end)
And here's the console output, running the game and then pressing right twice:
GameState.wall starts at 1
Pressed Right! Adding 1
Subscription args:
nil wall 1 2
GameState.wall is 2
Pressed Right! Adding 1
Subscription args:
nil wall 2 3
GameState.wall is 3
Thanks!