Help with class instance that contains array of string

1

Hi, I have a class with an array as a variable:

    public string history;
    public string[] answers;
    public StoryNode[] nextNode;
    public bool isFinal = false;

I want to create a reference in another class, as I must fill in the array, I've done something like that but it gives me an error:

  GameplayManager.StoryNode historia_inicial = new GameplayManager.StoryNode("Historia 1", {"Answer1", "Answer2"}, new StoryNode["1", "2"], false);

How to fill an array when instantiating another class? Thanks

    
asked by Miquel Vidal Portillo 09.10.2017 в 14:27
source

1 answer

1

If you have a class like it is

public class StoryNode 
{
    public StoryNode()
    {
        this.isFinal = false;
    }

    public string history {get;set;}
    public string[] answers {get;set;}
    public StoryNode[] nextNode {get;set;}
    public bool isFinal {get;set;}
}

then you can assign the proiedades

StoryNode historia_inicial = new StoryNode()
{
    history = "Historia 1",
    answers = new string[] {"Answer1", "Answer2"},
    nextNode = new StoryNode[] { new StoryNode(), new StoryNode()}
}

in this way when instantiating you assign the values

Use propeeds in the class and not public variables

You will see that the array of string instances and the assignment define the number of elements.

Now the nextNode should be seen as you define the constructor, because I do not see that assigning a number works, deebs create the instances using new

    
answered by 09.10.2017 в 16:58