Jump to content


Photo

Problem with parent object and variable


  • Please log in to reply
3 replies to this topic

#1 psnpivottt

psnpivottt

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 24 February 2012 - 04:43 AM

Hey guys, I'm not new here, I just lost my old account since I didn't come here for ages, and I don't have the same email I used to have. Well, let's say I'm learning GML and creating a game as a school project. A space shooter.

Now this may be a bit complex, as I can't see where is the error in my code. I have a obj_ennemy which is the parent of obj_bad_aim.

obj_bad_aim creation code
// Set health, speed, gun

{
    var_health = 2;
    speed = 4;
    alarm[0] = 30;
    direction = 270;
}

obj_ennemy collision code (with obj_projectile)
// Damage and destruction

{
    var_health -= other.var_damage;
    with(other) {instance_destroy()};
    if health <=0
    {
        obj_player.var_combo += 1;
        instance_change(obj_stardust,1);
    }
}

Now obj_projectile is actually the parent of all projectiles. Here's the code of the default bullet, obj_shard.
// Set damage

{
    var_damage = 1;
    event_inherited();
}

This is the creation event, which then leads to the inherited event giving the speed to projectiles.

If you read, you should see through all this that the var_damage of the projectile hurts the ennemy, and the max hp depends on the type of ennemy, and the damage depends on the type of projectile. However, it doesn't work at all. It's not a projectile error, that's for sure. It seems the variable var_health doesn't even exist in-game. I don't get it.
  • 0

#2 torigara

torigara

    GMC Member

  • GMC Member
  • 6507 posts

Posted 24 February 2012 - 06:08 AM

if health <=0

For starters, how about changing it into var_health?

If you mean you actually get an error, it is helpful to copy and paste the exact error message that tells what variable, what code in what object caused the error.
  • 1

#3 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9322 posts
  • Version:Unknown

Posted 24 February 2012 - 06:31 AM

Are you sure you're not accidentally using an instance of the parent object, or another child besides obj_bad_aim that's not setting the var_health in the create event? Other than the health=>var_health issue mentioned above, I see no problem with your code.

-IMP
  • 1

#4 psnpivottt

psnpivottt

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 24 February 2012 - 05:59 PM

Thanks torigara, I knew it would be something stupid like this.
However, I don't get why I could not display the var_health using the debugger. I mean, it existed, it just did nothing when it reached zero. Well, I guess I made a typo in the debugger, I can't see anything else. I'm glad it was that simple. Thanks! (I did get a few errors after adding the var_ part, but that's 'cause "var_combo" and "var_exp" didn't exist yet). Problem solved!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users