Need help finding problem in my code

P

Phearcet

Guest
Hi, I'm trying to make a very simple game in which you pick up an object and deliver it to a NPC.
My plan is to set a variable which is set to a specific number upon colliding with the pickup. Then when the player touches the NPC it checks for that number and gives a score if it matches. However, my game keeps crashing whenever I touch the object. Is there anything wrong with my code?

Here's my code for the NPC:
Code:
if (Pickup = 01)
{
    Score = Score + 1;
    Pickup = 00;
    instance_destroy(oNPC01);
}

And the code of my pickup:

Code:
if (Pickup = 00)
{
    Pickup = 01;
    instance_destroy(oPickup01);
}

And here's the crash report
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step EventoPlayer
for object oPickup01:
Variable oPickup01.Pickup(100006, -2147483648) not set before reading it.
at gml_Object_oPickup01_Collision_d722c0b6_a7bd_4053_9bdb_5179db91bb12 (line 1) - if (Pickup = 00)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_oPickup01_Collision_d722c0b6_a7bd_4053_9bdb_5179db91bb12 (line 1)
 

Phil Strahl

Member
Seems like you didn't initialize the variable Pickup in the Create event.
Code:
// create event
Pickup = 0;
Also it's good practice to use double equals signs in the if condition. Just having one works fine with GameMaker because it knows what you intend to do but when you switch to other programming languages "pickup = 0" and "pickup == 0" are two different things:

The first one returns "true" when it was possible to assign the value of 0 to the variable pickup (which is pretty much always the case); the latter one is what you want, checking if the value held in "pickup" is equal to the value on the right.
 

samspade

Member
This line: "Variable oPickup01.Pickup(100006, -2147483648) not set before reading it." Tells you why your game crashed. In this case it is because oPickup01 doesn't have the variable Pickup set when it is referenced.

So, this part of your code:

Code:
if (Pickup = 00) 
{
    ....
}
Is likely the probable as it references Pickup, but I don't see Pickup set anywhere before that.

Also, don't use 00 and 01. Those aren't numbers though it seems like GM will accept them. Use 0 and 1 if that is what you mean. If you want them to be strings, then use quotes.
 
Also, don't use 00 and 01. Those aren't numbers though it seems like GM will accept them. Use 0 and 1 if that is what you mean. If you want them to be strings, then use quotes.
Theres nothing wrong with writing the numbers this way if it improves readability. In reality, as far as the parser is concerned, it's no different the adding extra zeros to pass out decimal places.
 
Top