Advertisement

Separate lua scripts for each scene

Started by December 05, 2011 08:32 PM
2 comments, last by swiftcoder 12 years, 11 months ago
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.

Macbook Pro 2.66Ghz dual core, 4GB ram, 512MB vram, MacOSX 9.1, Windows 8.1
Xcode 5.0.2, C++, lua

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.
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.


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?

Macbook Pro 2.66Ghz dual core, 4GB ram, 512MB vram, MacOSX 9.1, Windows 8.1
Xcode 5.0.2, C++, lua


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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement