2

Maybe this was already discussed somewhere, I myself could not find an exact answer how to approach my problem:

I have a mutliniear «story» which executes code in each segment. I wrote a state machine, which initiates a new segment, whenever another segment calls that one. Each segment has an onEnter-, an onCheck and an onLeave function. As the name already says, the onEnter executes when the segment is called, the onCheck checks some input until some conditions are fullfilled (if yes they will lead to another segment) and onLeave executes just before the next segment is called.

Currently I just wrote a javascript object kinda like this:

var flow = {
    seg1: {
        onEnter: function() {
            this.say('Seg1');
        },
        onCheck: function(data) {
            if (data.condition) {
                machine.callNextSeg('seg2');
            } else if (data.condition2) {
                machine.callNextSeg('seg3');
            }
        },
        onLeave: function() {
        }
    },
    seg2: {
        onEnter: function() {
            this.say('Seg2');
        },
        onGestureCheck: function(data) {
        },
        onLeave: function() {
        }
    },
    seg3: {
        onEnter: function() {
            this.say('Seg3');
        },
        onGestureCheck: function(data) {

        },
        onLeave: function() {
        }
    }
};

The example is a bit simplified for understanding, but the code inside would be a little more complex.

I would rather like to have a JSON file, which is loaded, parsed and creates such an object. The JSON File should use a more simple and more abstract syntax to write. Especially to make it quicker.

I imagine something like this:

{
    "seg1": {
        "onEnter": {
            "say": 'Seg1'
        },
        "onCheck": {
            "condition1": "seg2",
            "condition2": "seg3"
        },
        "onLeave": {

        }
    },
    "seg2": {
        "onEnter": {
            "say": 'Seg2'
        },
        "onCheck": {

        },
        "onLeave": {

        }
    }
}

The conditions are booleans and if true the described segment should be called.

Would that be easy to parse the json, create an object, and create functions inside it for each onEnter, onCheck and onLeave? And also write the necessary if clauses?

Thanks for hints into the right direction.

cheers

J.

1 Answer 1

1

var flow = {...} is an object literal and is actually best suited for what you need to do, but I do understand the desire to have all of this "defined" in a JSON file. The issue there becomes the fact that the application processing your JSON file MUST understand/be Javascript (which defies the idea of having JSON as language agnostic - its name notwithstanding). This might be what you need: JavaScript Function Serialization

Also check the voted answer here: What is the correct way to "serialize" functions in javascript for later use

Sign up to request clarification or add additional context in comments.

1 Comment

Alright. Thanks for your comment. In my case my file would be javascript, so that would not be a problem. I'll try to build something alike. If I can't get it to work I'll stay with the object literal. Although I'd really like to have some simplified writing of the flow...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.