2
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Mario
    Jun 9, 2013 at 9:14
  • \$\begingroup\$ Then consider some scripting language like Lua or any other language with Java integration. But you can create branching in XML as well. \$\endgroup\$ Jun 9, 2013 at 9:35
  • \$\begingroup\$ @sm4 Just a quick question; cannot find an example of boolean expressions in deelang. For example, would == compare two things? What about != or || and &&? \$\endgroup\$
    – Jax
    Mar 21, 2016 at 16:11

You must log in to answer this question.

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