Hello. I am adding "spell system" in my game. Spells are unique: one just do damage, another moves player in other position, another one adds buff, etc. Can it be implemented in Lua scripting? I mean something like
zeus_arrow.lua in spells folder:
function handler(target)
--Implemented in C++
DamageCharacter(target, 20);
end
--Implemented in C++
RegisterSpell("Zeus Arrow", handler);
If yes, then how can I store handler function in C++, and call it everytime when spell is used?
Adding functions to Lua from C++ is not a problem, my question is: how can I call Lua function from c++ mulitple times?
For example:
In C++ I have Spell class:
class Spell {
int manacost;
string name;
int type;
//Handler function
};
And how can I store spell handler function in this class?
spell.act(); // How to do this?
Hello. I am adding "spell system" in my game. Spells are unique: one just do damage, another moves player in other position, another one adds buff, etc. Can it be implemented in Lua scripting? I mean something like
zeus_arrow.lua in spells folder:
function handler(target)
--Implemented in C++
DamageCharacter(target, 20);
end
--Implemented in C++
RegisterSpell("Zeus Arrow", handler);
If yes, then how can I store handler function in C++, and call it everytime when spell is used?
Since Lua functions cannot be converted direclty into C functions, you can (like FRex said) store a reference with luaL_ref.
You can make it global too, but declaring many globals are not good in Lua, so you can store of yours functions on a table, preserve a reference for that table or keep the table scoped globally and then search the required function on that table and then call it.