I am building my own 2D engine and I try to handle the progression of my character.

For example, when the player has a special item an door opens or when he talks with someone and has an item, the person reacts in a different way.

I don't want to hardcode that. Is there an common way to handle such things? I thought about a FSM to handle that but it seemed too complex for it. (I want an easier way.)

At the moment I handle it with several if statements that check if the characters items array has item X then say this and if he has item Y then say that.

PS: I am writing my engine in JavaScript so if you post code, I can live with Java and AS 3, but not with C/C++.

  • \$\begingroup\$ Common thing to do is to hardcode it in part of code you designate as scripts and therefore its no longer a code but data. In your engine you would then use something as "Door.js" class. In your map file you would define some door as "DoorSilverKey" and in your script (loaded and created accodring to data in your map) you would have DoorSilverKey.js which would open only if you have silverkey (handled by js code). Dialogues are more complex though, since most of games has some sort of dialogue system and stores dialogs as trees (and each node can have javascript condition for example) \$\endgroup\$
    – Kikaimaru
    Aug 19, 2013 at 11:24
  • \$\begingroup\$ I think Graph/FSM is your only choice ! But you "can" implement it in a simple way if you think on it depends on what you need to support in your engine . \$\endgroup\$
    – Emadpres
    Aug 19, 2013 at 11:27

1 Answer 1


Most games which are more complex use a scripting language to script world interactions.

Certain situations in the game can trigger scripted events (interacting with NPCs, killing a certain enemy, stepping into a certain area, picking up an item...). When such an event is triggered, a function from a script file is executed, which in turn calls back some functions of the game ("script bindings") which makes changes to the world.

Which of these events triggers what script is usually not hard-coded, but defined in the map editor or item description files.

Such a scripting language can either be self-written, or you can use an existing scripting language. I would recommend the latter, because writing a fully-functional programming language is a really hard task, both in planning and in execution. Many popular scripting languages have libraries for integrating them into all kinds of popular "real" programming languages. This gives you a well-tested implementation of a well-designed language with a minimum amount of work.

Which scripting language to use is a matter of personal taste.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .