What are Events?

BlitzMax Forums/BlitzMax Programming/What are Events?

BLaBZ(Posted 2010) [#1]
I've heard some incredible great things about events, is there an events tutorial? A place I could find an explanation of Hooks and EventQueues and their common usage?

Thanks!


Czar Flavius(Posted 2010) [#2]
Events are useful when your program will spend most of its time sitting around waiting for user input. For example a text editor. When the user isn't typing or clicking on a button, the text editor probably won't be doing anything either. For standard games, where action is always going on, I personally prefer the good old-fashioned game loop.

MaxGUI makes use of events - reading some tutorials for it might help you understand the concept behind events.

http://www.2dgamecreators.com/maxgui/


CS_TBL(Posted 2010) [#3]
It's also a different way to let actions happen. Imagine one general and 10 soldiers. In the old days of code, the general would instruct all soldiers individually to attack the enemy. In the event-days, the general sends out an event called 'AttackEnemies_KThxBye' and the soldiers are programmed to react on that specific event.

Or, in concrete code, the old days:

for t=0 to 9
soldier[t].AttackEnemy
next

And in the event days:

General.InstructTroupsToAttackEnemy

After this, *if* you have soldiers waiting for this event, then they'll attack. But if there aren't any soldiers waiting (haven't trained them, or they're already dead) then nothing happens. Also, the amount of soldiers or whether their index number is consecutive, doesn't matter, whoever reacts to this event will attack. You could even program a turtle to attack, as long as it reacts to that event (and has relevant attack code) This is a specific difference between the 'hardwired' first code. With events, things get less hardwired, and in a way 'easier', more natural perhaps.


Czar Flavius(Posted 2010) [#4]
Has anybody made an action or strategy game based around events? I'd be interested to see how that looks in concrete code.


BLaBZ(Posted 2010) [#5]
Wow thats sounds incredible! I'm guessing hooks are created via AddHook?!

It also sounds like if you have lets say 1000 soldiers, you don't have to continually loop through their updates, just feed what happens?! So you could have a ton more units as oppose to not using events.

Would it make multiplayer a bit easier? Through networking events?

If it's not to much to ask, how would you create an object that reacts to simple event?


CS_TBL(Posted 2010) [#6]
Well, the fun bit is that these 1000 soldiers aren't in any loop that's of your concern, they just 'are'. They manage themself, you don't have to worry about who listens when, that's all done automatically for you.

In the old days, you programmed actions. In these event days you program behaviour. And if that behaviour involves doing actions, then you have actions.


Czar Flavius(Posted 2010) [#7]
The lack of concrete code from a working prototype should warn you that this idea is better on paper than in practice. Programming isn't magic or sentient, no matter how many tricks you add.

So you could have a ton more units as oppose to not using events.
You can hide it or make it easier to manage, but you still have to program the nitty gritty of your game somewhere. Neither method would inherently be able to support more units than the other.

Of course if you can provide me with a non-trivial working example to prove me wrong...

Last edited 2010

Last edited 2010


CS_TBL(Posted 2010) [#8]
code+comments should be enough to get the idea..



Last edited 2010


BLaBZ(Posted 2010) [#9]
haha love the dialog

This is an awesome example and exactly what I was looking for, thanks so much!