Hello I am trying to learn how to use events in C# I have watched a couple tutorials and I see them create the delegate and the event and call the event all in the same script. Then have a second script that listens and and responds when the event is triggered.
What I want to know is how can I call events in any script and make the other scripts react. Like I have a script that starts the game which I want to call the gameStart(true); event. But when the ball ends I want to call the gameStart(false); event. I have tried doing this buy just calling the events in the scripts but it only listens to it inside that script and all the others ignore it. What am I missing to do this?
Events can be called only from class that publish them. You can make a method for calling event from another class, but I think it is wrong way. You should think about events like a reaction for some changes inside obect - when object chenged - it raise an event.
Ok then what would be the best way to do a major change across the game globally. Like a a game starting letting all objects scripts know that they can move and attack and power ups can start activating?
One of tutorials I already watched. I am trying to find a way to call events outside of the script that makes them so I can make changes across many objects easily.
Oh boolean arguments, how I hate thee. gameStart(false) is a pretty awful method signature. Are you starting the game or not? What does that false argument mean?
Yea i know its pretty lazy and not well worded. Im just used it real quick for learning. It will eventually get passed a string so the game can have many different states rather than go and stop. Also changing that to gameStatus
Well you’re probably not using an “event” as defined by C#. More likely you’re calling a method. An event is not the same thing, even if your method execute a game event logic.
What exactly is happening right now is I set up an event script and made an event that passes asks for a boolean value. And then I set up a function in that same script that asks for a boolean value. Then every script that needs to call the event has a reference to the event script then calls the function while passing a boolean which in turn calls the event using the boolean I passed. After that every script that is listening to the event does what it is suppose to do.