Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm in the planning phases of a 2D java game. The game is a tile-based platformer with some action-y parts to it, like a Metroidvania formula game.

The static portion of each stage is expressed as an x y grid of tiles - tile IDs correspond to drawable resources, and a boolean for collision (sky / air space doesn't cause collision with the player, but the ground or an elevated platform would for example)

Additionally, the game uses actors for dynamic things in the game - these could be monsters, boss mobs, moving platforms, checkpoints, powerups - anything that isn't a static tile.

I want to implement a java class ScriptedEvent, and include in each Level a linked list of scriptedevents for the stage.

ScriptedEvent may include a bool hasBeenActivated, bool canBeRepeated (for puzzle-like activities that may be re-ran by the player). I want each instance of ScriptedEvent to do an action, like MovePlayerTo(x,y), or combination of actions - SpawnEnemy(ID,x,y). Scripted events might also change the ID of a ground block to air, allowing the player to fall into a hidden room.

How do you accomplish this task in java? One thought would be to do eval() and have each event contain a string of steps. The steps then could be parsed out one by one from this large string, and interpreted as java code, handled by the main game logic.

Say a very simplfied example constructor might look like:

  ScriptedEvent ev1 = new ScriptedEvent("MovePlayer(200,0); PlaySound('teleport.wav'); SpawnItem(20,205,0);");

Where each function listed in the string would make the game logic do something.

Is there a better way to go about scripted events or should I roll with this solution? I don't really want to hard-code each event into a stage if I can avoid it.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Separate the actual code and the events completely. I'd recommend using XML to describe the event. Create a schema that will be able to desribe any event and then implement a parser of the events.

For the parser, I'd recommend using XMLReader and it's method setContentHandler. Your own content handler can then implement methods like:

public void startElement(String uri, String localName, String qName, 
        Attributes atts) throws SAXException { 
    if("playSound".equals(qName)) { 
        playSound(atts.getValue("soundName"));
    } 
} 

And your XML event will contain:

<playSound soundName="ding.wav"/>

If you want to use more powerful scripting, you can add some scripting language support. One of the easiest is DeeLang.

share|improve this answer
    
I guess the goal overall would be to be able to use some real scripting (e.g. branching) rather than only collecting several events or actions to do. –  Mario Jun 9 '13 at 9:14
    
Then consider some scripting language like Lua or any other language with Java integration. But you can create branching in XML as well. –  sm4 Jun 9 '13 at 9:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.