Since there has been recent confusion on how to communicate from clientside to serverside and vice versa with regards to level NPCs, I have decided to write my second installment of the "How-To" series on the triggerAction() function. triggerAction() can be used to perform other actions as well but that is not a part of this explanation.
Important Notes
. triggerAction() is used to trigger an action from clientside to serverside or on the clientside and serverside themselves in level NPCs that are located in the x and y coordinates specified in the parameters of the function.
. To send information from the serverside to the clientside, use save[] or attr[]'s.
. triggerAction() MUST take at least 4 parameters, if you don't want to send any extra information, then use empty quotations (i.e "") as the fourth parameter
. Classes work as global level NPCs so you treat triggerAction() in classes the same way.
. You must define a shape for the npc on the serverside if you want to communicate to the serverside, on the clientside if you want to communicate to the clientside, or both if you wish to do both (use setShape() or setShape2()).
. You read the function triggered by the triggerAction() via onActionActionname(), where Actionname is the name you specify in the parameters of the function.
. When reading the x/y coordinates of the npc, use this.x and this.y to avoid problems on gmaps.
. If you stack NPCs on top of each other and trigger to the same coordinates as the stack of NPCs, then the onActionActionname() will be called in each NPC in the specified coordinates.
. The specified action name in the parameters must be written in quotations (which applies to just about all unique text in GS2).
Syntax
PHP Code:
triggerAction(x coord, y coord, "Actionname", extra, parameters, here,...);
Examples
Without sending any extra parameters:
PHP Code:
function onCreated() setShape(1, 32, 32);
function onActionServersideTrigger() {
echo("Success!");
}
//#CLIENTSIDE
function onCreated() {
setShape(1, 32, 32);
triggerAction(this.x+1, this.y+1, "ServersideTrigger", "");
}
Sending extra parameters:
PHP Code:
function onCreated() setShape(1, 32, 32);
function onActionServersideTrigger(bar) {
echo(bar @ "!");
}
//#CLIENTSIDE
function onCreated() {
setShape(1, 32, 32);
temp.foo = 1;
triggerAction(this.x+1, this.y+1, "ServersideTrigger", foo);
}