Events system.

Previous topic - Next topic

Hatonastick

Not quite sure how to explain this, but I'm trying to add the traditional events system that many MUDs use to my game.  Basically you have a list made up of structures with several pointers and variables.  The main two being a timer or counter integer (depending on how you go about keeping track of time passed ie. ticks with countdowns, or comparisons with the system clock), and the other being a pointer that points to a function associated with that event ie. a function called when that event is due.

You'd have an AddEvent(timer, function) that adds events to the event queue, RemoveEvent(event) that removes it, and a CheckEvents() function that ran through the list and checks to see if an event needs to be called.

You'd end up with a list a bit like this:
60, BufferCleanup
12, CheckIdleTimers
etc.

Where every 60 seconds a function called BufferCleanup would be called, every 12 seconds CheckIdleTimers would be called.

Now the hard part, the bit I have no idea how to implement under GLB would be the structure that allows any GLB function to be pointed to as GLB (to the best of my knowledge) doesn't have pointers (which I'm glad about) unless I start delving into INLINE again.  I guess what I'm asking is, is there a way to make an event system in GLB without resorting to the C pointer trickery (the trickery being where you could have a pointer in the event structure pointing to the function that was to be called) used in my original C code?

The example uses given here are pretty minor, but a lot of the rest of the MUD relies on this system -- especially when you get into the scripting side of things.  What I've listed here as examples could be replaced by having a bunch of calls in the main loop, but the functionality I will require needs to be more flexible than that ie. be able to add and remove functions being called from the main loop at will.

If I have to, I'll post what C code I can, but it's all over the place (there's about 40 files in my original MUD) and probably incomplete as the only code I have left from the original is the beginnings of my rewrite.

For portabilities sake I'm trying to avoid threads as that would involve an external library of some sort (if such existed).

Also I'm not asking for someone to write one for me, just a few pointers (bad pun) in the right direction. :)

Hatonastick

#1
Ohhhh thanks mate!  Gives me a few ideas, I'll have a go at getting it working.

Edit: Ok while ADDRESSOF() is useful in getting the address of a function, there is no way to then call that function without doing some awful hack using SORTARRAY() -- at least as far as I can see.  I might have to consider another approach instead.  Maybe using CALLBYNAME() and using sub-routines.  That would allow me to store the sub-routine name as a string.  Pity we can't use CALLBYNAME() to call functions. :)