GML how do you check for a objects creator?

C

CPlanet1

Guest
so i'm making a smash clone fighting game and the hitboxes are trying to check if a player has been hit.
but because its checking for players the character instead keeps hitting himself. so i'm wandering how can
i get the hitbox to check if its touching the objects creator and not damage him?
 

CloseRange

Member
instance_create returns an id
Code:
var test = instance_create(x, y, obj);
you can use this to pass on variables to the created object.
Code:
var test = instance_create(x, y, obj);
test.health = 500;
so you can do this:
Code:
var test = instance_create(x, y, obj);
test.creator = id;
no the object has a variable called 'creator' set to the creator's id.
in the collision event you can say:
Code:
if(other != creator) {
    // do the hit
}
or if you arn't using collision event use the 'id' of the object you are checking
 
C

CPlanet1

Guest
so i tried out what you said but it doesn't seem to work. i put this in my attack state
Code:
var lightAttack = instance_create_layer(x,y,layer,Obj_lightAttackHB)
lightAttack.owner = self.id;
and i put this in the step event for my hitbox.
Code:
if (place_meeting(x,y,obj_player))
{
    if (other.id != owner)
    {
        instance_destroy();
    }
    if (other.id = owner) {
        // no hit
        show_debug_message("no hit")
    }
}
is there something i'm doing wrong?
 

TheouAegis

Member
Yes, you can't use other in the Step event like that.

with obj_player
if id != other.owner && place_meeting(x,y,other.id)
{
hp--;
instance_destroy(other)
}
 
For one, don't use self.id, just id is all you need. Self only exists for legacy reasons and should never be used.

Secondly, place_meeting doesn't allow you to use other within it. Read the documentation. Always read the documentation. Reading the manual, it points out that if you want the unique instance ID of the object being collided with, you should use instance_place instead. So, knowing all that, something like this should be the code you're aiming for:

Code:
var lightAttack = instance_create_layer(x,y,layer,Obj_lightAttackHB)
lightAttack.owner = id;
Code:
var other_id = instance_place(x,y,obj_player); // Get the ID of the colliding instance
var my_id = owner; // Get your own creators id
with (other_id) { // Jump into the colliding instance
   if (my_id != owner) { // If my_id does not equal the colliding instances creator then destroy
      instance_destroy();
   }
   else {
      // no hit
      show_debug_message("no hit");
   }
}
I haven't run it, so there might be a few small errors (or you may need to change the if statement slightly depending on whether I'm understanding what you're trying to do) but that should be enough to get started on.
 
C

CPlanet1

Guest
that's odd it still won't work, the debug message won't even show up. i think it might have something to do with something else in my code but i don't see anything wrong
Obj_player: step
Code:
switch (state)
{
    case PLAYERSTATE.FREE: playerState_Free(); break;
    case PLAYERSTATE.ATTACK: PlayerState_attack(); break;
    case PLAYERSTATE.HURT: PlayerState_hurt(); break;
}

// light attack
if (keyboard_check_pressed(ord("L")) && place_meeting(x, y+3, obj_solid))
{
    state = PLAYERSTATE.ATTACK;  
}
obj_player: create
Code:
// Create Variables
owner = id;

hspeed_ = 0;
max_hspeed = 4;
vspeed_ = 0;
grav = 0.3;
acceleration = 0.9;
jump_height = -9;
friction_ = .3;
speed_ = 6.3;
air_jump = 1;
sprite_reset = false;
enum PLAYERSTATE {
   
    FREE,
    ATTACK,
    HURT
}

state = PLAYERSTATE.FREE

//map the keys
keyboard_set_map(ord("D"), vk_right);
keyboard_set_map(ord("A"), vk_left);
keyboard_set_map(ord("W"), vk_up);
keyboard_set_map(ord("S"), vk_down);
playerState_free:
Code:
/// player movement
var hinput = keyboard_check(vk_right) - keyboard_check(vk_left);
var up_release = keyboard_check_released(vk_up);

// flip sprite
if hinput = 1 { image_xscale = 1.6}
if hinput = -1 { image_xscale = -1.6}


// alow sprite to reset
sprite_reset = false;

if hinput != 0 {
    hspeed_ += hinput*acceleration;
    hspeed_ = clamp(hspeed_, -max_hspeed, max_hspeed);
} else {
    hspeed_ = lerp(hspeed_, 0, friction_);
}

/// jump code

if !place_meeting(x, y+3, obj_solid) {
    vspeed_ += grav;
   
    // control jump hight
    if (up_release && vspeed_ < -6) {
        vspeed_ = -3;
    }
    // air jump
    if (air_jump > 0) && keyboard_check_pressed(vk_up) {
        vspeed_ = jump_height
        air_jump -= 1;
        }
} else {
    if keyboard_check_pressed(vk_up) {
        vspeed_ = jump_height;
        }
}
//check if on ground
if place_meeting(x, y+3, obj_solid) {
    air_jump = 1;
}

/// solid

if place_meeting(x+hspeed_, y, obj_solid) {
    while !place_meeting(x+sign(hspeed_), y, obj_solid) {
        x += sign(hspeed_);
    }
    hspeed_ = 0;
}

x += hspeed_;

if place_meeting(x, y+vspeed_, obj_solid) {
    while !place_meeting(x, y+sign(vspeed_), obj_solid) {
        y+=sign(vspeed_);
    }
    vspeed_ = 0;
}
y += vspeed_;
playerState_attack:
Code:
hspeed_ = 0;
vspeed_ = 0;

var lightAttack = instance_create_layer(x,y,layer,Obj_lightAttackHB)
lightAttack.owner = id;

if (sprite_reset = false) {
    sprite_index = Spr_player_Attack;
    sprite_reset = true;
}

// Hitbox
if (image_index >= 7) && (image_index <= 9)
{
    instance_create_layer(x,y,1,Obj_lightAttackHB)
}
if (image_index >= 8) {
    sprite_index = Spr_player;
    state = PLAYERSTATE.FREE;
}
Obj_lightAttackHB step:
Code:
var other_id = instance_place(x,y,obj_player); // Get the ID of the colliding instance
var my_id = owner; // Get your own creators id
with (other_id) { // Jump into the colliding instance
   if (my_id != owner) { // If my_id does not equal the colliding instances creator then destroy
      instance_destroy();
   }
   else {
      // no hit
      show_debug_message("no hit");
   }
}
Obj_lightattackHB draw end:
Code:
instance_destroy();
 
Top