with(id) returning an "object does not exist" error

ome6a1717

Member
Something incredibly strange is happening and I, for the life of me, can't figure out why.

Long story short, I'm checking an enemy hitbox against the player and using the following code:

ENEMY_HITBOX STEP EVENT

GML:
var phit = instance_place(x,y,o_player);
var blocked = false;
var parried = false;

if phit != noone && !blocked && !parried {
    with(phit){
        hit_player(other.dmg);
    }
}
This code works great. If I leave the room, and then re-enter and get hit again, I get the following code error:

Variable <unknown_object>.hit_player(105728,-214748648) not set before reading it.

However, if I do a "show_message(phit OR id);" right under the with(phit), it shows the id of my player....

Any ideas?
 
Last edited:

Roldy

Member
Post the actual error message, not a paraphrased version of it, including line number, event etc...
 

ome6a1717

Member
It looks like the issue is with hit_player. Where and how is that function declared?
That's what I thought, too. So I copied the entire function as code into the with(phit), and then it just gave an error on a different function in that strip of code.

Right now it's declared in the o_player STEP EVENT, but I also tried moving it to a script object and still came up with the same error.
 

Roldy

Member
It's declared in the create event set to 0, and is then added to upon a with(instance_create_layer){ dmg = <number>; }
Whose create event? It is declared in the create event of o_e_hitbox?
Before calling hit_player do the same show_message on other and dmg.

GML:
with (phit) {
    show_message(phit);
    show_message(other);
    show_message(other.dmg);
    show_message(self);
    // etc...
}
 

ome6a1717

Member
It is declared in the create event of o_e_hitbox?
Before calling hit_player do the same show_message on other and dmg.

GML:
with (phit) {
    show_message(phit);
    show_message(other);
    show_message(other.dmg);
    show_message(self);
    // etc...
}
phit = the id of the player (100118)
other = what looks like a struct of the enemy hit box variables { stun, dmg, myenemy, stagger, etc. }
other.dmg = 30
self = what looks like a struct of the o_player variables
 

Roldy

Member
So if you get rid of the with and just call hit_player what happens:
GML:
phit.hit_player(dmg);
hit_player(dmg);
EDIT: I gotta run.. good luck
 

ome6a1717

Member
So if you get rid of the with and just call hit_player what happens:
GML:
phit.hit_player(dmg);
hit_player(dmg);
Same thing - works on the first time I load the room, and then once I go to a different room and come back, same exact error.
 

samspade

Member
That's what I thought, too. So I copied the entire function as code into the with(phit), and then it just gave an error on a different function in that strip of code.

Right now it's declared in the o_player STEP EVENT, but I also tried moving it to a script object and still came up with the same error.
Sorry, I meant can you post the code for this function in full and where it is located? It looks very much like an error caused by improper script usage in 2.3.
 

samspade

Member
I'm not actually sure if that is allowed (it seems like you'd be overwriting the variable with a new function every step, but maybe GM doesn't do that, I've never actually tested it)? I would try moving into either the create event or a script asset.

Also, why is it indented so far? Is it a part of some other code block?
 

ome6a1717

Member
I'm not actually sure if that is allowed (it seems like you'd be overwriting the variable with a new function every step, but maybe GM doesn't do that, I've never actually tested it)? I would try moving into either the create event or a script asset.

Also, why is it indented so far? Is it a part of some other code block?
It's part of a region - I like to keep my code very visual if possible so I can figure out what I'm looking at :)

Is it not? You might be right - I wonder if I should just put it in the create step instead...regardless as stated in a previous reply, I've tried deleting it from there and moving it into a script object and that seemingly did the same thing.

EDIT - oh that indent; I used to have the function use a with(id) command and then decided to get rid of it and just use the with in the object collision event instead.
 

Roldy

Member
It's part of a region - I like to keep my code very visual if possible so I can figure out what I'm looking at :)

Is it not? You might be right - I wonder if I should just put it in the create step instead...regardless as stated in a previous reply, I've tried deleting it from there and moving it into a script object and that seemingly did the same thing.

EDIT - oh that indent; I used to have the function use a with(id) command and then decided to get rid of it and just use the with in the object collision event instead.
If you inspect you instance (phit) it should list all its functions... is hit_player defined when you load the room.

As @samspade is stating the error is 'hit_player' is not defined for that instance. Double check hit_player is getting defined for every instance of o_player before o_e_hitbox tries to call it. Make sure hit_player function declaration is not wrapped conditionally (that indent). Make sure hit_player has a chance to be defined before o_e_hitbox step event call hit_player.
 

ome6a1717

Member
If you inspect you instance (phit) it should list all its functions... is hit_player defined when you load the room.

As @samspade is stating the error is 'hit_player' is not defined for that instance. Double check hit_player is getting defined for every instance of o_player before o_e_hitbox tries to call it. Make sure hit_player function declaration is not wrapped conditionally (that indent). Make sure hit_player has a chance to be defined before o_e_hitbox step event call hit_player.
Inspecting it shows the correct id number for both phit and the o_player instance, but I can't find where to see if the function is loaded. Is it in the debugger somewhere?
 

ome6a1717

Member
I'm not actually sure if that is allowed (it seems like you'd be overwriting the variable with a new function every step, but maybe GM doesn't do that, I've never actually tested it)? I would try moving into either the create event or a script asset.

Also, why is it indented so far? Is it a part of some other code block?
I think you're right, actually. You cannot put a function in a step event on an object (you can, but things like this will happen..). It turns out there were other functions in the step event that the hit_player was using, and copying and pasting ALL functions from the player object seemed to have solved the issue.

Thank you @samspade and @Roldy for all your help!! I really appreciate it - to the script object it goes :)
 
Last edited:

Roldy

Member
Inspecting it shows the correct id number for both phit and the o_player instance, but I can't find where to see if the function is loaded. Is it in the debugger somewhere?
Yeah the debugger is a good place to look. If you break the debugger has a tab that shows all instances and its variables/functions.

Glad you figured it out.
 

ome6a1717

Member
Yeah the debugger is a good place to look. If you break the debugger has a tab that shows all instances and its variables/functions.

Glad you figured it out.
Yeah the debugger is intimidating at first glance, especially when I'm still wrapping my head around GML in general. Putting it in the step event made GMLive easier to utilize :)
 
Top