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

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

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

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×