events

Published December 13, 2005
Advertisement
I wired up the event system to GMGX last night. It works very nicely for entities, I need to expand it to the 'Game' as a whole, so that you can raise game events too. I'm tempted to make the 'Game' an entity in itself so that you can raise events to that, meaning I don't have to duplicate any code. However I'm not sure how I feel about this, so may just add game-level events straight to the game object itself.

Events mean you can now do this:-


on_something = function( )
{
print("Oh no! Something happened to " + .target.props["name"] );
}

ent = ent_create();
ent.events["on_something"] = on_something;
ent.props["name"] = "Billy";

trg = trg_create_entity_position_trigger( ent, vertex( 50, 60 ), "on_something" );
trg.activate();


When the entity reaches position 50,60 an event 'on_something' is fired for the entity we created and named as 'Billy'. If we set the trigger to 'never expiring' (or otherwise reactivate it) we can fire this event whenever this entity steps on 50,60 (bear in mind we also have area tests for circles, rectangles and more) - this makes it ideal for switches and various other game triggers :)

We can now use it to start defining various collision callbacks; so setting an "on_collided" callback will result in that being called whenever two entities collide:-


on_collided_player = function()
{
ent1 = .props["entity"];
print( ent1.props["name"] + " has collided with " + .target.props["name"] );
};


Neat, huh?

I really need to start crafting the tilemap stuff so I can throw out a simple pacman clone to test all the things we have so far.

The Future

So far this GMGX engine experiment is turning out to be something special. What we have right now is a simple 2d engine that runs entirely on scripts written in a simple yet powerful language. My intention is to now bolster the graphical ability of this engine, providing tile mapping, parallax scrolling and other goodies so that it can be used in 'real 2d games'...
Previous Entry gmBind 0.9.5
Next Entry GMScript consts
0 likes 3 comments

Comments

jollyjeffers
Looking smooooooooooth [grin]

I'm definitely thinking that this sort of scripting should feature in some future work of mine yet. Don't know what it is, but it'll have scripting.

Can you combine event triggers together? Say...

if( player is in circle( x,y,r) )
    if( player is facing(direction) )
        Open the door
    else
        hint that they should turn around
else
    do nothing


Also, probably related to the same thing, but can you define a sequence of events that lead up to a trigger.

For example, in order for event Z to be raised they must have completed Objective A, Objective B and Objective C. However those objectives may not have happened at the same time, and might have had other things occur in-between...

Keep up the good work!
Jack
December 13, 2005 08:54 AM
evolutional
Quote:Also, probably related to the same thing, but can you define a sequence of events that lead up to a trigger.


Events are simple 'named' actions which can be fired on an entity or just globally on the game; triggers are an test / action pair - you can use event firing as a trigger action without an issue. A trigger is simply a 'test function' coupled with an 'action function' when test proves true. You can define pretty much scripted function for the tests or the action (the trigger is passed implicitly as 'this'), so you can do almost anything. You could chain triggers so that one is created or activated (set to perform its tests) when another is fired. The engine will define some useful defaults for you, so there's simple tests and actions like "if [entity] is here, fire this [event] on it", or "if [test] is true, fire this [event] on [object]" - if the game doesn't provide such a default it's very easy to fire up your own tests and actions to fire on them (see examples in previous entries).

Quote:For example, in order for event Z to be raised they must have completed Objective A, Objective B and Objective C. However those objectives may not have happened at the same time, and might have had other things occur in-between...


You could accomplish this by setting some game flags and in your trigger test functions you can test for certain flags being set. So trigger A would set flag A when it happens, but something could happen to set flag A to 0, so when trigger B is tested for it notices trigger A is unset, meaning the test for B fails.

Eg:



global GameFlags = table();
GameFlags["A"] = 0; GameFlags["B"] = 0;

global trgATest = function()
{
if (.props["entity"].position == vertex( 50, 50 ) && GameFlags["A"] == 0 )
{
return 1;
}
return 0;
};


global trgBTest = function()
{
if (.props["entity"].position == vertex( 150, 150 ) && GameFlags["B"] == 0)
{
if (GameFlags["A"] == 1) { return 1; }
else
{
print( "Trigger A needs to be fired!");
}
}
return 0;
};

global trgAAction = function()
{
GameFlags["A"] = 1;
}

global trgBAction = function()
{
GameFlags["B"] = 1;
}

trgA = trg_create( trgATest, trgAAction );
trgB = trg_create( trgBTest, trgBAction );

trgA.activate(); trgB.activate();


In this situation, trigger A is fired when player hits (50,50) which sets flag 'A'. However, if player went to (150,150) trigger B would only fire if flag A is set, if something unset the flag in between you must go back to 50,50 to reset it.

As I said, the actions could be anything, so if you wanted you could quite easily just raise an event which is handled to set the flags - in fact, there's a special trigger you can set up which fires an event when test is true:

trg_create_event_trigger( [testfunc], [event_name_to_fire], [entity_target], [event params...] );
eg:
trg = trg_create_event_trigger( myTestFunc, "on_test", "my_entity" );


Because we're scripted, it's let me be flexible with parameters, so that you can pass it a function by name "myTestFunc", or by object reference. Likewise, entities can be assigned a name and anything that expects an entity object can be passed the global entity name. This lets you assign "player" to your player entity object and just use the "player" tag anywhere instead of needing the object reference. Of course, you can retreive the object by its handle at any time too.

It's pretty flexible as to how you can do things, one of the reasons I like it [grin]. It should be suitable for GUI development too, I believe as you can attach events to input actions, so that an event is fired on a GUI element that is under the cursor when a mouse is clicked for example.

I hope this answered your question!
December 13, 2005 10:59 AM
jollyjeffers
Quote:I hope this answered your question!

Most certainly did - thanks!

Any doubts about triggers/events are gone [smile]

Jack
December 14, 2005 04:30 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement