Jump to content
  • Advertisement
Sign in to follow this  

Correct way to implement a scripting language for game logic.

This topic is 2157 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'm about to start scripting Lua for my current project. I'll be using Luabind to bind it to c++. But before I started scripting I wanted to make sure I understood how this is typically done, and what things it's typically done for. 


My understanding of it as it stands is that I will create a scripts folder in my project folder and put a few more folders in that specifying the type of scripts inside for organization. Inside those I'll make short .lua scripts for each type of enemy/item/etc. These scripts will contain a few methods like OnCreate() to set up the values of the c++ instance of "Enemy"


When I start the game it will load the map (I'm using .tmx files for loading maps) and through reading the map it will find say an enemy on tile (16,12) with a name value of "man." It will create an instance of "Enemy.cpp" and in the constructor of "Enemy.cpp" it will take the name value and find "scripts/enemies/man.lua" and call the OnCreate() method of that .lua file which will set up the values that go along with the "man" enemy.
I also plan to have an Update() method that will keep track of AI and that particular enemies behavior. The Update() method will be called every time the enemy finished the last task provided by the Update() method.
I also plan to make similar scripts for abilities and actions/spells (like attacks or teleports) as well as quests (although those will be implemented fairly differently.)
I just want to make sure I'm on the right path with this as I haven't been able to find to many examples.

Share this post

Link to post
Share on other sites

Without elaborating your design, it could be ok or not. So let me check if we are on the same mind set. Let's do that easily by considering items first.

Suppose we have:

  1. food - decreases hunger attribute by F
  2. potion - increases health attribute by P

This does not need scripting. Data-oriented design is sufficient.

Enemies are a thougher call. In general, enemies need to have a set of skills which is where scripting comes into play. Personally I would never allow a script to fully define an enemy: there must be a basic set of properties about them. I'd rather allow script callbacks to evaluate its attack logic or skill use.

In my experience, having a generic Update() call to be shared across all entities is a bit fragile in concept. Better to have things such as RunEnemyLogic() or Tick(time) if everything fails. I don't have an Update() call in my system - distributing script processing is left to the script itself.

So the enemies might be:

  1. man { model: "man.mdl";  skillSet="humanSkills.lua";  skillList="hide, evade, attack"; };
  2. goblin { model: "goblin.mdl";  skillSet="gobSkills.lua";  skillList="attack, fury, resistance"; };

Of course in this case you'd have to figure out a way to maintain, for each "skill" loaded and bound to each actor, a way to maintain its state and design all the proper interfaces. Allowing to "just do everything" might make sense but I'm not quite sold on it.

Share this post

Link to post
Share on other sites

In addition to what was just said, scripts are most-often used to respond to events, such as taking damage.

In many games a lot of the events sent to scripts are related to physics to allow them to override the default engine way of handling their collision with things.  For example, a ball may hit a wall but with script override it simply bounces off via the engine’s physics implementation.  An enemy brute may hit a wall and decide to stop, turn, and start pounding on it to break through.


Scripts can also be used inside triggers, which are bounding boxes that, when touched by the player, cause some event to happen.  A cut-scene for example, or the spawning of a bunch of enemies.


For the most part, thinking of a script as “the logic for an event” will keep you fairly safe, as long as you have a good understanding of what needs to be an event and what should not.



L. Spiro

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!