Jump to content


Photo
* * * * * 1 votes

Radar Tutorial - Intermediate GML


  • Please log in to reply
8 replies to this topic

#1 wakeskater_X

wakeskater_X

    GMC Member

  • GMC Member
  • 448 posts

Posted 09 January 2012 - 11:02 PM

  • Title: Radar Tutorial
  • Description: Learn how to make a simple radar object to display where your enemies are! Intermediate Skill
  • GM Version: GM 8.1;
  • Registered: no (i don't think it uses any of the advanced commands, someone correct me if this is wrong)
  • File Type: .gm81
  • File Size: 13 kb
  • File Link: Mediafire Link
  • Required Extensions: none
  • Required DLLs: none
Summary
This is a short demo on how to make a radar screen in the corner of your view to show where enemies are.

The programming for the player and enemies are just hashed together, this is focusing on the radar object which contains most of the code you want to look at.

What you should know about beforehand:
-GML
-Views
-Draw functions

It's a simple code for drawing the sprite and blips (and I use trig functions instead of the length_dir ones, those can be used alternatively) I replaced the code in their with the new lengthdir ones **updated with credit to icuurd**

Let me know what you think.

EDIT (2-17-13) Updated the Mediafire link so it now works.

Edited by wakeskater_X, 17 February 2013 - 04:44 PM.

  • 1

#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14390 posts
  • Version:GM:Studio

Posted 10 January 2012 - 06:14 AM

You're being a little too careful with the instance_exists... you got the ids from the system, there is no chance the instance will disappear during your loops.

also, you can combine the 2 loops into one and not need the arrays.

also, instance_find() and instance_number(ParrentObj) would be more efficient and remove the need for get_parent function.

but most efficient would be to use the with statement which combines all the safety checks you used.

Your draw event, optimized
//Draws the radar screen
draw_sprite(Radar_Button, 0, view_xview[0]+75, view_yview[0]+75);
//stop here if no player
if (!instance_exists(Player_Obj))
{
    exit;
}
//local vars for easy access in the with() and to prevent creating variables in the instances
var d,a,xx,yy;
xx = Player_Obj.x;
yy = Player_Obj.y;
with(EnemyParent)
{
    //how far
    d = point_distance(xx,yy,x,y);
    //in range
    if( d< 3000)
    { 
        //convert radar range to radar display radius
        d = d/3000 *75; 
        //angle to target
        a = point_direction(xx,yy,x,y)
        //draw relative to center of radar using simplified lengthdir function
        draw_sprite(RadarBlip, 0, view_xview[0]+75 + lengthdir_x(d,a), view_yview[0]+75 + lengthdir_y(d,a));
    }
}

  • 0

#3 wakeskater_X

wakeskater_X

    GMC Member

  • GMC Member
  • 448 posts

Posted 10 January 2012 - 08:05 AM

Thanks, I knew there was a more efficient way to do this, I'm still an intermediate programmer myself with GML, so I tend to use practices from other programming experience.

Well anyway, I can add that in and update it. I basically just copied most of the code over from my current project as I thought "this might be something people would like to know how to do". Since I had to basically design it from scratch since there wasn't a tutorial on it. I'll make those changes to it, as it's probably faster to do it your way too.

Edit: And I wrote the code for the radar stuff before I knew about length_dir... so it was all trig functions ;)

Edit2: I adjusted the code you put a bit because radar (in games) tends to have that lingering effect when objects get out of the range you can still detect them for a little while. So here is the revised version of your code:

**MediaFire Link Updated**

///Draws the radar screen
draw_sprite(Radar_Button, 0, view_xview[0]+75, view_yview[0]+75);
//stop here if no player
if (!instance_exists(Player_Obj))
{
    exit;
}
//local vars for easy access in the with() and to prevent creating variables in the instances
var d,a,xx,yy;
xx = Player_Obj.x;
yy = Player_Obj.y;

with(EnemyParent)
{
    //how far
    d = point_distance(xx,yy,x,y);
    //in range
    if( d < 3000 && d > 1500)  // This will set the blips to the outside edge, creating a lingering effect
    { 
        //convert radar range to radar display radius
        d = 75; 
        //angle to target
        a = point_direction(xx,yy,x,y)
        //draw relative to center of radar using simplified lengthdir function
        draw_sprite(RadarBlip, 0, view_xview[0]+75 + lengthdir_x(d,a), view_yview[0]+75 + lengthdir_y(d,a));
    }
    else if(d <= 1500)  // This is the standard distance conversion on the radaar screen.
    {
        d = d/1500*75;
        a = point_direction(xx,yy,x,y)
        draw_sprite(RadarBlip, 0, view_xview[0]+75 + lengthdir_x(d,a), view_yview[0]+75 + lengthdir_y(d,a));
    }
}

Edited by wakeskater_X, 10 January 2012 - 08:16 AM.

  • 0

#4 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14390 posts
  • Version:GM:Studio

Posted 10 January 2012 - 08:26 AM

Oh I did not pay enough attention.

simpler (rather more streamlined) is to change in my first post
d = d/3000 *75;

to
d = min(d,1500);
d = d/1500*75;

or
d = d/1500*75;
d = min(d,75);

or, optimally
d = min(d,1500)/1500*75;

but your code is more obvious in its function
  • 0

#5 wakeskater_X

wakeskater_X

    GMC Member

  • GMC Member
  • 448 posts

Posted 10 January 2012 - 08:33 AM

Oh I did not pay enough attention.

simpler (rather more streamlined) is to change in my first post
d = d/3000 *75;

to
d = min(d,1500);
d = d/1500*75;

or
d = d/1500*75;
d = min(d,75);

or, optimally
d = min(d,1500)/1500*75;

but your code is more obvious in its function


Ah that's true. I see that's a little bit nicer and shortens up the function some. I'll be sure to use stuff like that in the future. I never think about min/max functions >_>.
  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14390 posts
  • Version:GM:Studio

Posted 10 January 2012 - 08:55 AM

The best tip I was ever told by a mathematician friend of mine is whenever you can reduce a ranged value to a range of 0 to 1 (or -1 to 0 to 1) without loosing accuracy, do so and enjoy simplicity in function.

d/1500 * 75

hspeed = (keyboard_check(vk_right) - keyboard_check(vk_left)) * 4


And don't forget about median to cap a value on both ends of your wanted range.

hspeed += (keyboard_check(vk_right) - keyboard_check(vk_left)) * .5
hspeed = median(-4,hspeed,4);
  • 1

#7 HayManMarc

HayManMarc

    The HayMan

  • GMC Member
  • 447 posts
  • Version:GM8.1

Posted 27 August 2012 - 07:30 AM

Very nice, simple radar!
  • 0

#8 wakeskater_X

wakeskater_X

    GMC Member

  • GMC Member
  • 448 posts

Posted 17 February 2013 - 04:45 PM

I updated the Mediafire link so it works again for anyone who was interested. updated 2-17-13

I didn't have the old file so I just re-made the tutorial. It was stuff I was using in SBX2 anyway.
  • 0

#9 Yassine

Yassine

    GMC Member

  • GMC Member
  • 8 posts
  • Version:GM8

Posted 03 May 2013 - 08:19 AM

I was searching about a tutorial like this and now I found it!

 

:thanks: for the tutorial. Now I can complete my game.

I was  :blush: and now I am  :woot: .


Edited by Yassine, 03 May 2013 - 08:20 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users