• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Incorrect String Is Displayed If Objects Are Collected Out of Order

I

IcyPenguin_

Guest
Hey all. I'm testing out some systems for a new project, and I've got an object drawing out some stats so I can see whether or not the system itself is working. I've got a parent object called prt_powerup, and in its create event, it establishes the name of the powerup so that it can be added to the inventory.

In the inventory, I have a for loop in the step event that states the following. It is triggered when the player collides with a powerup using the place_meeting function:
Code:
var i;
for(i = 0; i < array_length_1d(slot)-1; i++)
{
    if slot[i] == "" //If there is an empty slot...
    {
        slot[i] = prt_powerup.color_name; //...then make the slot carry that color.
        i = array_length_1d(slot)-1; //Ends the for loop if an empty slot is found
    }
}
Just in case something is wrong with the draw event, here's the code for that.
Code:
draw_self();

draw_set_font(fnt_test);
draw_set_color(c_black);

draw_text(prt_player.x,prt_player.y-13,"Contents of Slot 0: "+slot[0]);
draw_text(prt_player.x,prt_player.y,"Contents of Slot 1: "+slot[1]);
draw_text(prt_player.x,prt_player.y-26,"Selected Slot: "+string(selected_slot));
When I pick up blue before red, the code works. If I pick up red before blue, both slot contents from the draw event state "blue". Is there something different that I could do to fix this? Am I missing something here?
 
H

Homunculus

Guest
You probably need the color of the specific powerup instance you are colliding with, but instead you are using the object name ( prt_powerup.color_name ). When accessing a variable using the object name, you get the color of the first instance, which may or may not be the one you are colliding with.

You may want to use instance_place instead of place_meeting, since the former returns the instance id that can later be used to retrieve the color.
 
I

IcyPenguin_

Guest
I thought I did. Maybe I don't? An instance is just one particular occurrence of an object, correct?
 
I

IcyPenguin_

Guest
Figured it out. I had completely forgotten that instance_nearest() exists, so I used instance_nearest(prt_player.x,prt_player.y,prt_powerup).color_name instead of prt_powerup.color_name, which ended up working.
 

FrostyCat

Redemption Seeker
I thought I did. Maybe I don't? An instance is just one particular occurrence of an object, correct?
That's exactly the difference. So why are you still trying to refer to prt_powerup.colour_name, when you clearly know there are two potential instances of prt_powerup descendants that this could refer to?
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
My article also describes quite clearly what you should have done instead:
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ all return instance IDs. Save that instance ID into a variable (e.g. var inst_enemy = instance_place(x, y, obj_enemy);), then use that as the subject to work with (e.g. inst_enemy.hp -= 10;). Note that these functions return noone upon not finding a collision. Always account for this special case whenever you handle collisions this way.
And no, instance_nearest() is not a genuine solution. It just shows you don't have a clue about collision functions.
 
Top