Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Implementing spell system  (Read 3497 times)

0 Members and 2 Guests are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Implementing spell system
« 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?
« Last Edit: September 10, 2016, 03:49:11 pm by MrOnlineCoder »

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Implementing spell system
« Reply #1 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/++.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Implementing spell system
« Reply #2 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?
 

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Implementing spell system
« Reply #3 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.
Back to C++ gamedev with SFML in May 2023

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Implementing spell system
« Reply #4 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.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Implementing spell system
« Reply #5 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.
I would like a spanish/latin community...
Problems building for Android? Look here