While Datel's style of writing function bodies is no different (lua silently rewrites gunty's code to look identical to Datel's during compilation), there's a two problems to be addressed as well. The first is the change to the actual memory.register line. You want to give the function by name, not by actually calling it, which is what you do. The second regards the parameters the function itself gets.
No parameter is given to the called function. Lua fills in the name you given with nil to compensate.
It may sound stupid, but it's sorta built on the assumption that you'd use a different function for each memory address.
Alternatively, you can use upvalues...
-- a "do/end" is a block of scope, just like within a function, while loop, or other indented code block.
do
local addr = 0x7e0101
local function registered()
snes9x.message("Written: "..addr)
end
memory.register(addr, registered)
end
Even though "addr" falls out of scope, the function "registered" can still access it, and get its value from there.
Every time the flow of code would execute a function() block, a function is actually created out of the local variables in place at the time.
-- We construct 3 "functions" (technically call closures) with identical code,
-- but different upvalues for "addr" in each one. So while it's technically
-- one function, we can give the illusion that it knows some magic value
-- for its specific invocation
addresses = { 0x7e0001, 0x7e0002, 0x7e0003} -- Just go with it
for key,value in pairs(addresses) do
-- We need a distinct local for each function
local addr = value
local function registered()
... -- Use "addr" in here somewhere
end
memory.register(addr, registered)
end
... Or should I just add it anyways?