[solved] how to move an object in the image angle of a different object

Discussion in 'Programming' started by Nuthole, Aug 14, 2019.

Tags:
  1. Nuthole

    Nuthole Member

    Joined:
    Aug 14, 2019
    Posts:
    2
    ok so i need to make the image angle of objectA the direction of objectB, but when i do "launch = image_angle" on objectA, objectB does not understand the "launch" variable. how do i make objectB understand?
     
  2. spirit_games

    spirit_games Member

    Joined:
    Aug 14, 2019
    Posts:
    1
    Could you clarify a bit? What is the variable launch for? Does it store the objects direction? You can call out variables from other objects by stating the object before the variable.

    If you wanted objectA’s image_angle to equal objectB’s launch direction you could type:

    If objectB is calling the code...
    Code:
    objectA.launch = image_angle
    If objectA is calling the code...
    Code:
    objectB.image_angle = launch
     
  3. Nuthole

    Nuthole Member

    Joined:
    Aug 14, 2019
    Posts:
    2
    Hi, thank you so much! i have been having this problem for actual days now and this is the first thing that actually worked, the working code is: motion_add(objectA.launch, 17) for anyone who sees this in the future, thanks!
     
  4. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,210
    Just remember that there's a big difference between objects (the blueprint for instances) and instances themselves (the things that exist in your game). I feel like both the "answerer" and asker of this question don't really understand this vital difference. If you have more than one objectA in this scenario, you will get buggy code. What you should be doing is storing the instance ID of objectB when it is being created and then using that ID to tell the instance of objectB what to do, something like this (this is from within objectA):
    Code:
    var inst = instance_create_layer(x,y,"layer",objectB); // Store the instance ID of the objectB instance that was just created in a temp variable called "inst"
    with (inst) { // Now we use with() to tell muddle with the insides of the instance of objectB that was just created, using the previously created temp variable holding said instance ID "inst"
       launch = other.image_angle; // If this code is being run from within objectA then other in this circumstance will refer to objectA, so we can use other.image_angle to get the image_angle of objectA and set an instance variable called launch within objectB to it.
    }
    
    Then you can simply do:
    Code:
    motion_add(launch,17);
    
    Within objectB and it will aim wherever the image_angle of objectA was facing when objectB was created.
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,444
    While the members before you have some work to do when it comes to objects vs. instances, looks like you have some work to do as well when it comes to event timing. If you put the motion_add() line in the Create event of objectB, it will still trigger too early.

    One alternative is to put the motion_add() line in a User event and call that:
    Code:
    with (instance_create_layer(x, y, "layer", objectB)) {
      launch = other.image_angle;
      event_user(14);
    }
    
    Code:
    // User event 14
    motion_add(launch, 17);
    
    A lesser alternative is to put the motion_add() line with objectA like this:
    Code:
    with (instance_create_layer(x, y, "layer", objectB)) {
      motion_add(other.image_angle, 17);
    }
    
    I consider this worse than the user event solution because it puts the definition of properties for objectA in the hands of objectB.
     
    RefresherTowel likes this.
  6. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,210
    Oh, I didn't intend for them to put the motion_add in the Create Event (you'll notice I never actually said Create Event), though reading the post back I can see that that's the implication. I should've been more explicit in my "Within objectB" statement about the fact that it has to be done in a single step fire Alarm, a triggered User Event or within a flag variable if statement in the Step Event (though of course, the User Event is best way).
     

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