Adding callback opportunities to LUA?

SamBC

Emperor
Joined
Feb 2, 2010
Messages
1,052
Location
Lancaster
Is it possible, in a LUA file (a replacement for one of the core ones) to create a new callback opportunity, and then allow mods added afterwards to register callbacks so, for each "DoFoo" registered, the core file calls that function and gets it to add its own details to the structures passed to it? You'd have to trust callbacks to behave of course...

I ask this because I know not-too-much about LUA, but it seems a potential way to compromise between what the game designers did and what I think they should've done, allowing them to keep their hardcoded weirdness but for other people to add their own weirdness in a modular fashion.
 
I know what you're trying to accomplish and hopefully can help you in the future with it ;)

I dont know much about lua yet but i hope we can register custom events and/or callbacks like you suggest
 
So, looking at the example on pages 60-61 of Kael's guide, I'm thinking that I could add a bit of code somewhere in AssignStartingPlots.lua to create an Event:
Code:
local callbacks = {};
local cbhead = 1;

LuaEvents.RegisterCallback.Add(
 function(cb)
  callbacks[cbhead] = cb;
  cbhead=cbhead+1;
 end
);
And when I want to call them, do
Code:
for cb in pairs(callbacks) do cb() end;
The Lua may not be quite right (I'm a bit new to Lua), but from my reading of a Lua guide, this makes sense. Does it make sense to anyone else?
 
This:
Code:
local callbacks = {} -- declaring a table (don't put ; at the end of a statement, that's not needed)
local cbhead = 1 -- declaring a local varibale

LuaEvents.RegisterCallback.Add(
 function(cb)
  callbacks[cbhead] = cb --setting the table value to cb
  cb=cb+1 -- increasing cb. You probably want to increase cbhead here
 end
)
is almost right except you probably meant cbhead = cbhead + 1.

That:
Code:
for cb in pairs(callbacks) do cb() end;
won't work.
You want
Code:
for _, cb in pairs(callbacks) do cb() end
Because pairs returns a pair key/value, in your code cb is the key, so it's actually cbhead. The value is your callback function.

This is pure lua, I don't know what registercallback does.
 
This:
Code:
local callbacks = {} -- declaring a table (don't put ; at the end of a statement, that's not needed)
local cbhead = 1 -- declaring a local varibale

LuaEvents.RegisterCallback.Add(
 function(cb)
  callbacks[cbhead] = cb --setting the table value to cb
  cb=cb+1 -- increasing cb. You probably want to increase cbhead here
 end
)
is almost right except you probably meant cbhead = cbhead + 1.
Oops, yeah, fixed in original...
That:
Code:
for cb in pairs(callbacks) do cb() end;
won't work.
You want
Code:
for _, cb in pairs(callbacks) do cb() end
Because pairs returns a pair key/value, in your code cb is the key, so it's actually cbhead. The value is your callback function.
Okay, was taking example from http://www.lua.org/pil/4.3.5.html, to whit:
Code:
for k in pairs(t) do print(k) end
In that guide, ipairs(t) returns an actual pair... seemed odd to me, but there you go...
This is pure lua, I don't know what registercallback does.
That's from Kael's examples. Apparently, you can do "LuaEvents.WhatEver.Add(closure)", and later call "LuaEvents.WhatEver()" to call the function defined by the closure. It's how to call a function in one script file from another, even if that later one doesn't exist yet.
 
Top Bottom