Reference parameters and callback functions detailed

A new update to the engine brings complex reference parameters which I’ll detail here for future devs to look to.

I’ll also detail callback functions here.

Reference parameters is a parameter string in the tool which gets parsed in such a way for the parameter to be filled in with a result that’s stored in a reference.
At the moment, some of the tool such as the autocomplete does not make this as easy of a job as it should be, but that’ll be fixed later.

Variable Types

Private variables are never sent to the players unless they are specially referenced and emitted.
Public variables are emitted to players whenever an object is made known to them, as well as updates to an object state so long as they know of it.
When using setVariable, it defaults to setting a variable as public unless you are setting it to (type)objectName.private
A variable should explicitly be set as private, unless you’re positive you want the player to know of it, because it wastes a lot of resources. For just game logic, they should be private.

Basic JSON objects and arrays can also be set for a variable now. Just by setting it as {} or [], respectively. One filled out can be set by supplying a JSON parsable string, such as {foo:’bar’, baz:’quux’} or [‘foo’, ‘bar’, ‘foobar’], and there are functions for iterating array as well as the usual object lookup syntax(described below) working to pull out object properties.

Object lookup syntax

The engine uses its own syntax for parameter look ups, but it’s extremely similar to ECMA6 ones with the exception of type lookups.

(type)id
where “type” is the object type, such as scenario, player, trait, quest, item. “id” is the key name of it, which is often “this” when you’re trying to reference an objects self, but otherwise is the key name of the object type you’re trying to look up.
All objects such as Scenario are a sub-class of Object, so (object)this is the same as (scenario)wilderness-greeter, for example, if (object)this is being set on a parameter for that object.

object.variable
where “object” is a Game Object or JSON one, this will get the value of the property “variable”.

[reference]
where “reference” is the reference you want the value of.
This is often used for something like (item)[name] where you have the “name” of an item you want stored in a variable, instead of writing on one specific name.

All of these can be chained, nested, and mixed together indefinitely.

A simple example is when you use the setVariable function, say setting (player)this.private myVar foo.  then player.private.name would be set to “foo”.
To then get this “foo” somewhere else for the same player, you would simply put (player)this.private.foo into a parameter.

ex-complex1
A more complex example is like this.
“data” is an inherit callback that is always there on custom network events, where is the data object a client has supplied. See more about callbacks below.
First off, it’s just checking to make sure a player is marked as movable. I’m not letting stats be changed when a player can’t move, which is typically because they’re in the middle of a Scenario.
“canAffordStat” is a function that sees if a player has enough statPoints to a afford a stat upgrade. However, it needs some parameters supplied to know what it’s trying to calculate. It needs to know the current stat level, and what I’m trying to raise it to.
Firstly, data is something the player has sent like {name:’str’, lvl:4}.
It can then get the current stat level with (player)this.[data.name]. Where data.name is str, it’s getting (player)this.str which might be “3” for example.
data.lvl may be “4”, so then canAffordStat has the parameters it needs to know; starting at 3, and trying to raise to 4.
In this case, the “!” is selected inverting it, so I’m trying to see if it CAN’T afford the stat. In which case it resets the variable to whatever it already is. (Which really does nothing since it only propagates variable changes when they’re actually changed, but I was testing stuff out. :p)

These are mostly used for the game mechanics logic, but the same thing is there to make extremely complex and deep Scenarios.

Callbacks

A callback is a parameter of a function, generally the last one, that supplies the set variable to all functions nested inside of it.
This callback is a reference to the data the function has returned.


A better explanation is here.
This is the statement that, though it’s a little unfinished, makes a Scenario pop up for a player as they move on the map.
Every time a player moves to a new tile on the map, this event fires for them.

“roll” is a function that returns a number between the first and second parameters given. 0-99 in this case. Here I’ve named what it returns as “roll” with the third parameter. That “roll” is then available within that nesting (there is a small box made and the other lines are then inside that).
A callback variable is highlight in neon pink.

“arrayforEach” is a function that loops through an array, returning each item of the array.
In this case, the event is on the map itself, and (object)this.private.scenarios is an array of scenarios.
Thus, for each scenario, it’s checking if its rollChance is above the returned number. (In the case of rollChance being 100, it’d always be greater)
If the rollChance is indeed greater than the roll it made, it runs the triggerForPlayer function which will instance and run the given Scenario for that Player.

 

In addition…

In the previous engine, there was no way to expose public variables about a scenario to the player for the writing parser to use, and parsing was limited as a result.
There will be an update to the writing parser in the near future that will allow the parser to reference a Scenarios variables in them, just like how the Player’s variables are currently referenced in the writing parsing.
For example:
%scenario.npc.eyes could be some eye description set for the npc variable, which references the “eyes” variable of the Custom Object stored on the Scenario as “npc”.
%scenario.lastPlayer could be set as the player’s name, and be used as something like “‘%scenario.lastPlayer was here!’ was sprayed on the wall”.
A variable could be a number that’s iterated up, and some writing could be in a (if scenario.number >= 500){this will show if this scenario’s public number variable is at least 500}

Bookmark the permalink.