GM:S 1.4 Can´t get values from the other.object

Discussion in 'Programming' started by Kloetengott, Mar 10, 2019.

  1. Kloetengott

    Kloetengott Member

    Joined:
    Mar 10, 2019
    Posts:
    2
    Hello Community,

    good to see so many active members here. Looks like this community is very alive :)

    So hopefully somebody can help my with a little problem:

    I´m playing around with a small Arenagame. The part i´m working on right now is the "core" itself.
    You can put up to 6 fighters in the arena and you will get 6 enemy fighters against you.
    So if I would code every possible Object-Meeting i would need to do that 36 times. I thought i found a more elegant solution:
    Code:
    if place_meeting(x,y,obj_enemy_1) || place_meeting(x,y,obj_enemy_2) || place_meeting(x,y,obj_enemy_3) || place_meeting(x,y,obj_enemy_4) || place_meeting(x,y,obj_enemy_5) || place_meeting(x,y,obj_enemy_6)
        {
            if other.alive == true
            {
                move = false;
                other.move = false;
                dir = point_direction(x, y, other.x, other.y);
                glad1_strike = global.glad_own_agi[0]+random(global.glad_own_luck[0]);
                glad2_strike = other.agility+random(other.luck);
                if glad1_strike > glad2_strike
                {
                    other.hlife -= global.glad_own_str[0];
                    instance_create(other.x, other.y, obj_sword_g1);
    ....
    This is part of the code from step-event of my main gladiator.
    As you can see i created the IF with 6 different statements that can be true. This seems to work fine. I did some tests with this and draw_texts onscreen and it worked.
    I then wanted to use the "other" feature to get values from the object, that my main-object has just met. For example the enemy gladiator.
    Not much code here yet:
    Code:
    alive = true;
    move = false;
    agility = 8;
    luck = 8;
    life = 90;
    hlife = life;
    strenght = 9;
    
    This is from the Create-Event.

    When compiling i get the error, that the values for this variables are not set:
    glad2_strike = other.agility+random(other.luck);

    So it looks like the other-feature is not working as i want it to work.
    Could somebody give ma a hint what i did wrong here?
    I would prefer not to code 36 meeting-events seperately :)
     
    Last edited: Mar 10, 2019
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,282
    First, don't duplicate the collision check for each enemy type. Instead create a new object named pobj_enemy, set the parent of all the enemy objects to pobj_enemy, and check for collisions against pobj_enemy. This is one of the most basic best practices in GM.

    Second, stop believing that place_meeting() gives other a meaning. This is clearly detailed in my article about objects and instances:
    You have 2 options at this point:
    • Use the actual Collision event from the event selector (NOT the Step event), select pobj_enemy as the collision target, and move all of your enemy collision code there. Then you can remove the place_meeting() check and continue to use other.
    • Stay in the Step event and use the return value of instance_place() instead of other:
    Code:
    var inst_enemy = place_meeting(x, y, pobj_enemy);
    if (inst_enemy != noone)
        {
        if (inst_enemy.alive)
            {
    ...
    
     
  3. Kloetengott

    Kloetengott Member

    Joined:
    Mar 10, 2019
    Posts:
    2
    Hi FrostyCat,

    at first let me thank you for taking the time.
    I decided to go with the step-event-solution and this seems to work now.
    Normally i prefer to have all my code in the step or draw-event to keep the overview, but i had problems with the second solution :)

    Before closing this thread, i try to understand how that parent-object works. I Just want to make sure, that the correct values are calculated in the collisions.
    Is this some kind of mirror or parser, that projects the coordinates of all the "child"-instances?
    i called it obj_collision btw. Easier for me to remember its purpose :)

    Edit: Just read some more about Objects and Parents.
    I think i got it - hopefully.
    I was just not sure if the correct values are used, if i "group" a weak enemy and a strong enemy using the same parent for collision.
    But if i got this right, the children are mirroring their values to the parent.
     
    Last edited: Mar 10, 2019
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,625
    it's a bit overkill, but if you want to be sure that your strong enemies and your weak enemies have nothing in common other than both being able to be targeted by one line of code, then give them all the same events and leave the parent object empty. if you put any events in the parent object head, all of the children will automatically inherit those same events unless the child object has its own version of that event. so if your parent object has create event, that all of his children will have the same create event unless you put a create event in a child, in which case that child will not inherit the parent's create event. Likewise, if you gave the parent a create event and a draw event, all of the children would inherit that create and that draw event, but if one of the children has a draw event, then that child would still inherit the create event but not the draw event.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice