GMS 2.3+ Problem to manage branching dialogue

Hello everyone,

I have a little problem to manage my dialogue system when i have multiples question and multiples answers. I created an array to store the text

array [interlocutor with who you speak] [line text] [here i store if i just write a sentence or a sentence + answers, "answer 1","answer 2"....]

I give you an exemple :
GML:
array[John][0]=[0,"my name is John"]//line text 0 // 0 is because it's just a sentence 
array[John][1]=[3,"And you, what is your name?","Answer 1","Answer 2","Answer 3"]//line text 1//3 is because i have 3 answers

//if Answer 1
array[John][101]=[0, "blablabla"]//if answer 1 i add 100 to line text

//if Answer 2
array[John][201]=[0,"answer something"]//if answer 2 i add 200 to line text


For exemple line text = 1 ---->choice 1 ---->line text = 101 ----> choice 1 --->line text = ???
----> choice 2 --->line text = ???


line text = 1 ---->choice 2 ---->line text = 201 ----> choice 1 --->line text = ???
----> choice 2 --->line text = ???


Can you help me?
 

angelwire

Member
You won't want to use arrays. Gamemaker 2.3s structs and constructors will probably be a much better bet. If you don't know how to use them, do some reading on the manual and find some youtube tutorials, because they will make programming much easier.
Here's how you could set up structs and constructors:
-Have a "Statement" struct that contains the following
  • A string (what the character is saying)
  • An array of answers
  • An array of Statements that the character gives for each answer the player chooses
  • A "next" statement for when the character says something without wanting a reply
Here's some code to hopefully get you started:
GML:
//Constructor to hold statements and replies
function Statement(_string) constructor
{
    my_text = _string;
    my_answers = [];
    my_answer_statements = [];
    my_next_statement = undefined;
    
    function add_answer(_reply, _statement)
    {
        my_answers[array_length(my_answers)] = _reply;
        my_answer_statement[array_length(my_answer_statement)] = _statement;
    }
}
To use this method, you can use the constructor to create statements and replies like this:
GML:
introduction = new Statement("Hello, my name is John");
rude_reply = new Statement("That's rude");
kind_reply = new Statement("Thank you so much! You're so kind!");
neutral_reply = new Statement("Nice to meet you too");
farewell = new Statement("Goodbye!");

introduction.add_answer("That's a stupid name", rude_reply);
introduction.add_answer("That's such a nice name!", kind_reply);
introduction.add_answer("Nice to meet you, John", neutral_reply);

rude_reply.my_next_statement = farewell;
neutral_reply.my_next_statement = farewell;
kind_reply.my_next_statement = farewell;

//"That's a stupid name" -> "That's rude" -> "Goodbye!"
//"That's such a nice name" -> "Thank you so much! You're so kind!" -> "Goodbye!"
//"Nice to meet you, John" -> "Nice to meet you too" -> "Goodbye!"
 
Top