Search code examples
game-enginegame-makergame-maker-studio-2

Game Maker Studio 2 Array taking wrong values


Hey guys I'm new to Game Maker Studio and new to the language. I'm making a game and have been working on the dialogue system.

This chunk of code was designed for characters respond to a set of choices, the dialogue starts by printing out the first element of the line_array, which it does, then give the player the choice of two responses from the response_array, which it insteads prints out the second element of the line_array and I don't understand why.

Does an argument only hold one element of an array? I'm initializing two arrays in an object oCivilian2 and pushing them through code DialogueCode which is linked to another object oRespond that supposed to allow me to sift through dialogue in game. Anything helps thanks

It's initialized here in create of oCivilian2

line_array = [3];
line_array[0] = "Ethan it's good to see you! \n I thought after the incident well.... \n well I thought we had lost you";
line_array[1] = "I've said too much";
line_array[2] = "You hit your head trying to saver her\n It was horrible";
response_array = [2];
response_array[0] = "What happened?";
response_array[1] = "I don't recall alot. How bad was it?";
counter = 0;
x1 = RESOLUTION_W / 2;
y1 = RESOLUTION_H -70;
x2 = RESOLUTION_W/2;
y2 = RESOLUTION_H;
_print = "";
responseSelected = 0;

Then the step which links it to DialogueCode when spacebar is pressed

keyActivate = keyboard_check_pressed(vk_space);
if (keyActivate) 
{
    var inst = collision_rectangle(oPlayer.x+3,oPlayer.y+3,oPlayer.x-3,oPlayer.y-3, oCivilian2, false, false);
    if (inst != noone) 
    {
        ScriptExecuteArray(DialogueCode, line_array);
        ScriptExecuteArray(DialogueCode, response_array);
    }
}

Then through to step in the object oRespond

lerpProgress += (1 - lerpProgress) / 50;
textProgress += global.textSpeed;

x1 = lerp(x1, x1Target,lerpProgress);
x2 = lerp(x2, x2Target,lerpProgress);


keyUp = (keyboard_check_pressed(vk_up)) || (keyboard_check_pressed(ord("W")))
keyDown = keyboard_check_pressed(vk_down) || keyboard_check_pressed(ord("S"));
responseSelected += (keyDown - keyUp);
var _max = 2;
var _min = 0;
if (responseSelected > _max) responseSelected = _min;
if (responseSelected < _min) responseSelected = _max;

for (var i = 0; i < 2; i++) 
{
    var _marker = string_pos(",", response);
    if (string_pos(",",response)) 
    {
        responseScript[i] = string_copy(response,0,_marker);
        string_delete(response,0,_marker);
        var _marker = string_pos(",", response);
    }
    else 
    {
        responseScript[i] = string_copy(response,0, string_length(response));
    }
}
if (keyboard_check_pressed(vk_space))
{
    counter++;
}

Then to print in oRespond

/// text
//response 
NineSliceBoxStretched(sTextBox, x1,y1,x2,y2, 0);
draw_set_font(fText);
draw_set_halign(fa_center);
draw_set_valign(fa_top);
draw_set_color(c_black);
if (counter % 2 == 0) 
{
    var _i = 0;
    var _print = string_copy(text,1,textProgress);
    
    draw_text((x1+x2) / 2, y1 + 8, _print);
    draw_set_color(c_white);
    draw_text((x1+x2) / 2, y1 + 7, _print);
    _i++;
}
else 
{
    if (array_length_1d(responseScript) > 0) 
    {
        var _print = "";
        for (var t = 0; t < array_length_1d(responseScript); t++)
        {
            _print += "\n";
            if (t == responseSelected) _print += "--> "
            _print += responseScript[t];
            show_debug_message(responseScript[t]);
            if (t == responseSelected) _print += " <-- "
        }
        draw_text((x1+x2) / 2, y1 + 8, _print);
        draw_set_color(c_white);
        draw_text((x1+x2) / 2, y1 + 7, _print);
    }
}

Solution

  • Alright, i think to see many problems with your code.

    First of all, since arrays in GM are dynamic declare them like

    line_array[3]

    is a bad practice (in my point of view)

    I've never declared an array this way in GM so that could be the problem here.

    Second, i don't really understand the logic of your code, always create objects, at least in the GM environment, that corresponds to "physical" entities, i would make an object for the Civilian but not for the "respond".

    I've red your code a lot of times and since no one answered you in 3 months i can assume it's because no one can really understand your way of coding, and this way of coding will probably give you a lot of problems in future. The thing that you're trying to doing could be super-easy if done with a good hierarchy.

    I would like to help u with this code, but i find it very chaotic.

    If you've not resolved this problems, write a comment :)

    I advice you to fully re-implement it even if resolved anyway.