• 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!

Turret shooting the lead enemy

J

JCrockford1

Guest
I need my turret to shoot the leading enemy in my Tower Defence game, however, when the bullet collides with the enemy all the enemies on the screen is destroyed. Here is the code in my obj_Tower step event:
///Rotate with Enemy and shoot
if(instance_exists(obj_enemyPar))
{
enemyid = 0;

for(var i = 0; i < instance_number(obj_enemyPar) i++)
{
var enemy = instance_find(obj_enemyPar,i);
var enemyDistance = point_distance(x,y,enemy.x,enemy.y)

if(enemyDistance <= range)
{
if(enemyid==0) enemyid = enemy

var pos = enemy.path_position
if (pos > enemyid.path_position) enemyid = enemy

}

if (enemyid != 0)
{
dirOfE = point_direction(x,y,enemyid.x,enemyid.y)
image_angle = dirOfE

if (canShoot)
{
instance_create(x,y,obj_bullet)
canShoot = false
alarm[1] = reloadSpd
}
}
}
}
I cannot find the problem but hopefully one of you will.
 
D

DarthTenebris

Guest
Dat problem again :p
I'm happen to be working on a tower defense game as well, so might as well share some of the code with you :)
Here's how I did it:

Your shooter's create event:
Code:
target = undefined;
range = some_value; //experiment with this value
canShoot = true;
Your shooter's step event:
Code:
if (target == undefined) {
     target = instance_nearest(x,y,obj_enemyParent);
}

if ((distance_to_object(target) <= range) && (instance_exists(target))) {
     //to face the enemy; leave it if you don't need it
     image_angle = point_direction(x,y,target.x,target.y);
     
     if (canShoot == true) {
          canShoot = false;
          with(instance_create(x,y,obj_bullet)) {
               origin = instance_nearest(x,y,obj_your_object_that_fired_this_bullet);
               move_towards_point(origin.target.x,obj_target.y,bullet_speed;
          }
     }
} else {
     target = undefined;
}
Your bullet's create event:
Code:
origin = undefined;
bullet_speed = some_other_value; //experiment with this value too
Hope I helped you enough :D
 
J

JCrockford1

Guest
The target variable that would be the enemy parent right?
 
J

JCrockford1

Guest
So I would put it as target = obj_enemyParent in the create event?
Also, what do you mean by undefined? It's a known function in GamesMaker so I am wondering what does it mean in this context?
 
D

DarthTenebris

Guest
No, you don't. Just follow my code to get it to work, but if you happen to be curious as how the code works (which is great if you are, since you'll try to understand how the code works instead of just copy-pasting code and not knowing what it does), it is in the step event - the variable target will have two states, keeping track of an enemy id, or waiting to be filled with an enemy id. In the step event the code:
Code:
if (target == undefined) {
     target = instance_nearest(x,y,obj_enemyParent);
}
will check if the turret has a target locked, if not, search for one. If it can find one, the proceed, otherwise, wait until it finds one, then proceed.
The variable needs two states and must be initialized in order to be checked for, and by default it wouldn't have an enemy locked on yet, so in the create event it is initialized by the statement:
Code:
target = undefined;
Looking back at the step event code, since it is undefined, the turret will attempt to lock on an enemy, and shoot.

As for the variable undefined, it is a reserved keyword used by game maker studio. Information on it can be found on the manual: undefined (second to lowest point; scroll down to the maximum)

I hope I have satisfied you curiosity :)
 
J

JCrockford1

Guest
Yeah I tried to put a variable on it haha thank you though!
 
J

JCrockford1

Guest
I've got this fatal error:
FATAL ERROR in
action number 1
of Step Event0
for object obj_tower:

Variable obj_tower.target(100017, -2147483648) not set before reading it.
at gml_Object_obj_tower_StepNormalEvent_1 (line 38) - if (target == undefined) {

However, I have followed the code that you gave to me.
 
J

JCrockford1

Guest
Super confused still. Can someone show me a template on how that is supposed to look like? Sorry, I'm a beginner with GamesMaker.
 
E

Ethanicus

Guest
Why aren't you doing this in the Collision Event? This would be much easier.
 
Top