Another way to fix our problem of immediately unregistering when an event script gets called is - instead of using a loop that increments to run the listener scripts, we could use a "reverse loop" (we start from the end and work our way to the start)! This means the order of our list won't mess things up for entries "downstream" when they get removed. We also wouldn't have to go to the bother of putting "return true" in certain scripts - we would just put event_unregister as normal. Thank you to xDGameStudios for pointing this out!
This works as long as instances remove only themselves form the list, but if one of your event callbacks removes another instance that has yet to be processed, you run into the same issue. I solved this in a project by using a temporary copy of the listeners list just for the loop. You do have to check for the existence of the instance running the event beforehand in this setup, but it's something I'd recommend anyways just as a safety net (if for some reason you forget to call unsubscribe somewhere).
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function. How is it work the event_fire() and the event_unregister()?
This video was supremely helpful with my current project, thanks for putting out such great videos! For anyone who may be having a similar issue, I encountered a problem with this system where I had an event listener tied to an instance that I needed to remain active rather than unregistering once it triggers, as the event handled toggling something on and off. However, because I restarted the room when the player died and the event handler was persistent, it would register a duplicate event listener every time the room restarted and cause the event to trigger multiple times for the same listener. To resolve this, I added an extra check to the event registry script to iterate through the listenerList(if one already existed for the given event) to see if the object ID matches any already in the list. If a match is found, the rest of the script is skipped over and the listener isn't added to the list.
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function. How is it work the event_fire() and the event_unregister()?
I would like to let you know that I have been studying and learning a lot of Game Maker Language on my own and all of the videos I watch by you are all of amazing quality. I frequent many different UA-cam channels on all similar topics and as I keep watching more and more tutorials I keep ending up learning from your videos the most. I really appreciate what you do and just want to make sure you know you are appreciated. :)
This is just what I was looking for. Thanks so much for posting this. I ended up putting my arguments into their own array like this: listenerInfo = [listener1, script, [arg1, arg2]] , and I used the "reverse loop" like you suggested. Those were the only changes I made. Great video.
Great tutorial, definitely needed! I implemented something similar (which by the way is essentially the observer pattern) for my inventory system, so that adding / removing from the inventory causes an event to be fired, decoupling the inventory data from its representation, and provides an easy way for the player to react based on the equipped items.
I just wanted to give feedback to you, as you helped so much with this tutorial. I immediately implemented the system in my project and must say that it is soooooo useful. Thank you for your time. Be awesome, and can't wait for more tutorials from you.
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function. How is it work the event_fire() and the event_unregister()?
I may suffer from too little sleep but I currently don't understand how to implement this. Do you plan on any follow ups explaining how to write scripts for this system?
if you dont understand how this is working here is a short comment I wrote for the code you can place in the event manager in case you forget. //event_register_script(event.rainStarts,id,script) this code adds its self as a listener and fires its code when that event_fires(). To fire an event and cause all listeners to react by running their scripts simply call event_fire(event) where the event is a named event in the enum below. example of the data structure created keys Values rainStarts [ [listener1"obj", script"genaric or unquie function"] [listener2"obj", script"^"] ] to unregister return true after listener excutes its script. ie ive taken my umbrella out dont take it out agian. you can always regester as a listener to event agian once its applicple. */ the reason I suggest adding this to your eventManager obj if you don't understand is that a quick look from the data structure and the enum should tell you what you need to do next inorder to have this code work for you. Thank you very much FriendlyCosmonaut
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function. How is it work the event_fire() and the event_unregister()?
how do the npc's know when the rain starts so they can pull out their umbrella edit: would I use the event_fire script, and also when would i use the event_unregister script
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function. How is it work the event_fire() and the event_unregister()?
Hey! I encountered a problem in the event_register where it says that the line ds_list_add(listenerList, listenerInfo) the argument takes a number. Which also makes me question DOES THIS FUNCTION ADDS AN ARRAY? EDIT: I fixed it I forgot the logical not in line number 11..
Can anyone clarify what the Rain Starts and Rain Ends listed in the numerator are? Are they objects? Scripts? I don't follow how those 2 data points in the "Events" enumerator helps you use Event_fire script. Any clarification would be greatly appreciated. I'm clearly missing how it all comes together.
they are the keys/the names of your events that you are registering your objs as "listeners" too in order to run their scripts when the enum value is called. so event_register_script(event.Rain_Starts,id,itsRaining()); when put into your creation code of an obj should register it as a listener to that enum and when event_fire(event.Rain_Starts); is called the listeners register to that should run their subsequent scripts.
I am having a weird problem where I can't get my system to register any new scripts I have created. that is I have an event.one, event.two and script_one and script_two I can have as many new events but I can not create any new scripts and have them work as register. naming conventions do not matter nor do create a new event manager. in fact, a separate event manager exacerbates this problem. Has anyone had trouble registering more than two scripts?
you use script_execute a lot in your videos, but that almost always costs a lot of processing power. when I optimize my games, oftentimes script_execute is first to go
Hi FriendlyCosmonaut! Hope you're well. tl;dr: I need help with registering 'events' from the 'enum(event){1,2...}' into the ds_map I'm a huge fan, and your videos help me so much for anything concerning GameMaker. I have so many positive things to say about you, from the amount of helpful/tutorial content you produce and its quality, to your own creations -- especially the devlogs about Project Magenta and your relationships/comradery with your sister! Anyway, the million potential compliments aside, I am struggling a bit on understanding this event system. I've everything as you've shown in the video, but I'm having trouble understanding how to say to these lists and scripts that "This object is a listener" and "this listener wants to perform this script" As I have it, I have an obj_demo with a demo_script, which just changes its sprite_index to a different coloured one. I have in my obj_demo's instance creation code: event_register_script(event.demoEvent, id, demo_script); I think what I'm struggling to understand is how the enumerator is actually used to tell the eventmanager to add that event into the map? I would appreciate it so much if you could try to explain where I'm going wrong, but I will endeavour to solve it on my own! Thanks so much for all of your videos, keep them up, you're very very talented! :D
@@rpgtogether7022 I know this was a year ago, and is a long shot, but what type of code is the "Event" that's listed in the event_fire script? In FC's example in it's listed as Rain Starts & Rain Ends. Are those 2 events in the event_manager object enumator objects? scripts? And where do you put Event_register?
Another way to fix our problem of immediately unregistering when an event script gets called is - instead of using a loop that increments to run the listener scripts, we could use a "reverse loop" (we start from the end and work our way to the start)! This means the order of our list won't mess things up for entries "downstream" when they get removed. We also wouldn't have to go to the bother of putting "return true" in certain scripts - we would just put event_unregister as normal. Thank you to xDGameStudios for pointing this out!
This works as long as instances remove only themselves form the list, but if one of your event callbacks removes another instance that has yet to be processed, you run into the same issue. I solved this in a project by using a temporary copy of the listeners list just for the loop. You do have to check for the existence of the instance running the event beforehand in this setup, but it's something I'd recommend anyways just as a safety net (if for some reason you forget to call unsubscribe somewhere).
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function.
How is it work the event_fire() and the event_unregister()?
5 years later, still an excellent system. Made a few updates with some of the nice new features GMS2 has, and it works wonderfully for my RPG project!
This video was supremely helpful with my current project, thanks for putting out such great videos!
For anyone who may be having a similar issue, I encountered a problem with this system where I had an event listener tied to an instance that I needed to remain active rather than unregistering once it triggers, as the event handled toggling something on and off. However, because I restarted the room when the player died and the event handler was persistent, it would register a duplicate event listener every time the room restarted and cause the event to trigger multiple times for the same listener.
To resolve this, I added an extra check to the event registry script to iterate through the listenerList(if one already existed for the given event) to see if the object ID matches any already in the list. If a match is found, the rest of the script is skipped over and the listener isn't added to the list.
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function.
How is it work the event_fire() and the event_unregister()?
I would like to let you know that I have been studying and learning a lot of Game Maker Language on my own and all of the videos I watch by you are all of amazing quality. I frequent many different UA-cam channels on all similar topics and as I keep watching more and more tutorials I keep ending up learning from your videos the most. I really appreciate what you do and just want to make sure you know you are appreciated. :)
This is just what I was looking for. Thanks so much for posting this. I ended up putting my arguments into their own array like this: listenerInfo = [listener1, script, [arg1, arg2]] , and I used the "reverse loop" like you suggested. Those were the only changes I made. Great video.
This definitely is the most elegant system I have come across in gamemaker so far, great video!
Great tutorial, definitely needed! I implemented something similar (which by the way is essentially the observer pattern) for my inventory system, so that adding / removing from the inventory causes an event to be fired, decoupling the inventory data from its representation, and provides an easy way for the player to react based on the equipped items.
One of the best gamemaker developer content creators out there. Keep up the excellent work it is very much appreciated.
I just wanted to give feedback to you, as you helped so much with this tutorial. I immediately implemented the system in my project and must say that it is soooooo useful. Thank you for your time. Be awesome, and can't wait for more tutorials from you.
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function.
How is it work the event_fire() and the event_unregister()?
You make the best tutorials on YT. You are great teacher.
PS Sorry for my poor english.
Thanks for updating ,cosmonaut ,long time no see!!!welcome back.
I look forward to the next part! Your Game Maker tutorials are the best on UA-cam))
Really like the tutorial but would also love to see another one that explains everything a little bit more with more examples.
Thank you for your hard work. Your videos are so clear and informative.
This is lovely! Thank you so much for your tutorials. I absolutely love them and they are incredible! Please keep up the amazing work!
Thanks!! I was waiting for this. Keep up the good work 🤩!
This is a fantastic tutorial. Thanks a bunch!
I may suffer from too little sleep but I currently don't understand how to implement this. Do you plan on any follow ups explaining how to write scripts for this system?
Okay, nevermind. It likely was the lack of sleep ;)
You're a genius coder.
if you dont understand how this is working here is a short comment I wrote for the code you can place in the event manager in case you forget.
//event_register_script(event.rainStarts,id,script) this code adds its self as a listener and fires its code when that event_fires().
To fire an event and cause all listeners to react by running their scripts simply call
event_fire(event) where the event is a named event in the enum below.
example of the data structure created
keys Values
rainStarts [
[listener1"obj", script"genaric or unquie function"]
[listener2"obj", script"^"]
]
to unregister return true after listener excutes its script. ie ive taken my umbrella out dont take it out agian.
you can always regester as a listener to event agian once its applicple.
*/
the reason I suggest adding this to your eventManager obj if you don't understand is that a quick look from the data structure and the enum should tell you what you need to do next inorder to have this code work for you.
Thank you very much FriendlyCosmonaut
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function.
How is it work the event_fire() and the event_unregister()?
hoooo yeah ...I was waiting for this one ! thanks a lot ... I really like the way you go in depth ! keep it up !
how do the npc's know when the rain starts so they can pull out their umbrella
edit: would I use the event_fire script, and also when would i use the event_unregister script
In the object where rain starts: fire rain start..
PS: Don't forget to register
Cosmonaut can you help me understanding shader?, i kinda having a hard time understanding it
In event.rainStarts i think there is a switch or something like that somewhere to trigger the rain function.
How is it work the event_fire() and the event_unregister()?
Useful and well presented. Thanks!
Love your videos. Keep up the great work.
Hey! I encountered a problem in the event_register where it says that the line ds_list_add(listenerList, listenerInfo) the argument takes a number. Which also makes me question DOES THIS FUNCTION ADDS AN ARRAY?
EDIT: I fixed it I forgot the logical not in line number 11..
@FriendlyCosmonaut do you think you could make a video about the hoverboard mechanics you had in the demo?
Can anyone clarify what the Rain Starts and Rain Ends listed in the numerator are? Are they objects? Scripts? I don't follow how those 2 data points in the "Events" enumerator helps you use Event_fire script. Any clarification would be greatly appreciated. I'm clearly missing how it all comes together.
they are the keys/the names of your events that you are registering your objs as "listeners" too in order to run their scripts when the enum value is called.
so event_register_script(event.Rain_Starts,id,itsRaining()); when put into your creation code of an obj should register it as a listener to that enum
and when event_fire(event.Rain_Starts); is called the listeners register to that should run their subsequent scripts.
@@brianjarema264 Thank you! I will take another look at it
This deserves more views
I am having a weird problem where I can't get my system to register any new scripts I have created. that is I have an event.one, event.two and script_one and script_two I can have as many new events but I can not create any new scripts and have them work as register. naming conventions do not matter nor do create a new event manager. in fact, a separate event manager exacerbates this problem. Has anyone had trouble registering more than two scripts?
Hey what a great video! Just wanted to let you know that your patreon link in the description isn't working.
Oh whoops, thank you! I fixed the description now haha.
I have a really hard time visualizing this in my head so I mostly copy pasted what I saw. I think stuff like this could really use some visuals.
THIS IS SO HELPFUL
This looks very similar to your cutscene tutorial!
you use script_execute a lot in your videos, but that almost always costs a lot of processing power. when I optimize my games, oftentimes script_execute is first to go
Hi FriendlyCosmonaut! Hope you're well.
tl;dr: I need help with registering 'events' from the 'enum(event){1,2...}' into the ds_map
I'm a huge fan, and your videos help me so much for anything concerning GameMaker. I have so many positive things to say about you, from the amount of helpful/tutorial content you produce and its quality, to your own creations -- especially the devlogs about Project Magenta and your relationships/comradery with your sister!
Anyway, the million potential compliments aside, I am struggling a bit on understanding this event system.
I've everything as you've shown in the video, but I'm having trouble understanding how to say to these lists and scripts that "This object is a listener" and "this listener wants to perform this script"
As I have it, I have an obj_demo with a demo_script, which just changes its sprite_index to a different coloured one.
I have in my obj_demo's instance creation code:
event_register_script(event.demoEvent, id, demo_script);
I think what I'm struggling to understand is how the enumerator is actually used to tell the eventmanager to add that event into the map?
I would appreciate it so much if you could try to explain where I'm going wrong, but I will endeavour to solve it on my own! Thanks so much for all of your videos, keep them up, you're very very talented! :D
EDIT: Use event_fire
I know this was a while ago, but did you ever figure this out?
@@rpgtogether7022 I know this was a year ago, and is a long shot, but what type of code is the "Event" that's listed in the event_fire script? In FC's example in it's listed as Rain Starts & Rain Ends. Are those 2 events in the event_manager object enumator objects? scripts? And where do you put Event_register?
OMg i think im going back to Rpg Maker 2000
Coding your own games is easier than you think.
top video!
Delightful voice. More lessons please)
P.S. From Russia with love.
Thank you!
I like this
thanks!
There are levels to this coding game....too high for me lol