• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Does GMStudio 2 have Observers?

H

Homunculus

Guest
Not natively. There's a series of two videos by FriendlyCosmonaut though that shows a way to implement an event system that's based on the same principle:

As explained in the comments below the second video though, things get a bit messy when instances try to remove themselves or other instances from the "observers" list during an event callback, so keep that in mind.
 
Last edited by a moderator:
C

Catastrophe

Guest
I'm pretty sure that is what the entire event system actually is. Every step all objects with a step event receive a notification to run the step event, every mouse_click every object registered for mouse click runs it, etc. Implementing your own is a simple matter of making your own user defined events and calling them. One simple way would be making a parent object for all of your objects, with some user defined events, overload them with objects that want t run the event, and then using with (all) {evt_perform(num)} when you want to run the event.

Whether or not this is actually useful is another thing, the event system already in GMS is pretty good. The only time I've seen this put to good use is in Rimworld, where the tickmanager allows for some objects to only run a step event every X ticks, but without needing to actually do the if (x ticks have passed) inside the used objects, which saves execution time.
 

Yal

šŸ§ *penguin noises*
GMC Elder
I'd say the simplest way would be to have a system like this...
  • Make a script called observer_setup.
  • It creates a list of local variables named things like "script_dooropenedevent" for all things you want to observe. All of them are set to a null value.
  • Every object in your game calls this in its create event.
  • Objects that want to subscribe to an event sets the corresponding variables to a script, their callback.
  • When an event comes, let every instance run the corresponding script (if any)
Code:
with(all){
  if(script_dooropenedevent != null){
       script_execute(script_dooropenedevent)
  }
}
  • If you want to add more types of events later, just add them to observer_setup - since everything already runs it, the variable will be defined without you having to go back and change a million objects.

You can of course speed this up if all relevant objects must inherit from a "event listener" parent, so you don't need to loop over ALL instances, and ALL instances don't need a freshly baked fudgeton of variables they won't ever use.
 

FrostyCat

Redemption Seeker
Traditional OOP patterns like this will soon be available natively with the GML 2020 update. Dinosaurs notwithstanding, I expect most of the workarounds presented thus far to fall into obsolescence in about a year's time.
 
Top