1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 [Solved] Randomly picking an instance's direction

Discussion in 'Programming' started by Biosyn, May 3, 2019.

  1. Biosyn

    Biosyn Member

    Joined:
    Oct 17, 2017
    Posts:
    43
    Hi.

    I'm trying to make a set of projectile objects move toward a set of deflector object instances. The deflectors are arranged in a circle after being spawned through a for loop. It seems to be working except for an issue where the deflected projectiles do not randomize and only choose to move towards one of the deflector object instances (probably the first one created thru the for loop).

    The setup is as follows. A player object creates a set of deflector object instances linked to it:

    def_inst=instance_create....def_object
    def_inst.myid=myid; //myid being the main controller for the player, deflector and projectiles.

    After creating the projectile instances, they are programmed to spawn within the deflector instances arranged in a circle and choose a direction pointing towards one of the deflectors, randomly. It works fine up to this point. Now when the projectile moves toward a deflector and collides with it, I want it to change direction to point towards any of the other deflectors (not the one it just collided with). The end result should be the projectiles colliding against the deflectors and criss-crossing within the circle formation.

    The projectile code for colliding with the deflector:

    Code:
    if can_deflect==1
        {
        can_deflect=0
        alarm[0]=4 //reset can_deflect
        //direction=180+direction//curr_dir  
        var inst = instance_find(myid.def_inst, irandom(instance_number(myid.def_inst) - 1));
        direction=point_direction(x,y,inst.x,inst.y)  
        }
    As I said earlier, the problem is, after colliding with the deflectors initially, instead of randomly picking and moving towards any other deflector's direction, the projectiles move towards one only.

    Is there any way to achieve this?

    Any guidance is appreciated. Thanks!
     
    Last edited: May 3, 2019
  2. vdweller

    vdweller Member

    Joined:
    Jun 24, 2016
    Posts:
    137
    Hmmm have you tried using
    myid.def_inst.object_index?
     
    Biosyn likes this.
  3. Biosyn

    Biosyn Member

    Joined:
    Oct 17, 2017
    Posts:
    43
    Well I'll be. Thanks for that quick fix! Works as expected.
     

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