DanmakuJSON Procedures

Procedures in DanmakuJSON work pretty similar to functions in programming languages. They are a way to encapsulate and reuse actions, which can be executed by the call-action from any bullet-source.

One important thing to keep in mind is, that all actions (and therefore all procedures) are operating on the bullet-source which executes them.

Defining procedures

To define procedures, you first have to add the element procedures on the top-level of your DanmakuJSON-file. Inside this element you can add keys (the names of your procedures), which contain an ActionSequence (a list of actions).

A simple DanmakuJSON-file which uses a procedure looks like this:

root:
{
    behaviour:
    [
        # Call the procedure "SimpleShot"...
        {call: {procedure: "SimpleShot"}},

        # ...wait for 2 seconds...
        {wait: {duration: 2}},

        # ...and call it again
        {call: {procedure: "SimpleShot"}}
    ]
},

procedures:
{
    SimpleShot:
    [
        # These actions will operate on the
        # bullet-source which called this procedure
        {repeat: {times: 10, actions:
        [
            {shoot: {speed: 5,
                     angle: 180}},
            {wait: {duration: 0.3}}
        ]}}
    ]
}

Passing parameters

In the previous example, all numeric values in the procedure are constant which results in the same behaviour for every call. To achieve much more flexibility, you can pass numerical parameters to a procedure which can then be used in Terms for calculations (see the call-action for details).

The following example shows how you can achieve different behaviour by using the same procedure but with parameters:

root:
{
    behaviour:
    [
        # Call the procedure "SimpleShot"
        # with the parameter "myAngle"...
        {call: {procedure: "SimpleShot",
                params: {myAngle: 200}
        }},

        # ...wait for 2 seconds...
        {wait: {duration: 2}},

        # ...and call the procedure again,
        # now with a different value for "myAngle"
        {call: {procedure: "SimpleShot",
                params: {myAngle: 160}
        }}
    ]
},

procedures:
{
    SimpleShot:
    [
        {repeat: {times: 10, actions:
        [
            # Note, that "angle" now uses a variable!
            {shoot: {speed: 5,
                     angle: "$myAngle"}},

            {wait: {duration: 0.3}}
        ]}}
    ]
}

Procedures calling procedures

Inside a procedure you can call another one, which allows even more flexibility. See the example below:

root:
{
    behaviour:
    [
        # Call the procedure "BulletChain"
        # with the parameters "bullets" and "myAngle"...
        {call: {procedure: "BulletChain",
                params: {myAngle: 200, bullets: 5}
        }},

        # ...wait for 2 seconds...
        {wait: {duration: 2}},

        # ...and call the procedure again,
        # now with different values
        {call: {procedure: "BulletChain",
                params: {myAngle: 160, bullets: 10}
        }}
    ]
},

procedures:
{
    BulletChain:
    [
        # The number of repetitions is now determined
        # by the parameter "bullets".
        {repeat: {times: "$bullets", actions:
        [
            # Call the procedure "SimpleShot" and
            # pass "myAngle"... 
            {call: {procedure: "SimpleShot",
                    params: {myAngle: "$myAngle"}
            }},
            {wait: {duration: 0.3}}
        ]}}
    ],

    SimpleShot:
    [
        {shoot: {speed: 5,
                 angle: "$myAngle"}}
    ]
}

Read more about actions to learn more about how they work.


Leave a comment