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;
},
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?
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.
@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.
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!