Hey all,
My latest script I created:
This will avoid running 100 of times running the same onPlayerConnect() script.
Code:
/*
Created by by: Selbie
Release Version: v1.0
Release date: 31-01-2013
Release For: Public
Contact me:
- Emai: kikker916@gmail.com
- xfire:selbie
Make sure this code runs first when startGametype() executes
Event registration is as follows:
level thread [[level.functions["registerEvent"]]]("onJoinedTeam", ::);
level thread [[level.functions["registerEvent"]]]("onJoinedSpectators", ::);
level thread [[level.functions["registerEvent"]]]("onMenuResponse", ::);
level thread [[level.functions["registerEvent"]]]("onPlayerConnect", ::onPlayerConnect);
level thread [[level.functions["registerEvent"]]]("onPlayerSpawned", ::);
level thread [[level.functions["registerEvent"]]]("onPlayerKilled", ::);
level thread [[level.functions["registerEvent"]]]("onPlayerDisconnect", ::);
*/
init()
{
// Enable event debug
level.events_debug = false;
level.events = [];
level.events["onJoinedTeam"] = [];
level.events["onJoinedSpectators"] = [];
level.events["onMenuResponse"] = [];
level.events["onPlayerConnect"] = [];
level.events["onPlayerSpawned"] = [];
level.events["onPlayerKilled"] = [];
level.events["onPlayerDisconnect"] = [];
level.functions["registerEvent"] = ::registerEvent;
level.functions["onEvent"] = ::onEvent;
// this line can be commented out, but make sure the events are set in the gametype .gsc file
level thread onPlayerConnect();
}
registerEvent(event, func)
{
if(!isDefined(level.events[event]))
{
if(level.events_debug)
iprintln("Register event failed: unkown event: " + event);
return false;
}
level.events[event][level.events[event].size] = func;
if(level.events_debug)
iprintln("Registered event: " + event);
}
onEvent(event, player, arg1, arg2)
{
if(isDefined(level.events[event]))
{
for(i = 0;i < level.events[event].size;i++)
{
if(isDefined(player))
{
if((isDefined(arg1)) && (isDefined(arg2)))
player thread [[level.events[event][i]]](arg1, arg2);
else if(isDefined(arg1))
player thread [[level.events[event][i]]](arg1);
else
player thread [[level.events[event][i]]]();
}
else
{
if((isDefined(arg1)) && (isDefined(arg2)))
level thread [[level.events[event][i]]](arg1, arg2);
else if(isDefined(arg1))
level thread [[level.events[event][i]]](arg1);
else
level thread [[level.events[event][i]]]();
}
resetTimeout();
}
if(level.events_debug)
{
if(isDefined(player))
{
if((isDefined(arg1)) && (isDefined(arg2)))
iprintln("Mapped " + level.events[event].size + "x event [" + event + "] to player: " + player.name + "^7 with args " + arg1 + "," + arg2);
else if(isDefined(arg1))
iprintln("Mapped " + level.events[event].size + "x event [" + event + "] to player: " + player.name + "^7 with arg " + arg1);
else
iprintln("Mapped " + level.events[event].size + "x event [" + event + "] to player: " + player.name + "^7");
}
else
{
if((isDefined(arg1)) && (isDefined(arg2)))
iprintln("Mapped " + level.events[event].size + "x event [" + event + "] with args " + arg1 + "," + arg2);
else if(isDefined(arg1))
iprintln("Mapped " + level.events[event].size + "x event [" + event + "] with arg " + arg1);
else
iprintln("Mapped " + level.events[event].size + "x event [" + event + "]");
}
}
}
else if(level.events_debug)
{
if(isDefined(player))
iprintln("Dropped event [" + event + "] from player: " + player.name);
else
iprintln("Dropped event [" + event + "]");
}
}
onPlayerConnect()
{
for(;;)
{
level waittill("connected", player);
player thread onEvent("onPlayerConnect", player);
player thread onJoinedTeam();
player thread onJoinedSpectators();
player thread onMenuResponse();
player thread onPlayerSpawned();
player thread onPlayerKilled();
player thread onPlayerDisconnect();
}
}
onJoinedTeam()
{
self endon("disconnect");
for(;;)
{
self waittill("joined_team");
onEvent("onJoinedTeam", self);
}
}
onMenuResponse()
{
self endon("disconnect");
for(;;)
{
self waittill("menuresponse", menu, response);
onEvent("onMenuResponse", self, menu, response);
}
}
onJoinedSpectators()
{
self endon("disconnect");
for(;;)
{
self waittill("joined_spectators");
onEvent("onJoinedSpectators", self);
}
}
onPlayerSpawned()
{
self endon("disconnect");
for(;;)
{
self waittill("spawned_player");
onEvent("onPlayerSpawned", self);
}
}
onPlayerKilled()
{
self endon("disconnect");
for(;;)
{
self waittill("killed_player");
onEvent("onPlayerKilled", self);
}
}
onPlayerDisconnect()
{
self waittill("disconnect");
onEvent("onPlayerDisconnect", self);
}
This script might look like similar script of IznoGod but I decided to write my own version.
It's up to you if you want to use it.
Selbie