December 04, 2020, 11:32:00 AM

Author Topic: [bb] Swift Event System by sswift [ 1+ years ago ]  (Read 371 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Swift Event System by sswift [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Swift Event System
Author : sswift
Posted : 1+ years ago

Description : This system allows you to create events which trigger at a certain time, remain active for a while, and expire.

There are 1001 uses for this.  Event systems are a must have for games.  All professional games use an event driven architecture.

For example, let's say that your player picks up a key and uses it on a door and the door opens.

Right there you could have a ton of events:  
* Make object dissapear and add to inventory.
   
   Which Triggers:
   * Emit particles for 3 seconds.

      Which Triggers:
      * Animate particle for 1 second.
 
* Player open door.

   Which triggers:
   * Play click noise.
   * Play creak noise.
   * Rotate door 90 degrees on Y axis.
   * Remove key from inventory.
Any time you need something to happen immediately, or happen 10 seconds from now, or you need something to appear, rotate for 30 seconds, and then dissapear, you need events!

One event can even trigger several others when it is in progress or expires.
You'll notice the code isn't all that complex.  Adding the cool stuff is up to you.  Just make functions to do the things you want each event to do, and stick the function call in the case staements in the appropriate functions.  

Don't add your code directly into the library, that will just make it messy and hard to read.  Just add function calls to code in your main game source.

Events can have data attached to them, but you have to modify the event type to do that.  I provided some data storage within the type already for ints floats and strings.  You should try to keep the data variables having general names and use the same ones for multiply types of events.  You only need one int if one event needs a counter and the other needs to store an entity pointer.  Though you might decide that you use entity ponters so often that you want a specific Entity data variable for storing just that.  What you do is up to you, I merely offer suggestions. :-)

Ideally I would have liked to have just used C like Structures and a single pointer for event data and you could create on structure for each type of event, but that leads to the necessity of deleting that data as well as the event data and I couldn't automate that if types were used for example...  And banks are a pain to use.  So adding a few variables to the event data as needed seems like the best solution for now.
I hoe you find this useful!  

I worked on it for several days to try to find the best and most generally useful way to do everyhting.  I was going to make it so that events could retrigger themselves and do it at random intervals, but then I realised that you can just add some simple code to specific events to do that so why bother making the system much more complicated for functionality people may only use for one or two events?


Code :
Code: BlitzBasic
  1. ; -------------------------------------------------------------------------------------------------------------------
  2. ; Swift Event System for Blitz Basic - Copyright 2003 Shawn C. Swift - sswift@earthlink.net
  3. ; -------------------------------------------------------------------------------------------------------------------
  4. ;
  5. ; License:
  6. ;      
  7. ;       You may use tand modify this code for free.
  8. ;
  9. ;       If you find it useful, a small donation would be appreciated!
  10. ;       To send a donation, go to http://www.paypal.com and send your donation to sswift@earthlink.net.
  11. ;
  12. ; --
  13. ;
  14. ; Code usage:
  15. ;
  16. ;       1. Add fields for data you want to store with your events to the Event type.
  17. ;
  18. ;       2. Add code to the Event_Start, Event_Active, and Event_End functions to perform specific actions for each type
  19. ;       of event in your game.
  20. ;
  21. ;       3. Create an event using NewEvent().
  22. ;
  23. ;       4. Call UpdateEvents() every game loop, passing it the current game time.
  24. ;
  25. ; --
  26. ;
  27. ; Notes:
  28. ;
  29. ;       ALL events trigger Event_Start() when they begin, Event_Active() every update during the time they are active,
  30. ;       and Event_End() when they expire.  These triggers can all occur, in that order, in a single update.
  31. ;
  32. ;       If you want an event to trigger another event when it expires, place code in Event_End() which calls NewEvent().
  33. ; -------------------------------------------------------------------------------------------------------------------
  34.                    
  35.  
  36. Type Event
  37.  
  38.         Field Event_Type
  39.  
  40.         Field Time_Start
  41.         Field Time_End
  42.        
  43.         Field Active
  44.        
  45.  
  46.         ; -- Add data for your events here:
  47.         ; -- All variables below are not used by the system and can be deleted at will.
  48.  
  49.         Field Data_Int[3]
  50.         ;Field Data_Float#[3]
  51.         ;Field Data_String$[1]
  52.                
  53. End Type
  54.  
  55.  
  56. ; -------------------------------------------------------------------------------------------------------------------
  57. ; Call this function to create a new event.
  58. ; The function returns a pointer to the newly created event so you can add data to it.
  59. ;
  60. ; Time_Start and Time_End are optional.
  61. ;
  62. ; If Time_Start is not specified, the event will trigger on the next call to UpdateEvents().
  63. ; If Time_End is not specified, the event will trigger AND expire on the next call to UpdateEvents().
  64. ; -------------------------------------------------------------------------------------------------------------------
  65. Function NewEvent.Event(Event_Type, Time_Start=-1, Time_End=-1)
  66.  
  67.         If (Time_End = -1) Then Time_End = Time_Start
  68.  
  69.         ThisEvent.Event = New Event
  70.        
  71.         ThisEventEvent_Type   = Event_Type
  72.         ThisEventTime_Start   = Time_Start
  73.         ThisEventTime_End     = Time_End
  74.         ThisEventActive       = False
  75.  
  76.         Return ThisEvent
  77.        
  78. End Function
  79.  
  80.  
  81. ; -------------------------------------------------------------------------------------------------------------------
  82. ; Call this function every loop to update your events.
  83. ;
  84. ; You should pass the current time, in milliseconds, to the function.
  85. ; (Time must always be a positive value.)
  86. ;
  87. ; (The reason I don't just grab millisecs() is so that your game time can be independent of the system time, which
  88. ; allows you to do effects like slow-mo and fast-forward while still having your events trigger at the right time.)
  89. ; -------------------------------------------------------------------------------------------------------------------
  90. Function UpdateEvents(Time)
  91.  
  92.         ; Loop through all events.
  93.         For ThisEvent.Event = Each Event
  94.  
  95.                 ; If it past the start time for this event...
  96.                 If (Time >= ThisEventTime_Start)  
  97.                                
  98.                         ; If the event is not currently active...
  99.                         If Not ThisEventActive
  100.        
  101.                                 ; Call the code which should be executed when the event starts, and set the event as active.
  102.                                 Event_Start(ThisEvent, Time)
  103.                                 ThisEventActive = True
  104.  
  105.                         EndIf
  106.                
  107.                         ; Call the code which should be run continuously while this event is in progress.
  108.                         Event_Active(ThisEvent, Time)
  109.  
  110.                         ; If it is time for this event to end...               
  111.                         If Time >= ThisEventTime_End
  112.  
  113.                                 ; Call the code which should be executed when this event ends, and delete the event.
  114.                                 Event_End(ThisEvent, Time)
  115.                                 Delete ThisEvent
  116.                                
  117.                         EndIf
  118.                
  119.                 EndIf  
  120.  
  121.         Next
  122.  
  123. End Function
  124.  
  125.  
  126. ; -------------------------------------------------------------------------------------------------------------------
  127. ; This function is called when an event first starts.
  128. ; -------------------------------------------------------------------------------------------------------------------
  129. Function Event_Start(ThisEvent.Event, Time)
  130.  
  131.         Select ThisEventEvent_Type
  132.        
  133.        
  134.         End Select
  135.  
  136. End Function
  137.  
  138.  
  139. ; -------------------------------------------------------------------------------------------------------------------
  140. ; This function is called continuously while an event is in progress.
  141. ;
  142. ; To find out how much time has passed since an event started when animating an object, do this:
  143. ; Time_Elasped = ThisEventTime_Start - Time
  144. ;
  145. ; To find out where, 0..1, in the event you are do this:
  146. ; Event_Point# = Float(ThisEventTime_Start-Time) / Float(ThisEventTime_End-ThisEventTime_Start)
  147. ;
  148. ; I don't do this for all events because some events are instantaneous, which would cause a divide by 0 error, and
  149. ; others trigger immediately and have a Time_Start of -1.
  150. ; -------------------------------------------------------------------------------------------------------------------
  151. Function Event_Active(ThisEvent.Event, Time)
  152.  
  153.         Select ThisEventEvent_Type
  154.        
  155.        
  156.         End Select
  157.  
  158. End Function
  159.  
  160.  
  161. ; -------------------------------------------------------------------------------------------------------------------
  162. ; This function is called when an event ends.
  163. ; -------------------------------------------------------------------------------------------------------------------
  164. Function Event_End(ThisEvent.Event, Time)
  165.  
  166.         Select ThisEventEvent_Type
  167.        
  168.        
  169.         End Select
  170.  
  171. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal