- Home /
Any references on how to handle RPG events in Unity?
Hello there,
I am working on this RPG game and I've come to the point where I need to develop the system for handling events. The thing is, I have a class called "interactable", and that class is supposed to serve everything from NPCs to treasure chests and doors. Programming the behaviour of each instance in the instance itself (in the case of Unity, in the prefabs' scripts) is, however, definitely not the way to go. I would like a way to express how interactables should behave (NPC dialogue, items contained in chests, etc.) from an external file (probably a text file) and load the pertinent interactable data depending on the room (scene) I'm in. Any tips on how to start on that?
Moreover, I need a way to handle the events that these interactables will trigger. I've looked at this reference for event handlers in Unity with C#: http://www.everyday3d.com/blog/index.php/2010/10/04/c-events-and-unity3d/ , which already helped me a bit, but I've thought of basically two ways to implement this in a game-wide system: Either one event controller per scene, which would have information on the events that could happen on that scene and handles all of it (activates listeners, etc.); or a game-wide event controller (a singleton), with information about events from the whole game. Which do you think is better? Or would you rather go with another alternative?
Thanks in advance for the replies
Answer by whydoidoit · Mar 15, 2014 at 03:18 PM
To add the behaviours from a file I would be tempted to make it an XML file that defines the list of MonoBehaviours to add for a particular item - then you can just loop through them and call AddComponent with each name.
There are really 3 ways of handling events in Unity. You should consider SendMessage/BroadcastMessage if these events happen sporadically (definitely not every frame). SendMessage is dead simple, works well with mix and match behaviours and is kind to memory.
If you will have things that happen every frame then you are going to have to go with delegates or events. For performance reasons a Singleton is probably not a good idea if there are lots of events, because a lot of unnecessary code is going to execute.
In the case of wiring up an individual objects events then that might be most easily achieved by sending a message to a game object after all of its components have been added, providing a reference for the thing that can be subscribed to. It's easy and the performance is fine for a startup process.
The problem with any events is memory leaks caused by not unwiring them when objects are destroy - if you go that way be sure you have your unwiring protocols clear.
If you wanted a singleton "pattern" with events I'd be tempted to make a dictionary on the event manager that linked event name keys to a list of delegates to be executed. You'd still have to be sure to remove dying delegates from the list, but at least you would be doing the minimal possible when an event was raised - the downside is that the events would all have a single signature (though that could of course pass dynamic objects). If you fancied it you could use multicast delegates rather than lists though it requires a few extra calls, it performs better.
Also you could check out this Unity Gems article on events. http://unitygems.com/getcomponent-vs-event/
@Nilk_Ritcorfe - If you question is answered, please click on the checkmark next to the answer to close it out. Thanks.
@robertbu - I would still like to know other people's opinions on this matter, is that all right?
@Nilk_Ritcorfe - you are in control of when/if you accept an answer. Accepting an answer doesn't stop additional answers or comments, but it does make it less likely that others will answer. A lot of new users don't understand that when they get their question answered, they should accept the answer. Accepting helps with future searches in the topic and also provides $$anonymous$$arma for the person that answered your question.
Your answer
Follow this Question
Related Questions
Persistent listeners for Button.onClick are removed at runtime, if buttons are instances of prefab 1 Answer
Listen to changes on attached by serializefield script? 1 Answer
How to access the persistent listener gameobjects on a button? 1 Answer
Weird behaviour when trying to dynamically create buttons 0 Answers
Any way to pass the function of a listener as a string? 1 Answer