Scripting Levels / Maps (with Editors)?


I am quite new to jMonkey and I could not find an answer to my question (if there is an answer in the forum, please just show me):

I want to build a Racing Game with jME2. There are specific situations in that game, where for instance specific events are triggered, when the player is in a certain position:

Example: The player's car is at a crossroad with red traffic lights, so he has to wait. Suddenly another car appears from behind with high speed, gets over the crossroad without caring about the taffic lights and drives away.

For that scene I have to script the level. When the player is within a radius of the crossroad, the traffic lights will turn red and after, say, 5 seconds the other car is created behind the player (20 metres away) and  is driving with 120 km/h over the crossroad.

Is there any kind of "Level Editor" with that functionality? Does anyone know how I can do that? If there is no Editor, how can I code that?

Thanks in advance!

Xerxes  :D

In jme i think the abstract class com.jme.scene.Controller is a good candidate to scripting stuff.

For the editor i'm writing, the approach i choosed is this: declare a generic interface for the scrip objects, like this:

package it.tukano.jupiter.script;

public interface Script {

    void initialize(Bindings data);

    void eval(Bindings data);

    void destroy(Bindings data);

And let each script define that interface. Bindings is a second interface:

package it.tukano.jupiter.script;

import com.jme.bounding.BoundingVolume;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.Spatial;
import java.util.Collection;

public interface Bindings {

    Camera getCamera();

    Spatial getSpatial(String id);
    float getTimePerFrame();

    Collection<Spatial> getSpatials(Vector3f from, Vector3f to);

    Collection<Spatial> getSpatials(BoundingVolume region);

    String createSpatial(Spatial spatial);

    void removeSpatial(String id);

    SpatialInfo getSpatialInfo(Spatial s);

In this scenario the example of the semaphore will look like this:

public class SemaphoreAI implements Script {

    public void initialize() {}

    public void destroy() {}

    public void eval(Bindings data) {
        Spatial car = data.getSpatial("id of the car object");
        Spatial semaphore = data.getSpatial("id of the semaphore object");
        Vector3f carCenter = car.getWorldBounds().getCenter();
        Vector3f semaphoreCenter = semaphore.getWorldBounds().getCenter();
        if(carCenter.distanceSquared(semaphoreCenter) < ACTIVATION_RADIUS) {
            //do something through Bindings
        } else {
           //do something else through Bindings

The scripts are plain java classes that will be compiled on the fly and dynamically loaded by a scene graph manager module when attached to a spatial.
Xerxes01 said:

There is still nobody, who has any tips or hints how to do that? I would really appreciate any kind of help!

The way I would approach something like this is by modularizing the events..  You could make an interface for 'story elements' or something of the like.

So say in your update method: (pseudocode)

if(car is near red light){
new RedLightEvent(parameters for the event);

The RedLightEvent could be something like this:

Car newCar = new Car(ColorRGBA.Red);
car.setLocalTranslation(getPlayerTranslation.subtract(new Vector3f(0,0,-20));, endVector, speed);

There is still nobody, who has any tips or hints how to do that? I would really appreciate any kind of help!

Java code != script code.  That is not scripting.  That is just moving compilation to the runtime where it is much more dangerous and much less controllable by the developer.

Scripting syntax should be simplified and is usually much more useful if it can be interpreted line-by-line.  Runtime apps should not require a JDK.

Most of the behavior you want is mandated by the game design and is known at build time.  You can and should make robust and flexible classes to enforce the needed game behavior at dev time.  Use scripting WITH IDIOT-PROOF SYNTAX for high-level runtime control using your well-designed objects.  Some strategies for real scripting would be JMX, BeanScript or the Java scripting-language hook JSRs, JFlex + Cup, but you can also make powerful interpreters using java.util.regex.

I think maybe your goal is to access application-specific behavior from a generic editor.  Scripting is pretty irrelevant to that requirement, as the loaded objects, not the editor-user, should have the app-specific intelligence, and simple interfacing can provide the needed hooks.  If you don't want your app designed before hitting the generic editor, there are late-binding and interpreted frameworks much more suited to that than Java.  You would eliminate many of the strengths of Java by using it in that way.