• 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!

GML Find out object type from id

L

leuchtstein

Guest
Hello. I have an object, a turret from a tank. On creation it does
Code:
parent = instance_nearest(x,y,obj_entity)
Basically, obj_entity is the parent of all players, enemies or other AI. I want to something in the Step Event depending on what type of object the "parent" variable is.
For example:
I have obj_enemy, which is the parent of all enemy objects
I have obj_allied, which is the parent of all allied objects
I have obj_player, which is the player

I now want, if "parent" is for example an obj_enemy, it should do different things than if it is an obj_allied (Allied should shoot enemy, and reverse, as it is a turret)

But using simply
Code:
if parent == obj_enemy
or something like that doesnt work
I also tried working with object_index and object_get_name, but it didn´t work.
I have the object ID in the parent variable, I thought I can do something with it, but I can´t figure anything out.
Can anybody help me? Thanks.

( I am using GameMaker 8.1 )
 
S

Stratadox

Guest
Disclaimer: Haven't done GML in a while, might be rusty.

From what I remember, game maker differentiates between instances and objects; your property parent is an instance. Therefore, when comparing it to an object, they are by defintion not equal.

You're probably looking for parent.object_index, as in

if parent.object_index== obj_enemy

Edit: Corrected, see below
 
Last edited by a moderator:
Disclaimer: Haven't done GML in a while, might be rusty.

From what I remember, game maker differentiates between instances and objects; your property parent is an instance. Therefore, when comparing it to an object, they are by defintion not equal.

You're probably looking for parent.object, as in

if parent.object == obj_enemy
Actually, the variable is "object_index". See here. You had the correct idea though! :)

Example:
Code:
parent = instance_nearest(x,y,obj_entity);
if (parent.object_index == obj_enemy)
//Do code
 
L

leuchtstein

Guest
EDIT: It did work for obj_player but not for anything else.
I will paste my code here, maybe I messed up
Code:
if parent.object_index != obj_player && parent.object_index == obj_allied {


if instance_exists(obj_enemy) {
nearest = instance_nearest(x,y,obj_enemy)
dir = point_direction(x,y,nearest.x,nearest.y)
image_angle = dir
if distance_to_object(nearest) <= 64 {
if !cooldown {
    cooldown=true
    alarm[0]=20
    action_create_object_motion(obj_standard_bullet,x + lengthdir_x(30, image_angle), y + lengthdir_y(30, image_angle),7,dir)
}
}
}

} else if parent.object_index  == obj_enemy {

if instance_exists(obj_allied) {
nearest = instance_nearest(x,y,obj_allied)
dir = point_direction(x,y,nearest.x,nearest.y)
image_angle = dir
if distance_to_object(nearest) <= 64 {
if !cooldown {
    cooldown=true
    alarm[0]=20
    action_create_object_motion(obj_s_enemy_bullet,x + lengthdir_x(30, image_angle), y + lengthdir_y(30, image_angle),7,dir)
}
}
}

} else {
//spieler
dir = point_direction(x,y,mouse_x,mouse_y)
image_angle = dir

}
Basically, if the turret belonged to an enemy, it should target obj_allied (parent of player and allied objects) and if it was an ally it should target obj_enemy, player could just do what he wants (as seen in code)

It doesnt work though, the player code always kicks in
 
Last edited by a moderator:
I think I might have found the issue, but I don't have the solution. Maybe I can think about it for a while or someone else can chime in, but "parent == instance_nearest (x,y,obj_entity)" might be returning the ID of itself, not the nearest entity. Or rather, the nearest entity is itself. The reason I am wondering this is because of this statement from the documentation on the instance_nearest function:
Documentation said:
Please note that if the instance running the code was created as an instance of the object being checked, then it will be included in the check.
You could confirm this by running this code after you set "parent":
Code:
if (parent == self)
game_end();
If the game ends then that means it is setting parent to itself. Now you at least might know part of the problem.

This could be a ridiculous shot in the dark, but I thought I'd post it. It's kind of a guess though, just to get closer to finding the problem.
 
In addition to @RefresherTowel 's post above to avoid returning the instances own id, when I've had trouble identifying the object_index when I have a multi-level object hierarchy, I use the function object_is_ancestor(object, parent).

So instead of your code:

Code:
} else if parent.object_index  == obj_enemy {
use this:
Code:
} else if object_is_ancestor( parent.object_index,obj_enemy) {
This may not apply in your case if you don't have several levels of parents before you get to obj_enemy, thought I would mention it.
 
Top