Jump to content


Instance_nearest problem - Turrets *Solved*


  • Please log in to reply
4 replies to this topic

#1 Guest_Awesomeman_*

Guest_Awesomeman_*
  • Guests

Posted 08 November 2011 - 07:26 AM

*Solved*
Hey need help with my online topdown shooter, what happens is in the step event of the player object it checks for turrets within a certain range and sees whether that turret has the same id as the player if not then the turret shoots at the player, the problem is it only works for the turret that's closest to the player(you can place 2 turrets hence problem). This is my code and what i attempted to do to fix it but obviously it only works if your on the right of a turret that has another turret to the left of it within 64 pixels.

near=instance_nearest(x,y,obj_turret)
if instance_exists(near)
{
near2=instance_nearest(near.x-64,near.y,obj_turret)
}

if distance_to_object(near) <  320
{
if near.owner_id!=mplay_player_id(0)
{
////code
}}

if instance_exists(near)
{
if distance_to_object(near2) <  320
{
if near2.owner_id!=mplay_player_id(0)
{
////code
}}}


So in other words; i want it so two turrets that weren't created by the player and are in range can shoot at the player at the same time (also i don't want to have to create a second turret object or put that code into the turret object.)

Edited by Awesomeman, 09 November 2011 - 08:21 AM.


#2 filulilus

filulilus

    GMC Member

  • GMC Member
  • 928 posts
  • Version:GM:Studio

Posted 08 November 2011 - 03:24 PM

1. make a ds_list and store the distance between the turret and the players that the turret can hit.
2. sort the the list and get the 2 lowest values.
3. check if the values are smaller then 320 and then fire! :D
  • 0

#3 StallionTG

StallionTG

    GMC Member

  • GMC Member
  • 142 posts
  • Version:GM:Studio

Posted 08 November 2011 - 05:17 PM

Put this in the step event of the player object

global.pid = id;
with(obj_turret){
if distance_to_point(global.pid.x,global.pid.y) < 360 && global.pid.owner_id!=mplay_player_id(0){
////attack code
}
}


I'm not sure what mplay_player_id(0) does exactly, but assuming it matches the owner_id the turret will attack if its in range.

Also consider, that the turret will probably only attack 1 person at a time. In this case, where it ses ////attack code make a variable like "attack_target" and assign the global.pid variable to it. then use that variable "attack_target.x" and "y" to tell the bullets where to shoot.

Edited by StallionTG, 08 November 2011 - 05:17 PM.

  • 0

#4 Guest_Awesomeman_*

Guest_Awesomeman_*
  • Guests

Posted 09 November 2011 - 08:20 AM

Thanks for the suggestions, in the end i used StallionTg's code and it worked like a charm (after some tweaking).Posted Image

#5 StallionTG

StallionTG

    GMC Member

  • GMC Member
  • 142 posts
  • Version:GM:Studio

Posted 09 November 2011 - 08:12 PM

Thanks for the suggestions, in the end i used StallionTg's code and it worked like a charm (after some tweaking).Posted Image


Glad to hear it, Let me know when you post this in the works in progress forum.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users