A question about event_inherited

Status
Not open for further replies.
I have an object named PoisonObject. In it is a create event. In the create event is an instance that
reads attackTarget = noone. I have a piece of code calls upon the attackTarget in an event
BatParentObject. Note the if attackTarget == other.id. This is the error I'm getting.

if (mouse_check_button_released(mb_left))
{

//inst = instance_position(mouse_x, mouse_y, all)
inst = instance_position(mouse_x, mouse_y, BrainParentObject)
attackTarget = inst;

if distance_to_object(attackTarget)<attackRange
{
if (inst.object_index != BrainParentObject && inst.object_index != BatParentObject)
{


//end battle
attacking = false;
speed = 0;
path_speed = 0;
image_index = 0;

// move_towards_point(mouse_x, mouse_y, flyspeed);
with attackTarget
if attackTarget == other.id
decision_timer = room_speed+(image_number-image_index)*sprite_get_speed(sprite_index);
}
if(point_distance(x, y, inst.x, inst.y))
{
//sprite_index = global.party_walk;
sprite_index =global.party_still;
}
}

}
What happens, functional-wise, is the Forest Brain Object (of which it is a parent of BrainParentObject of) - when it collides the brains one of the two die and throw the error
above.
 

Nidoking

Member
That's not an error message. That's a bunch of code.

However, I think I see the issue. You've got with attackTarget and then you're checking attackTarget again. That's checking the attackTarget value of attackTarget. Is that what you wanted?
 
Last edited:

Nidoking

Member
Well, you probably intended for SOME of those lines to be in a with. But when you're in a with, you're executing code from the perspective of the thing in the with, not the thing you started in. If attackTarget is a variable in the calling object, then you'd have to use other.attackTarget within the with to get to it. If you don't understand that much, then there are probably other places where you're mixing objects that way.
 
We could really do with seeing this error that you are talking about. Also, what does this have to do with event_inherited() when you are not using that anywhere in your code?
You are also not checking that you have actually got a value in inst and attackTarget before you start using them anywhere. You need to check what is in them before you start refering to it as if either is a valid instance:
if inst != noone and/or if attackTarget != noone is probably what you should be doing.
 
Thought I pasted the error. Anyway here it is:

GML:
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object BatParentObject:

Variable ForestSpecialtyBrain.attackTarget(100050, -2147483648) not set before reading it.
 at gml_Object_BatParentObject_Step_0 (line 63) -                             if attackTarget == other.id
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_BatParentObject_Step_0 (line 63)
called from - gml_Object_SnowBatObject_Step_0 (line 46) - event_inherited();
The event_inherited i reason is SnowBatObject, Step Event. The relevant code is:

Code:
/////////////STRENGTH//////////////

global.bat_strength = strength;
/////////////////DEFENSE//////////////
global.bat_defense = defense;

//////////////////ATTACK/////////////////

global.bat_attack = attack;

//////////////////////DEXTERITY/////////////
//ability to use a projectile

global.bat_dexterity = dexterity;

/////////////////SPEED ///////////

//unaccelerated spaed

global.bat_spd =  spd;

///////////////INTELLIGENCE////////////////
//strength of projectiles

global.bat_intelligence = intelligence;

///////////EXPERIEINCE///////////////

//rolling over experience every time kill something with this bat type

global.bat_experience = experience;

///////////////LEVEL///////////////////////////

//experience adds up to a new level for that bat
//this increases stats for that bat type

global.bat_experience_level = experience_level;


global.party_kill = SnowBatAttack
global.party_still = SnowBatStill;
global.party_walk = SnowBatWalk;
event_inherited();
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
let's take a look at the error:
Code:
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0                       // <= the event
for object BatParentObject:   // <= the object

Variable ForestSpecialtyBrain.attackTarget(100050, -2147483648) not set before reading it.   // <= problem attackTarget does not exist
at gml_Object_BatParentObject_Step_0 (line 63) -                             if attackTarget == other.id
############################################################################################
you have an error in the BatParentObject... during the step event!
so you are trying to access the attackTarget variable but it is not set.
So probably you are forgetting to define the attackTarget variable in the START EVENT.
 

Nidoking

Member
The attackTarget it's trying to access is not in BatParentObject. It's in ForestSpecialtyBrain, due to the with. If you want to fix the problem, you really need to understand the problem.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
here is the code it should not throw the error please keep in mind USE PARENTHESIS, INDENT YOUR CODE when asking for help.

Code:
if (mouse_check_button_released(mb_left))
{
   //inst = instance_position(mouse_x, mouse_y, all)
   inst = instance_position(mouse_x, mouse_y, BrainParentObject)
   attackTarget = inst;

   if (distance_to_object(attackTarget) < attackRange)
   {
      if (inst.object_index != BrainParentObject && inst.object_index != BatParentObject)
      {
         //end battle
         attacking = false;
         speed = 0;
         path_speed = 0;
         image_index = 0;

         // move_towards_point(mouse_x, mouse_y, flyspeed);
         // the code you have can be written like this!!!!
         if (attackTarget == id)
         {
            decision_timer = room_speed+(image_number-image_index)*sprite_get_speed(sprite_index);
         }
      }
      if (point_distance(x, y, inst.x, inst.y)) // you are not using this distance at all?! this is the same as doing: if(x != inst.x || y != inst.y)
      {
         //sprite_index = global.party_walk;
         sprite_index =global.party_still;
      }
   }
}
hmmm... though it makes no sense whatsoever.

The attackTarget it's trying to access is not in BatParentObject. It's in ForestSpecialtyBrain, due to the with. If you want to fix the problem, you really need to understand the problem.
I do understand the problem don't need to be so aggressive. if the code was correctly formatted it would be faster to detect the problem.
 
Last edited:
The code right now only fails when I use the SnowBatObject..

This is the error:
GML:
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object SnowBatObject:

Variable SnowBatObject.attackTarget(100050, -2147483648) not set before reading it.
at gml_Object_SnowBatObject_Step_0 (line 209) - if instance_exists(attackTarget)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_SnowBatObject_Step_0 (line 209)
@chamaeleon Could you give me a hand setting up git and the gui for it?
 

TsukaYuriko

☄️
Forum Staff
Moderator
Check where you are declaring attackTarget - is this code actually executing for the instance that spawns this error message? If it's in a parent object and the error is spawned by a child of this parent, ensure that the parent's corresponding event is not overwritten, that it declares the variable under all circumstances and that it declares it before the child attempts to use it.
 

Nidoking

Member
Also, this part:
if (inst.object_index != BrainParentObject && inst.object_index != BatParentObject)
suggests to me that something has gone fundamentally wrong with your design. If you're creating instances of parent objects, then you're almost certainly doing something wrong. If you're not, then this check is meaningless. And if you can't tell whether or not an instance of BrainParentObject is also a child of BatParentObject (you got it from instance_position(mouse_x, mouse_y, BrainParentObject)), then I just don't understand your inheritance structure at all. This is the output of a process that sees a problem, fails to understand the problem, nudges the program until the problem goes away, and awaits further chaos.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
In order for us to try and solve your problem first of all we need,
so information on the problem itself. Follow a template like this.

====
What you are trying to do?!
(place description here)

What is going wrong?
(what is not the way you want)

My code:
(paste code here)

The errors:
(paste error here)
====

BTW @Nocturne @TsukaYuriko is it possible in the new version of the forums to create pre-made post templates?
It would save a lot of time if there was a template like this in the PROGRAMMING sub-forum ;)
 

TheouAegis

Member
First off, the reason for the first posted error message was already mentioned near the beginning of this thread:
You've got with attackTarget and then you're checking attackTarget again. That's checking the attackTarget value of attackTarget.
SnowBatObject has attackTarget defined just fine -- there's no error there -- but because Ted used "with attackTarget", the following reference to attackTarget is looking for a variable attackTarget inside the instance. In this case, the variable attackTarget inside SnowBatObject holds the id for an instance of a ForestSpecialtyBrain. ForestSpecialtyBrain doesn't have an attackTarget variable defined, so the error is thrown.

I'm not sure when Ted changed up his code, since the second code was modified from the first. Maybe his error message was different initially, as well? In his first code, the error was the same as the error in xDGame's
Code:
inst = instance_position(mouse_x, mouse_y, BrainParentObject)
attackTarget = inst;

if distance_to_object(attackTarget)<attackRange
{
if (inst.object_index != BrainParentObject && inst.object_index != BatParentObject)
{
As BabiA pointed out, if inst is noone, then attackTarget is noone and thenceforth all references to attackTarget or inst will throw an "instance not found" error.

Further down Ted's code is the error Nido pointed out:
Code:
with attackTarget
if attackTarget == other.id
Based on the error message Ted posted, SnowBatObject did have a valid id in attackTarget and inst -- it was the id of an instance of a ForestSpecialtyBrain. As Nido said, though, the second reference to attackTarget is trying to read the variable "attackTarget" inside ForestSpecialtyBrain, which apparently wasn't defined. The brain parent should have attackTarget set in its Create event just like the bat parent.

I don't remember the original code, but that line was correct: if the bat's target brain is targeting the same bat, make the two units duke it out. I don't remember what was supposed to happen if one unit was targeting a different unit, but that was the original intent.

So two changes to make:
1) Make sure the brains have an "attackTarget" variable as well.
2) Make sure you check "if (inst)" before setting attackTarget in the step event. If inst is noone, you're not supposed to change attackTarget. Or, if you wanted to clear the attackTarget when not clicking on anything, you instead need to check "if (attackTarget)" before checking the distance to it.
 

Nidoking

Member
As BabiA pointed out, if inst is noone, then attackTarget is noone and thenceforth all references to attackTarget or inst will throw an "instance not found" error.
I was going to say that, but then I checked it out. It turns out that distance_to_object(noone) actually returns 1000000, which is presumably larger than attackRange. While I prefer to check for noone explicitly, this does the job.
 
First off, the reason for the first posted error message was already mentioned near the beginning of this thread:

SnowBatObject has attackTarget defined just fine -- there's no error there -- but because Ted used "with attackTarget", the following reference to attackTarget is looking for a variable attackTarget inside the instance. In this case, the variable attackTarget inside SnowBatObject holds the id for an instance of a ForestSpecialtyBrain. ForestSpecialtyBrain doesn't have an attackTarget variable defined, so the error is thrown.

I'm not sure when Ted changed up his code, since the second code was modified from the first. Maybe his error message was different initially, as well? In his first code, the error was the same as the error in xDGame's
Code:
inst = instance_position(mouse_x, mouse_y, BrainParentObject)
attackTarget = inst;

if distance_to_object(attackTarget)<attackRange
{
if (inst.object_index != BrainParentObject && inst.object_index != BatParentObject)
{
As BabiA pointed out, if inst is noone, then attackTarget is noone and thenceforth all references to attackTarget or inst will throw an "instance not found" error.

Further down Ted's code is the error Nido pointed out:
Code:
with attackTarget
if attackTarget == other.id
Based on the error message Ted posted, SnowBatObject did have a valid id in attackTarget and inst -- it was the id of an instance of a ForestSpecialtyBrain. As Nido said, though, the second reference to attackTarget is trying to read the variable "attackTarget" inside ForestSpecialtyBrain, which apparently wasn't defined. The brain parent should have attackTarget set in its Create event just like the bat parent.

I don't remember the original code, but that line was correct: if the bat's target brain is targeting the same bat, make the two units duke it out. I don't remember what was supposed to happen if one unit was targeting a different unit, but that was the original intent.

So two changes to make:
1) Make sure the brains have an "attackTarget" variable as well.
2) Make sure you check "if (inst)" before setting attackTarget in the step event. If inst is noone, you're not supposed to change attackTarget. Or, if you wanted to clear the attackTarget when not clicking on anything, you instead need to check "if (attackTarget)" before checking the distance to it.
Yay ahead of you. The code was using for the pairing was borrowed, hence the terrible formating. Also it is over a year ago so I have improved a bit. I cleaned up the code, made it legible and I saw how he used the attackTarget, put it in ForestBrainSpecialty and anyway its done.
Also, this part:
if (inst.object_index != BrainParentObject && inst.object_index != BatParentObject)
suggests to me that something has gone fundamentally wrong with your design. If you're creating instances of parent objects, then you're almost certainly doing something wrong. If you're not, then this check is meaningless. And if you can't tell whether or not an instance of BrainParentObject is also a child of BatParentObject (you got it from instance_position(mouse_x, mouse_y, BrainParentObject)), then I just don't understand your inheritance structure at all. This is the output of a process that sees a problem, fails to understand the problem, nudges the program until the problem goes away, and awaits further chaos.
@Nidoking
Also, this part:
if (inst.object_index != BrainParentObject && inst.object_index != BatParentObject)
suggests to me that something has gone fundamentally wrong with your design. If you're creating instances of parent objects, then you're almost certainly doing something wrong. If you're not, then this check is meaningless. And if you can't tell whether or not an instance of BrainParentObject is also a child of BatParentObject (you got it from instance_position(mouse_x, mouse_y, BrainParentObject)), then I just don't understand your inheritance structure at all. This is the output of a process that sees a problem, fails to understand the problem, nudges the program until the problem goes away, and awaits further chaos
I wouldn't think so. I'm using inheritance to group like children. Consider it like this. This is a case of polymorphism.
Subtyping (also called subtype polymorphism or inclusion polymorphism): when a name denotes instances of many different classes related by some common superclass.[3]
Polymorphism

@TheouAegis
DIdn't get the snowbat problem fixed yet,. He doesn't throw an error but they get locked up with specialty brains. And only specialty brains. I set up a parent-child relationship for my Frankenstein and it works perfectly. As you know I set up a bat parent child hierarchy and that works. Now the specialty brains are separate from the regular brains. So I tried to set up the same relationship bat-specialtybrain, and as I described earlier the snowbat locks up with the forest specialty brain. Turns out for every special brain they lock up ( don't star) and don't take damage.

Special Brain
 
OK post cleaning. I have one character, a snowman character that's a part of the bat characters. they are the good guys, I also have the bad guys, these animated brains,. they fight and everything works except when the snowman character gets in the mix,. The snowbat object makes everything freeze.

Everytime the snowbat collides with a brain all the brains and bats freeze up.
 

chamaeleon

Member
OK post cleaning. I have one character, a snowman character that's a part of the bat characters. they are the good guys, I also have the bad guys, these animated brains,. they fight and everything works except when the snowman character gets in the mix,. The snowbat object makes everything freeze.

Everytime the snowbat collides with a brain all the brains and bats freeze up.
Write a script that logs lines to a file and use it anywhere where it helps you narrow down why something freezes.
 
Instead of writing a script to write to a log
(Problem #1) I looked through the archive of t he forum and it seems there were a few people that have the same issue:
The variable <unknown_object> etc. I haven't found anybody who had a solution
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object WindBatObject:
Variable <unknown_object>.hp(100007, -2147483648) not set before reading it.
at gml_Object_WindBatObject_Draw_0 (line 61) - if (hp<=0)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_WindBatObject_Draw_0 (line 61)
2. I do not know how to write a script that would do that.
 

chamaeleon

Member
Instead of writing a script to write to a log
(Problem #1) I looked through the archive of t he forum and it seems there were a few people that have the same issue:
The variable <unknown_object> etc. I haven't found anybody who had a solution


2. I do not know how to write a script that would do that.
Off you go figuring out if you have an instance_destroy() happening before you reach that line.
 

chamaeleon

Member
As for writing a log script.. That should be easy. Open file, concatenate all arguments given to the script in some fashion, write string to file, close file.
 
Ok. I forfite. I checked my BatParentObject and my WindBatObject there isn't any instance_destroys. You seem to know something you don't want to divulge... ;)
As for a log file I know how to do that. That's a lot of code though! And not even that, I wouldn't even know where to point the log?
 

Nidoking

Member
If the line quoted in the error message inside a with? It's likely that you're not checking to make sure the thing in the with isn't noone.
 

chamaeleon

Member
Ok. I forfite. I checked my BatParentObject and my WindBatObject there isn't any instance_destroys. You seem to know something you don't want to divulge... ;)
As for a log file I know how to do that. That's a lot of code though! And not even that, I wouldn't even know where to point the log?
Instance destroy was a guess on my part I don't know for a fact that it is the same issue for you. However, when I try it myself, I get a very similar output.
GML:
instance_destroy();
if (hp < 10) { show_debug_message("Test"); }
draw_self();
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_generic:

Variable <unknown_object>.hp(100006, -2147483648) not set before reading it.
 at gml_Object_obj_generic_Draw_0 (line 6) - if (hp < 10) { show_debug_message("Test"); }
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_generic_Draw_0 (line 6)
 
Instance destroy was a guess on my part I don't know for a fact that it is the same issue for you. However, when I try it myself, I get a very similar output.
GML:
instance_destroy();
if (hp < 10) { show_debug_message("Test"); }
draw_self();
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_generic:

Variable <unknown_object>.hp(100006, -2147483648) not set before reading it.
at gml_Object_obj_generic_Draw_0 (line 6) - if (hp < 10) { show_debug_message("Test"); }
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_generic_Draw_0 (line 6)
draw_self() has to be at the top of the event doesn't it?
 

chamaeleon

Member
HEy quick question. How do you get the current font?
I do not believe you can, you can only set it. I may be wrong. You could do it yourself by using a global variable and replacement script for draw_set_font() that both sets the font and assigns the font to that variable so you can check it at your convenience. Obviously this means changing all draw_set_font() calls to your script instead.
 
Yeah only way you can do it is:


myFont = Arial_Font_Modified
draw_set_font(TinyChancery)
draw_text_transformed_colour(x+150, y, "HP:" + string(floor(hp)), 2, 2, 0, c_black, c_black, c_black, c_black, 1.0);
draw_set_font(myFont)

@chamaeleon Figure out the unknown variable thing yet? lol I"m running out of time
Task list for Ted Today = 1) Specialty Brains need fixed. 2) Snowbat locking up 3) weird hp unknown variable bug
 

TsukaYuriko

☄️
Forum Staff
Moderator
There is a draw_get_font now - I believe it hasn't always been there, but was added alongside a couple of other "missing" get functions quite a while ago.
 

chamaeleon

Member
For fun you could use show_debug_message("Exists? " + string(instance_exists(id))) at the top of the draw event and then just before the line of the crash, and see if you get a 1 and a 0, a 0 and 0, or some other combination.
 
Not funny!!

@chamaeleon Oh....that instance_destroy

@chamaeleon Ok. I don't understand why this doesn't work:

with (HealthMeterParent) {
if (hp<=0)
{
instance_destroy()

}
}
I understood why calling instance_destroy before a function that code. But all this does is check hp
 
Last edited:

chamaeleon

Member
Not funny!!

@chamaeleon Oh....that instance_destroy

@chamaeleon Ok. I don't understand why this doesn't work:

with (HealthMeterParent) {
if (hp<=0)
{
instance_destroy()

}
}
I understood why calling instance_destroy before a function that code. But all this does is check hp
Are you calling with() inside the draw event in order to do something with all instances that are a HealthMeterParent or a child thereof?
 
Are you calling with() inside the draw event in order to do something with all instances that are a HealthMeterParent or a child thereof?
I am calling instance destroy on HealthMeterParent, and therefore calling instance destroy on on all the children of HealthMeterParent, right?? What gets me is
with (HealthMeterParent)
{
instance_destroy()
}
works, but
if (hp <= 0)
{
with(HealthMeterParent)
{
instance_destroy()
}
}
is an error
 

Nidoking

Member
I am calling instance destroy on HealthMeterParent, and therefore calling instance destroy on on all the children of HealthMeterParent, right?? What gets me is
with (HealthMeterParent)
{
instance_destroy()
}
works, but
if (hp <= 0)
{
with(HealthMeterParent)
{
instance_destroy()
}
}
is an error
Which object/instance is the hp variable in? HealthMeterParent, or the one that's running this event?
 

chamaeleon

Member
I am calling instance destroy on HealthMeterParent, and therefore calling instance destroy on on all the children of HealthMeterParent, right?? What gets me is
with (HealthMeterParent)
{
instance_destroy()
}
works, but
if (hp <= 0)
{
with(HealthMeterParent)
{
instance_destroy()
}
}
is an error
Is the instance that first runs the with() code itself a HealthMeterParent or child thereof?
 
If there's an error, always post the error. ;)
Miss @TsukaYuriko I believe I did post the error....it was just waaaaaaay up.

GML:
Instead of writing a script to write to a log
(Problem #1) I looked through the archive of t he forum and it seems there were a few people that have the same issue:
The variable <unknown_object> etc. I haven't found anybody who had a solution
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object WindBatObject:
Variable <unknown_object>.hp(100007, -2147483648) not set before reading it.
at gml_Object_WindBatObject_Draw_0 (line 61) - if (hp<=0)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_WindBatObject_Draw_0 (line 61)
2. I do not know how to write a script that would do that.
 
Quote Reply
Report Edit
 

TsukaYuriko

☄️
Forum Staff
Moderator
The instance that runs in the with() code the HealthMeterParent.
Did I understand this correctly? Are you running with (HealthMeterParent) { instance_destroy(); } in an instance of HealthMeterParent? If so, that will destroy the calling instance as well (as it is an instance of HealthMeterParent) - if you then try to access any of its variables afterwards by referring to it via its instance or object ID, that will end up crashing as the instance will already have been removed from the instance list.
 
Did I understand this correctly? Are you running with (HealthMeterParent) { instance_destroy(); } in an instance of HealthMeterParent? If so, that will destroy the calling instance as well (as it is an instance of HealthMeterParent) - if you then try to access any of its variables afterwards by referring to it via its instance or object ID, that will end up crashing as the instance will already have been removed from the instance list.
Nope. I'm running with(HealthMeterParent) { instance_destroy(); } in the Draw event

While we are talking, I need the object name of an instance. I tried object_get_name(attackTarget) but it came back undefined.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
In the Draw event of what, specifically? Please post the full code of said Draw event as well.

object_get_name will explode on you if you feed it an instance ID instead of an object ID.
You'd first have to get the object ID of the object the instance is an instance of. This is stored in the instance's variable object_index.
 
In the Draw event of what, specifically? Please post the full code of said Draw event as well.

object_get_name will explode on you if you feed it an instance ID instead of an object ID.
You'd first have to get the object ID of the object the instance is an instance of. This is stored in the instance's variable object_index.
Ok. How do you get the object_index of an instance?
 

TsukaYuriko

☄️
Forum Staff
Moderator
The instance's object ID is stored in its variable object_index. You can refer to this variable as you would refer to any other instance variable.
 
Also. I've been thinking. I want my brains to be solid. But what happens is they get all congested. So I'd like to have a timer of sorts so that after so many seconds they self-destruct. How would I go about doing that?
 

TsukaYuriko

☄️
Forum Staff
Moderator
You could use an alarm for this - when they're congested, check if the alarm has already been set, and set it if it hasn't been. In the corresponding Alarm event, destroy it.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Some of the collision functions have _list variants - you could use this to check if a certain amount of instances are overlapping the calling one (as the size of the returned list would increase).
 
Status
Not open for further replies.
Top