1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Teleport objects to random objects

Discussion in 'Programming' started by MagicFool64, Feb 10, 2019.

  1. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    17
    I use GM: Studio 1
    The game is a top-down one. I want to make a make a my character in the game, summoning 10 objects that spawn on 10 random enemies' position. This is the point: my character summons 10 roots that block 10 random enemies and make them stop walking, until the roots disappear. Maybe I'm asking too much. Can you give me a script? Maybe a second one that summon a single object too
     
    Last edited: Feb 10, 2019
  2. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    299
    One thing to help simplify this is to have all susceptible enemies have the same parent object. The parent object need not have any code in it, nor even a sprite.

    The simplest script in this case would be something like the following:

    Code:
    var count, so_far;
    so_far = 0;
    count = 10;
    with(EnemyParent) {
        if not place_meeting(x, y, Root) and so_far < count {
            instance_create(x, y, Root);
            so_far += 1;
        }
    }
    Please ask questions if there is any part of this that you don't understand.
     
    SnotWaffle Studios likes this.
  3. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    364
    If I'm not mistaken this will loop through EnemyParent objects in order of creation. You'd have to add their IDs to something like a ds_list, shuffle the list, then iterate through it
     
  4. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    299
    Yes, but I figured starting with the simplest form would be helpful. We could add the randomization factor in later.
     
  5. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    364
    wut
     
    SnotWaffle Studios likes this.
  6. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    17
    Does it work only on enemies on screen?
     
  7. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    299
    No, this would work on any enemies in the room. But as stated in earlier replies, this raw code only spawns roots on the ten earliest created enemies. For full randomization, see the code below.

    Again, this works for ALL enemies in the room.

    Code:
    var count, so_far, id_list;
    so_far = 0;
    count = 10;
    id_list = ds_list_create();
    with(EnemyParent) {
        ds_list_add(id_list, id); //Appends this enemy's ID to the list
    }
    ds_list_shuffle(id_list); //Randomize the list
    
    while so_far < count and so_far < ds_list_size(id_list) {
        with(ds_list_find_value[so_far]) {
            if not place_meeting(x, y, Root) and so_far < count {
                instance_create(x, y, Root);
                so_far += 1;
            }
        }
    }
    ds_list_destroy(id_list); //Destroy the list. We don't need it anymore.
     
  8. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    17
    Can you give me a script that works only on enemies in the view/on the screen?
     
  9. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    299
    Modified code below. Note that it ASSUMES that you have views enabled in the room.
    Only enemies that are even partially in view will be included in the list of possible targets.
    If an enemy is completely out of view, it cannot be targeted.

    Code:
    var count, so_far, id_list;
    so_far = 0;
    count = 10;
    id_list = ds_list_create();
    with(EnemyParent) {
       if bbox_right >= view_xview and bbox_left < view_xview + view_wview
       and bbox_bottom >= view_yview and bbox_top < view_yview + view_hview {
           ds_list_add(id_list, id); //Appends this enemy's ID to the list
       }
    }
    ds_list_shuffle(id_list); //Randomize the list
    
    while so_far < count and so_far < ds_list_size(id_list) {
       with(ds_list_find_value[so_far]) {
           if not place_meeting(x, y, Root) and so_far < count {
               instance_create(x, y, Root);
               so_far += 1;
           }
       }
    }
    ds_list_destroy(id_list); //Destroy the list. We don't need it anymore.
     
  10. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    17
    It appears a Compile Error message. It says to check the compile window. Maybe this is a script for GM: Studio 2, and I have the first
     
  11. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    299
    What specifically is the error message?
     
  12. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    17
    "Compile Failed - Please check the Compile window for any additional information"
    What it does mean?
     
  13. YoSniper

    YoSniper Member

    Joined:
    Jun 24, 2016
    Posts:
    299
    There should be additional messages in the log window.
    Like where it says "Writing chunk SPRT" and such. What is the last thing it says before giving a compile error?
     
  14. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    17
    Where have I to read the last thing? Can we try with an alternative script? Maybe a script that checks enemies' range to the player
     
    Last edited: Feb 11, 2019
  15. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,559
    The compile error is likely coming from this line:
    Code:
    with(ds_list_find_value[so_far]) {
    
    It should be:
    Code:
    with (id_list[| so_far]) {
    Or:
    Code:
    with (ds_list_find_value(id_list, so_far)) {
     
  16. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    17
    I tried with "with (id_list[| so_far]) {" and "with (ds_list_find_value(id_list, so_far)) {". The game crashes. Can we try with an alternative script?
     

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