- Home /
Use Of Static Variables
Hi guys,
I'm making a game with some dialogue between the player and ai. I want the player and AI to be able to choose their messages and responses from a pool of messages which are available to both.
I initially created a Message class
[System.Serializable]
public class Message
{
public string sentence;
public Message response = null;
public enum Category {Greeting, Question, Compliment, ZeroWing};
public Category category;
public Message(string s, Category c)
{
sentence = s;
category = c;
}
public Message(string s, Category c, Message r)
{
sentence = s;
category = c;
response = r;
}
}
the idea being that certain messages would have a specific response (for example a greeting or asking someone the time of day). (although I will probably make this a list or array later so that there can be greater diversity in the conversation).
Then I wanted to store all of the possible Messages in an external place so that I can simply add them to the characters.
[System.Serializable]
public class AllMessages
{
public List<Message> messages = new List<Message>()
{
new Message("What happen?", Message.Category.ZeroWing),
new Message("Somone set up us the bomb.", Message.Category.ZeroWing),
new Message("We get signal", Message.Category.ZeroWing),
...
};
}
**Person Class**
public AllMessages allMessages;
I then wanted to add specific responses to certain Messages which ended up looking like this:
new Message("What happen?", Message.Category.ZeroWing, new Message("Somone set up us the bomb.", Message.Category.ZeroWing)),
which is starting to get a bit heavy because I'm not storing them as a variable. So I figured I need to create specific Messages and then then add them all to the list like so:
[System.Serializable]
public class AllMessages
{
Message whatHappen = new Message ("What happen?", Message.Category.ZeroWing, setUpUsBomb);
Message setUpUsBomb = new Message ("Someone set up us the bomb.", Message.Category.ZeroWing);
Message signal = new Message("We get signal", Message.Category.ZeroWing);
...
public List<Message> messages = new List<Message>()
{
zeroHappen,
setUpUsBomb,
...
};
}
This way I can create more dynamic conversation by allowing Message A to be responded to by B and C and to have responses D and E whilst C has responses E and F etc.
However it's giving me the error "a field initialiser cannot access the non-static field, method or property of allMessages.setUpUsBomb".
So my question is: How should I be creating the instances of Messages so that I can create these relationships between them and still add specific groups of them to a character (maybe based on category or something)? Also should I declare them somewhere else? Ideally I would like to have a seperate c# file which just contains all of the messages and their relationships but I need to place them inside a class right? So should I be putting them at the bottom of the Message Class? or in a different class?
Sorry if I've not been very clear about what my problem is and I know this is a long question, I'm just not sure how I'm actually able to tackle this issue. Thanks.
Answer by rob5300 · Feb 11, 2016 at 12:24 PM
Your message class can have static members, i would suggest using the system you have already with using a List.
I would alter your Message class to be like this. Note the changes to the constructors aswell as the addition of the List:
You may want to consider using a Dictionary instead of a List if you will be retrieving these messages by name frequently
[System.Serializable]
public class Message
{
//New Line Below
public static List<Message> messages = new List<Message>();
//Or
//public static Dictionary<string,Message> messages = new Dictionary<string,Message>();
public string sentence;
public Message response = null;
public enum Category {Greeting, Question, Compliment, ZeroWing};
public Category category;
public Message(string s, Category c)
{
sentence = s;
category = c;
//This should add the newly made message to the List automatically, can be changed easily to work for Dictionaries.
List.Add(this);
}
public Message(string s, Category c, Message r)
{
sentence = s;
category = c;
response = r;
//This should add the newly made message to the List automatically, can be changed easily to work for Dictionaries.
List.Add(this);
}
}
You may want to consider using a static constructor instead of what i added to the Constructors. Hope this can help you out. Its good to know that you can mix static and non static members in the same class.
Please tell me if this doesn't work, haven't use c# for a little while!
this seems really useful :) thank you for your reply. I'll look into it after work today.
just a quick question. When I construct the $$anonymous$$essages is there a specific place I should do that? Does it need to be in the Start method of something? Also can I construct them without putting it in a variable since i'm going to be storing it in a Dictionary ? i.e.
$$anonymous$$essage(<string>,<category>);
//ins$$anonymous$$d of
$$anonymous$$essage m = new $$anonymous$$essage(<string>,<category>);