SFML community forums

Help => General => Topic started by: MrOnlineCoder on September 10, 2016, 03:45:47 pm

Title: Implementing spell system
Post by: MrOnlineCoder on September 10, 2016, 03:45:47 pm
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?
Title: Re: Implementing spell system
Post by: Gambit on September 10, 2016, 04:45:57 pm
To answer your question, yes. However it's complicated to explain simply. I would recommend reading the api documentation for Lua and finding examples of adding functions to Lua from c/++.
Title: Re: Implementing spell system
Post by: MrOnlineCoder on September 10, 2016, 05:14:14 pm
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?
 
Title: Re: Implementing spell system
Post by: FRex on September 10, 2016, 09:09:36 pm
You could look it up in appropriate file by name each time or you could use luaL_ref to store reference to it.
Title: Re: Implementing spell system
Post by: Gambit on September 12, 2016, 02:24:04 am
That is not correct. Instead you should look up the function which changes depending on where the function is stored. If you are handling user data then you cast it straight back it's original type but if you aren't then you need to push the global table the push the appropriate fields and values then do lua_call which will pop all of the arguments and the function name/field and push the result.
Title: Re: Implementing spell system
Post by: DarkRoku12 on September 12, 2016, 03:56:35 am
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.