Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by unity_sZSOKfh_MmAJww · Mar 26, 2020 at 04:25 PM · scripting problemscripting beginnerclassperformance optimizationstruct

Is putting a struct within a class going to affect performance?

I am trying to create a simple dialogue system and right now, I've come up with a this script that uses a struct within a class inside my main script. (If that make sense?) I'm using the class to create dialogue for the NPC and the struct is for my players responses (since I want them to have choices). I'm sorry if I can't be more concise, I'm a beginner but I want my code to be optimized and performant :). Below is the script:

public class DialogueController : MonoBehaviour {

 [System.Serializable]
 public class Dialogue
 {
     public int tag;
     public string message;

     [System.Serializable]
     public struct Response
     {
         public int tag;
         public string message;
     }

     public List <Response> responses = new List <Response> ();
 }

 public List <Dialogue> NPCDialogue = new List<Dialogue> ();

 public int currentDialogue = 0;

},

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

Answer by streeetwalker · Mar 26, 2020 at 05:37 PM

@unity_sZSOKfh_MmAJww , They can - it depends how you use them. Structs are passed by value. You need keep that in mind when creating references to your struct objects: you are making a copy. You could end up getting a lot of copies, and they are stored on the stack so memory use is a consideration.

Generally, structs should be for small amounts of data. In your case, to use in a list is fine. It's going to be more manageable than having to deal with multiple lists.

Often you have to balance performance over code manageability. In your example, the only other choices are to create two lists - one for the tag and one for the message; or to create a class, and your example doesn't warrant that.

But your example begs the question: is tag simply an int that indexes the message? That is, the 1st message has a tag of 0, the 2nd message tag is 1, and so on? If so, then why use a struct at all? Why not use a simple array or list, of messages, and use the index of the array/list element as the tag?

Comment
Add comment · Show 3 · 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 unity_sZSOKfh_MmAJww · Mar 27, 2020 at 04:00 AM 0
Share

Thanks for that! I didn't use a list/array since I also needed a tag variable to deter$$anonymous$$e where the choice would lead to (kind of like in a simple flow chart or behaviour tree where 2 choices can lead to different things or only lead to one).

I was wondering if this would lead to performance issues down the road, especially since almost all NPCs in the game will have this script.

avatar image streeetwalker unity_sZSOKfh_MmAJww · Mar 27, 2020 at 04:58 AM 0
Share

@unity_sZSOKfh_$$anonymous$$mAJww , If each each NPC will have a different list, what choice do you have? You'll need some kind of data structure for each. - your struct is an example. The struct itself isn't going to be much less efficient than maintaining separate lists.

How are you doing to effect a tree functionality with only one index variable? $$anonymous$$aybe it's too early in the morning for me, but it seems like you are going to need a more complex structure.

However that is trivial. I think a more important issue will be how to manage all such lists if you have many NPCs. It will be more efficient performance wise and easier to maintain if you create a Dialog $$anonymous$$anager class and that contains the data structure definition and list management functions, and have your NPC create an instance of that. In that case you may not need an actual struct, but can embed the struct variables as members of that class.

avatar image unity_sZSOKfh_MmAJww streeetwalker · Mar 28, 2020 at 02:58 PM 0
Share

Ill try to implement that and see what works for me. I'm still trying to wrap my head around all this. Thank you for the help!

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

306 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

(C#) How do I "Do Things" after loading the scene in Unity 2018.2.20??? 1 Answer

scripting tutorial is not updated...? 0 Answers

UnityCar - Controlling Speed with a duration? 2 Answers

Play animation grow once, then play animation rotate on continuous loop. 0 Answers

Having trouble moving a GameObject to another scene and Transform it to a different position 0 Answers


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