18. Bonus: Event Handlers for Connecting/Disconnecting¶
18.1. Basic example¶
Let’s say you have this script called scr_welcome
:
show_message("Cool! Someone connected!");
return true; //- We will come to that later
You want to execute this script, if someone connects. To do that simply use this code somewhere after the engine was started (obj_htme is created):
htme_serverEventHandlerConnecting( scr_welcome );
That’s all you have to do. For a script to be run at disconnection,
simply call htme_serverEventHandlerDisconnecting
instead.
18.2. What is the return value for?¶
As you saw in the example above, the script scr_welcome
returns
true
.
If you are using a script as an event handler for connecting players, you need to return either true or false (you don’t have to return anything for the Disconnect-Event).
18.3. Getting more information about the player¶
Both, the disconnect and connect event, provide an argument for your script.
Here is an example how you can use this information. The server will refuse all connections from the local computer when using this script:
///somewhere in your code
htme_serverEventHandlerConnecting( scr_no_local_clients );
htme_serverEventHandlerDisconnecting( scr_goodbye );
///scr_no_local_clients(player_map);
var player_map = argument0;
if (player_map[? "ip"] == "127.0.0.1") return false;
else {
show_message("Cool! "+player_map[? "ip"]+":"+string(player_map[? "port"])+" connected!");
return true;
}
///scr_goodbye(player_map);
var player_map = argument0;
show_message(":( ! "+player_map[? "ip"]+":"+string(player_map[? "port"])+" with the hash "+player_map[? "hash"]+" left!");