Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Eventmanager
#1
Got my idea from openwarfare, but made my own version. In my opinion does it make your code more clear and consisting of lesser code. Here's my version:
Code:
init()
{
    level.event = [];
    level.event[ "connecting" ] = [];
    level.event[ "connected" ] = [];
    level.event[ "spawned" ] = [];
    level.event[ "death" ] = [];
    level.event[ "killed" ] = [];
    level.event[ "disconnected" ] = [];
    level.event[ "joined_team" ] = [];
    level.event[ "joined_spectators" ] = [];
    level.event[ "menu_response" ] = [];
    
    level.on = ::addEvent;

    [[level.on]]( "connected", ::onPlayerSpawned );
    [[level.on]]( "connected", ::onPlayerDeath );
    [[level.on]]( "connected", ::onPlayerKilled );
    [[level.on]]( "connected", ::onPlayerSpawned );
    [[level.on]]( "connected", ::onPlayerJoinedSpectators );
    [[level.on]]( "connected", ::onPlayerJoinedTeam );
    [[level.on]]( "connected", ::onPlayerMenuResponse );

    thread onPlayerConnecting();
    thread onPlayerConnected();
}

addEvent( event, process )
{
    if( !isdefined( level.event[ event ] ) || !isdefined( process ))
        return;
    level.event[ event ][ level.event[ event ].size ] = process;
    
}

onPlayerConnecting()
{
    while(1)
    {
        level waittill( "connecting", player );
        for( i=0; i<level.event[ "connecting" ].size; i++ )
            player thread [[level.event[ "connecting" ][i]]]();
    }    
}

onPlayerConnected()
{
    while(1)
    {
        level waittill( "connected", player );
        for( i=0; i<level.event[ "connected" ].size; i++ )
            player thread [[level.event[ "connected" ][i]]]();
    }    
}

onPlayerSpawned()
{
    self endon( "disconnect" );

    while(1)
    {
        self waittill( "spawned_player" );
        for( i=0; i<level.event[ "spawned" ].size; i++ )
            self thread [[level.event[ "spawned" ][i]]]();
    }
}

onPlayerDeath()
{
    self endon( "disconnect" );

    while(1)
    {
        self waittill( "death" );
        for( i=0; i<level.event[ "death" ].size; i++ )
            self thread [[level.event[ "death" ][i]]]();
    }
}

onPlayerKilled()
{
    self endon( "disconnect" );

    while(1)
    {
        self waittill( "killed_player" );
        for( i=0; i<level.event[ "killed" ].size; i++ )
            self thread [[level.event[ "killed" ][i]]]();
    }
}

onPlayerJoinedTeam()
{
    self endon( "disconnect" );

    while(1)
    {
        self waittill( "joined_team" );
        for( i=0; i<level.event[ "joined_team" ].size; i++ )
            self thread [[level.event[ "joined_team" ][i]]]();
    }
}

onPlayerJoinedSpectators()
{
    self endon( "disconnect" );

    while(1)
    {
        self waittill( "joined_spectators" );
        for( i=0; i<level.event[ "joined_spectators" ].size; i++ )
            self thread [[level.event[ "joined_spectators" ][i]]]();
    }
}

onPlayerDisconnected()
{
    while(1)
    {
        self waittill( "disconnect" );
        for( i=0; i<level.event[ "disconnect" ].size; i++ )
            self thread [[level.event[ "disconnect" ][i]]]();
    }
}

onPlayerMenuResponse()
{
    self endon( "disconnect" );

    while(1)
    {
        self waittill( "menuresponse", menu, response );
        for( i=0; i<level.event[ "menu_response" ].size; i++ )
            self thread [[level.event[ "menu_response" ][i]]]( menu, response );
    }
}
Just thread init() once. Then you can use [[level.on]] anywhere you want to add new events.
Example usage:
Code:
init()
{
    [[level.on]]( "connected", ::onPlayerConnected );
    [[level.on]]( "spawned", ::onPlayerSpawned );
    [[level.on]]( "menu_response", ::onPlayerMenuResponse );
}

onPlayerConnected()
{
    iprintln( self, " is connected." );
}

onPlayerSpawned()
{
    self iprintln( "You are spawned!" );
}

onPlayerMenuResponse( menu, response )
{
    self iprintln( "Menu: ", menu, " response: ", response );
}
Hope this helped some people.
Reply

#2
I dont exactly get it but thnx Big Grin
Reply

#3
Good tutorial.
[Image: MaEIQ.png]
Reply

#4
Looks very good, now i can get rid of several unnecessary loops.
And you saved me the trouble of having to make this myself.

Thank You
Reply

#5
Nice.
Reply

#6
Nice, but seems like double work to me? When doing alot of stuff it might be handy to keep it organized and shiz.

Ty
(08-10-2011, 12:58)Pozzuh Wrote:
Se7en Wrote:Stealed, from cod4 mod ...
look who's talking

[Release] Old School Mod v2.2
[Release] Scroll menu

Reply

#7
Nice tutorial !
Nyan Cat
C++/Obj-Cdeveloper. Neko engine wip
Steam: Click
Reply

#8
Hmm isn't it supposed to be

Code:
level thread [[level.on]]( "connected", ::onPlayerConnected );
(08-10-2011, 12:58)Pozzuh Wrote:
Se7en Wrote:Stealed, from cod4 mod ...
look who's talking

[Release] Old School Mod v2.2
[Release] Scroll menu

Reply

#9
(08-28-2011, 20:56)iAegle Wrote: Hmm isn't it supposed to be

Code:
level thread [[level.on]]( "connected", ::onPlayerConnected );
Nope not neccesary.


(08-28-2011, 20:04)iAegle Wrote: Nice, but seems like double work to me? When doing alot of stuff it might be handy to keep it organized and shiz.

Ty
I don't see where it is double work. It is to avoid double work Wink
Reply

#10
(08-28-2011, 21:38)Justin Wrote:
(08-28-2011, 20:56)iAegle Wrote: Hmm isn't it supposed to be

Code:
level thread [[level.on]]( "connected", ::onPlayerConnected );
Nope not neccesary.

Ohh now i get it, you make 1 file that collects every event from a certain var and then it does all these. Its kinda complicated lol
(08-10-2011, 12:58)Pozzuh Wrote:
Se7en Wrote:Stealed, from cod4 mod ...
look who's talking

[Release] Old School Mod v2.2
[Release] Scroll menu

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.