SOLVED Objects somehow sharing local variables?

nayru55

Member
Hey! So I'm returning to Game Maker after a long enough break that I might as well be new. I'm working on a dialogue system where different lines of text are assigned in an array called "activeText". Pressing enter proceeds through the array, and then it closes when you get to the last blank line. It works without issue.

My problem is when I have two NPCs who share a parent. They both mostly have the same code, which I assumed would be fine since I was under the impression local variables wouldn't leave objects until otherwise specified. However when I talk to one NPC, and then talk to another, it seems as if the second NPC is using the activeText array value from the first NPC. Talking to the second NPC gives me an error, claiming that "activeText" is higher than it's index. This is really confusing to me because I assumed each NPC would have their own "activeText" amounts that started at 0 and raised independently, because they are local variables.

I've tried fiddling with assigning them to different variables and I've tried unparenting them, but it doesn't seem to matter. Raising the activeText array of the first NPC seems to raise the activeText of the second NPC as well. I think I could probably just reset the value to 0 at the end of things, but that wouldn't work very well for adding more text later. I've reread the document pages on variables and parents/children to try and see if I'm just not understanding something, but I'm at a total loss here.

Here's the relevant code, and the error:

oPlayer Step event:

GML:
if keyboard_check(vk_space)
{
    if (distance_to_object(pNPC) < 5)
    {
        var localNPC = instance_nearest(x,y,pNPC)
        localNPC.talk = true

    }
}

oMisa Create Event
GML:
talk = false

activeText[0] = "Hello, welcome to your farm!"
activeText[1] = "It is lovely to meet you."
activeText[2] = "My name is Misa, your neighbour!"
activeText[3] = "I see you have lots of farmable land."
activeText[4] = "Please take these seeds!"
activeText[5] = "I look forward to seeing your farm grow!"
activeText[6] = ""

textCheck = 0

oMisa Step event:
Code:
if (textCheck = (array_length(activeText) - 1))
            {talk = false}

if (keyboard_check_pressed(vk_enter))
            {textCheck += 1}

if textCheck > 5
{
    talk = false
    textCheck = 0
    exit
}

oMisa Draw GUI event:
Code:
var camera_x = view_xport[0] + (view_wport[0] /2 )
var camera_y = view_yport[0] + (view_hport[0] /2 )

if talk = true
{
    draw_sprite_ext(sTextBox,0,camera_x,(camera_y (camera_y/1.5)),2,2,0,c_white,1)
    draw_sprite(sMisaPortrait,0,(camera_x-(camera_x/3.5)),(camera_y+(camera_y/7.5)))
    draw_text_transformed(camera_x-(camera_x/2.5),(camera_y+(camera_y/2.6)),"Misa",2,2,0)
    draw_text_ext_transformed(camera_x-(camera_x/1.6),(camera_y+(camera_y/1.8)),activeText[textCheck],20,250,2,2,0)
}

oMerchant Create event:
GML:
talk = false

activeText[0] = "Hello, good sir!"
activeText[1] = "Interested in purchasing some seeds?"
activeText[2] = "I like you kid. I'll cut you a deal."
activeText[3] = "Just for you, I'll sell them cheap."
activeText[4] = "20 seeds for $15."
activeText[5] = "Do we have a deal?"
activeText[6] = ""

textCheck = 0

oMerchant Step Event:
Code:
if (textCheck = (array_length(activeText) - 1))
            {talk = false}

if (keyboard_check_pressed(vk_enter))
    {textCheck += 1}


oMerchant Draw GUI event

Code:
if talk == true
{
    if textCheck < 5
    {

        draw_sprite_ext(sTextBox,0,camera_x,(camera_y+(camera_y/1.5)),2,2,0,c_white,1)
        draw_text_transformed(camera_x-(camera_x/2.5),(camera_y+(camera_y/2.6)),"Merchant",2,2,0)
        draw_text_ext_transformed(camera_x-(camera_x/1.6),(camera_y+(camera_y/1.8)),activeText[textCheck],20,250,2,2,0)
    }
}
First time posting so apologies if I did anything wrong or shared too little/too much info! Happy to fix whatever I need to make the post appropriate
 

Nidoking

Member
This has nothing to do with variable sharing or scope. You're incrementing the textCheck variable for every pNPC instance every time you press Enter. The oMisa has a bounds check that resets textCheck if it would go past the end of the array. The oMerchant doesn't. Clearly, you've realized somewhere along the way that something was going wrong with how you handle the variable, but you put a bandage over the problem instead of correcting it. A better fix would be to increment the variable only for the instance you're talking to. Ideally, the player instance would store that information and perform the update to the NPC rather than every NPC tracking that separately.
 

nayru55

Member
This has nothing to do with variable sharing or scope. You're incrementing the textCheck variable for every pNPC instance every time you press Enter. The oMisa has a bounds check that resets textCheck if it would go past the end of the array. The oMerchant doesn't. Clearly, you've realized somewhere along the way that something was going wrong with how you handle the variable, but you put a bandage over the problem instead of correcting it. A better fix would be to increment the variable only for the instance you're talking to. Ideally, the player instance would store that information and perform the update to the NPC rather than every NPC tracking that separately.
This makes sense, I'm not sure why I didn't realize I was raising them both with every press. And I think you're right, having it done on the player's end would probably be better.

Thank you for the time, Nidoking!
 
Top