Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nilk_Ritcorfe · Mar 15, 2014 at 03:09 PM · prefabrpgeventslistenerinteractable

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 15, 2014 at 03:20 PM 0
Share

Also you could check out this Unity Gems article on events. http://unitygems.com/getcomponent-vs-event/

avatar image Nilk_Ritcorfe · Apr 20, 2014 at 02:45 AM 0
Share

Thanks for the help!

avatar image robertbu · Apr 20, 2014 at 02:49 AM 0
Share

@Nilk_Ritcorfe - If you question is answered, please click on the checkmark next to the answer to close it out. Thanks.

avatar image Nilk_Ritcorfe · Apr 20, 2014 at 06:23 AM 0
Share

@robertbu - I would still like to know other people's opinions on this matter, is that all right?

avatar image robertbu · Apr 20, 2014 at 06:31 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges