• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Cycling through enemy ID

chefdez

Member
Hello there,

I have an enemy object that spawns a target on itself when the player right clicks in a slow motion state.

oPlayer:
Code:
with(oEnemy)                          
 {
   if distance_to_point(mouse_x, mouse_y)<20                         
   {                   
    new_target_id = instance_create(x, y, oTarget);
    new_target_id.EnemyTargeted = id;                                                                                                                                     }
oTarget:
Code:
if (EnemyTargeted > -4 ) //if enemy object has detected mouse near to enemy,has created this target object, and has told this object the instance id of the enemy to follow
   {
      x = EnemyTargeted.x //match the target position to the enemy instance position x
      y = EnemyTargeted.y //match the target position to the enemy instance position y 
    }
My main goal is to have my player object "dash" towards these targeted points 1 by 1, with an alarm separating the dashes. For example:
  • Player enters slow motion state and a count down occurs.
  • Player right clicks enemy within a specific distance
  • Enemy creates a target on itself, and stores its own location.
  • When the count down is over, the player object will be forced to that position, one by one if multiple enemies are targeted.
How can my player object cycle through each enemy one by one depending on location or id?
 
H

hunijkah

Guest
Maybe use a ds_list?

https://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds lists/

Create for obj_player
targets = ds_list_create();

with(oEnemy)
{
if distance_to_point(mouse_x, mouse_y)<20
{
new_target_id = instance_create(x, y, oTarget);
new_target_id.EnemyTargeted = id;
with(obj_palyer){ ds_list_add(targets, new_target_id); }

Then once slow mo is over run some code with the obj_player like:
var listSize = ds_list_size(targets);
for(var i =0; i < listSize; i++){
var targetObj = ds_list_find_value(targets, i);
x = (targetObj).x;
y = (targetObj).y;
}

By the way how do you make these code blocks on this forumn?
 

samspade

Member
By the way how do you make these code blocks on this forumn?
Click on insert in the formatting bar or use [ code] [/ code] but without the spaces.

To the original question, storing the x/y or instance id in a list, queue, or array would probably be easiest. The previous example is good, except that you wouldn't want to use a for loop as that happens instantaneously. lists are the easiest to understand (in my opinion) and have a fair amount of flexibility to them. You'd have to figure out how to order it though. Arrays are the lowest complexity, but no helper scripts (like sort or add) so you have to know how to manipulate them. Priority Queues are the most complex of the three (though not very complex) but they are by definition 'sorted' so that could be easier.

Once you've stored everything in order you can have a simple process that goes like this:
  • Set up your own alarm or use a custom alarm or flag
  • When the alarm/flag goes off pull the next value (id or x y) from the list or array
    • for an array, track the number of times you've moved and use that to pull the next value
    • for a ds list, use the first value and delete it
    • for a priority queue use ds_priority_delete_min
  • Move to that point/perform action
 
Top