GMS 2 How do I set an alarm for every Instance of an Object individually?

Discussion in 'Programming' started by moichelm, Apr 25, 2017.

  1. moichelm

    moichelm Guest

    Hey Community!
    This is my first post since my recent introduction to Game Maker and I am loving it!
    I've now run into a problem, that Google and browsing the forum doesn't seem to fix.

    I trying my hand at a top-down shooter and I am struggling with something:

    I have multiple instances of my Enemy object and I want them to shoot individually at a random rate.
    Right now I have an alarm that sets itself everytime it triggers + a random range.
    Problem with that is, that only one instance shoots...

    How would I go about solving this problem?
     
  2. TheMagician

    TheMagician Member

    Joined:
    Jun 20, 2016
    Posts:
    57
    This may be the wrong forum for your question.

    Anyway, from what you're describing the instances should all shoot individually. So we will need to see some code in order to find the error.
     
  3. moichelm

    moichelm Guest

    Oh my bad, I will post it in the other forum plus the code.
     
  4. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,892
    No need! I've moved the topic for you. And you needn't make a new topic if you post in the wrong forums, simply hit the Report button and request that a moderator move it for you. ;)

    As for your issue, we really need to be able to see what the code/DnD is for both shooting and for the bullet movement. It could be many things, but from what you say you want to check how you are setting the bullets to move and their position, and also how you create them. One obvious thing though would be that maybe you are using an object index instead of an instance ID when you create them?
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    If your code even remotely looks like the codes below or part of the codes below
    Code:
    instance_create(ship.x,ship.y,bullet);
    bullet.direction = point_direction(ship.x,ship.y,player.x,player.y)
    ship.alarm[0] = 5;
    
    Code:
    instance_create(x,y,bullet);
    with bullet direction = point_direction(x,y,player.x, player.y)
    
    then you messed up already.

    Referencing instances by their object names like in the first code is a no-no. The first line uses object variable reads; since only one instance can be read at a time, only one particular ship will have its coordinates fetched and it may not be the ship you want. The second and third line contains an object variable write (and reads, but we covered that); when writing to an object, all instances of that object are affected, so in this case all bullets would be affected.

    Using with() on an object name is a no-no. It will make the following code affect all instances of that object, not the last one created.

    Your code should instead look like one of these two:
    Code:
    if can_shoot { 
    with instance_create(x,y,bullet) direction=point_direction,x,y,player.x,player.y);
    can_shoot=false;
    alarm[0] = 5;
    }
    or
    Code:
    if can_shoot {
    var inst = instance_create(x,y,bullet);
    inst.direction = point_direction(x,y,player.x, player.y);
    can_shoot = false;
    alarm[0] = 5;
    }
    
     
    Gasil and CMAllen like this.
  6. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    855
    It should also be said that unless you can be absolutely certain any run-time created objects will be destroyed properly, objects that create other objects should have some sort of array or list of the IDs of any objects created so that you can check in on them and destroy any errant objects manually (say, once every few seconds). Both examples above operate under the assumption that the objects being created will always destroy themselves by some inescapable event. If they don't, you get a memory leak.
     

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