Game Mechanics Space Radar

I

Ionn Blignault

Guest
Hey guys,
I'm just getting into GML so I hope this makes sense ha ha...here goes

Ok so I'm playing around with a Space farming concept and I want to implement a radar system that points to objects in the room.

Below you can see the Space Station's marker(red) pointing in it's direction correctly, since there is only one instance of the object i can hard code the point_direction BUT I can't do that for the asteroids and that is what this post is about:

radar.jpg

First I loop through all the asteroid objects and push their id's into a global array

for ( i = 0; i < instance_number(obj_asteroid); i += 1 ){
global.asteroid_ids = instance_find( obj_asteroid, i );
}


Then I get the array count to do an instance_create loop for each asteroid

var asteroid_array_count = array_length_1d( global.asteroid_ids );

for( i = 0; i < asteroid_array_count; i++ ){

/* ====
testing to see if x and y values are returned...they are!
show_debug_message(string(i) + " asteroid x: " + string(global.asteroid_ids.x ) );
show_debug_message(string(i) + " asteroid y: " + string(global.asteroid_ids.y ) );
=====*/

//create asteroid marker
var radar_arrow_id = instance_create(obj_player.x, obj_player.y, obj_radar_arrow);

with(radar_arrow_id)
{
?????????
point_direction( obj_player.x, obj_player.y, global.asteroid_ids.x, global.asteroid_ids.y );
}

}


I don't want to use instance_nearest because the markers must have a set target and only that target.

Again, I'm new to GM and GML...actually game development in general LOL so feel free to correct me.

Cheers guys
 

Yal

🐧 *penguin noises*
GMC Elder
You could just use each ID, right?

Code:
var i = 0;
with(radar_arrow_id)
{
  direction = point_direction(obj_player.x,obj_player.y,asteroids_ids[i].x,asteroids_ids[i].y);//Or however you make them point right
  i++
  if(i >= instance_number(obj_asteroid)){//End the loop if you run out of asteroids
    break
  }
}
 
I

Ionn Blignault

Guest
Thanks for your reply Yal!

I'm using this for the facing direction:
var radar_dir = point_direction(x, y, radag_arrow_target.x, radag_arrow_target.y);
image_angle = radar_dir;


Apologies for the lack of indentation, the "with()" is inside a for loop so the [ i ] is already increment...right?
But I'll give that a try thanks :)
 

Yal

🐧 *penguin noises*
GMC Elder
Oooh, point. I misread the code, my code assumes you've got an object named radar_arrow_id and would loop through all of those to assign a random asteroid to each radar. (But the same asteroid gets assigned to each radar each step due to how instance ordering works)

This should work better.
Code:
with(radar_arrow_id)
{
  image_angle = point_direction(obj_player.x,obj_player.y,asteroids_ids[i].x,asteroids_ids[i].y);
}
 
I

Ionn Blignault

Guest
Shaweeet! I got it working!

Ha ha ha no I'm actually doing it the opposite way ha ha getting the asteroid count and then creating an radar arrow for each arrow and assign a unique target to it

Have a look at the image below

The pink section are the asteroid Id's 100003, 100007
  1. The debug message logs the radar_arrow.id
  2. Then I log the target references to the global.asteroid_ids[target] that is supposed to increment...but doesn't for some reason
  3. and finally i log the target asteroid.id of the radar_arrow instance created...IT"S THE SAME ID!?!?!? 100003

Soooo what that means to me is, two radar arrows are created but they are pointing to the same target???


var radar_arrow_id = instance_create(obj_player.x, obj_player.y, obj_radar_arrow);

var target = 0;

with(radar_arrow_id)
{
radag_arrow_target = global.asteroid_ids[target];

show_debug_message("asteroid arrow: " + string(radar_arrow_id) + " targer: " + string(target) + " aasteroid: " + string(radag_arrow_target) );
}

target++;



radar.jpg
 
I

Ionn Blignault

Guest
Finally it's working properly now.

I just changed target = i seeing that it doesn't want to increment ha ha

for(i = 0; i < asteroid_array_count; i++){
var radar_arrow_id = instance_create(obj_player.x, obj_player.y, obj_radar_arrow);

var target = i;
with(radar_arrow_id)
{
radag_arrow_target = global.asteroid_ids[target];
}
}


Thanks for all the help Yal! Much appreciated
 

Attachments

Morendral

Member
I would recommend changing it to Yal's suggestion. Running through a loop like the way you want to do it is a much bigger performance hit.
 
Top