Jump to content
  • Advertisement
Sign in to follow this  
NSDuo

Separate lua scripts for each scene

This topic is 2565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a series of nodes used to organize various events into scenes within my engine.
However, it has recently occurred to me that I have no idea how to properly program logic for these scenes individually.

The only ways I know I could do this is:
Many "if" or "case" statements

-- this would be in lua
if level1 then
level1logic()
elseif level2 then
level2logic()
elseif level3 then
level3logic()
end
-- and so on and so on


I have a strange gut feeling that this method might be too slow and confusing.

Scenes calling lua functions

// this would be in C++

scene::callLogic()
{
luaCall("functionName");
}

// and then

currentScene->callLogic();

This is the method I like the most, but I have a feeling it won't work

These are the two methods for scripting logic that I was able to come up with, but I'm not sure which one is the best, or even if they're the correct approach at all.
I would really appreciate the help.

Share this post


Link to post
Share on other sites
Advertisement
It isn't uncommon in game frameworks to have a scripting system/manager class that on a pre-defined interval (every game loop or every X frames or every X milliseconds) the update() method on the script manager class is executed which iterates each active script and runs the script's update method (or some variant thereof). This is a great way to write game logic that you want to test out and prototype too and if you find the performance isn't ideal, you can then transpose that logic to C++ later if you find it needs to be faster. But the concept allows you rapidly prototype features which is a huge advantage.

Share this post


Link to post
Share on other sites

It isn't uncommon in game frameworks to have a scripting system/manager class that on a pre-defined interval (every game loop or every X frames or every X milliseconds) the update() method on the script manager class is executed which iterates each active script and runs the script's update method (or some variant thereof). This is a great way to write game logic that you want to test out and prototype too and if you find the performance isn't ideal, you can then transpose that logic to C++ later if you find it needs to be faster. But the concept allows you rapidly prototype features which is a huge advantage.


That sounds somewhat like the second method.
When you say "each active script", what exactly does that mean? Do you mean a ".lua" file for every scene?

Share this post


Link to post
Share on other sites

When you say "each active script", what exactly does that mean? Do you mean a ".lua" file for every scene?

Usually it is more like a '.lua' script for every object, plus a '.lua' script for every scene. The idea is to move as much of the behaviour of each object as possible into the object script, so that your scene script has less to do - it really just needs to setup the objects, and check for the ending condition.

Consider a game of PacMan: each level might have a script to spawn the player, enemies and dots, as well as to check for the victory/loss condition (eat all dots/be eaten). Each type of enemy would also have its own script to provide it's behaviour - movement, strategy, etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!