Teleport objects to random objects

M

MagicFool64

Guest
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 by a moderator:

YoSniper

Member
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.
 
B

Bayesian

Guest
with(EnemyParent) {
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
 

YoSniper

Member
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
Yes, but I figured starting with the simplest form would be helpful. We could add the randomization factor in later.
 
M

MagicFool64

Guest
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.
Does it work only on enemies on screen?
 

YoSniper

Member
Does it work only on enemies on screen?
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.
 
M

MagicFool64

Guest
Can you give me a script that works only on enemies in the view/on the screen?
 

YoSniper

Member
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.
 
M

MagicFool64

Guest
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.
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
 

YoSniper

Member
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?
 
M

MagicFool64

Guest
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?
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 by a moderator:

FrostyCat

Redemption Seeker
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)) {
 
M

MagicFool64

Guest
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?
 
Top