View Poll Results: Is this use full for you?

Voters
8. You may not vote on this poll
  • Yes

    4 50.00%
  • No

    3 37.50%
  • I don't give a shit

    1 12.50%
Results 1 to 4 of 4

Thread: Release - COD2 Player Events

  1. #1
    ... connecting
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 8 Times in 3 Posts

    Post Release - COD2 Player Events

    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

  2. The Following 5 Users Say Thank You to Selbie For This Useful Post:

    EvoloZz (31st January 2013),kung foo man (31st January 2013),Mitch (31st January 2013),STAUFFi (31st January 2013),ysniper (17th April 2016)

  3. #2
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,725
    Thanks
    17
    Thanked 1,080 Times in 682 Posts
    I kinda like it, but could you add some redirections from the gametype.gsc file, and then remove the need for the waittills? (maybe add a var to disable that, or define smthing)

  4. #3
    Global Mossaderator Mitch's Avatar
    Join Date
    Nov 2012
    Posts
    654
    Thanks
    204
    Thanked 451 Times in 305 Posts
    It looks very nice. What are the benefits when you use this script?

  5. #4
    Brigadier General
    Join Date
    Oct 2012
    Posts
    998
    Thanks
    20
    Thanked 589 Times in 389 Posts
    Quote Originally Posted by Mitch View Post
    It looks very nice. What are the benefits when you use this script?
    It will cut down the number of instances of waittill() and endon() for each event. If you have lots of individual files all with an instance of waittill() and endon() for all the events, this adds to the maximum number of script variables the game engine will allow. So, building an event manager like this will make it more manageable and stop server crashes.

    Personally, I think callbacks are better, built out of the engine callbacks, and hooking into the game events and tracing back to certain files and folders. Callbacks require no script variables, and are therefore preferable to loops with script variables in them.
    Last edited by Tally; 1st February 2013 at 00:39.

  6. The Following 2 Users Say Thank You to Tally For This Useful Post:

    kung foo man (1st February 2013),ysniper (17th April 2016)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •