GML Player to Tank

  • Thread starter Captain_ArrowWound
  • Start date
C

Captain_ArrowWound

Guest
I am trying to make it so that the player can switch from the player character to a tank when they are within 100 pixels of a tank by pressing the E key. I pretty sure that I would have to use a state machine. I have watched a couple of tutorials on they but I can’t get them to work. Is there any suggestions for what to do?
 

TheouAegis

Member
If the tank is already a separate object, have a variable in it like driving=0. When the player interacts with it, set driving=1. In the step event, check if driving==1. If it does, dectivate the pl@yer, and let the tank's movement controls take over.
 
S

Sabrina Stoakes

Guest
Several ways actually :)

One way would be to deactivate the player object, but if you exit the room with the character deactivated then it will stay in memory. If you're using views that follow the player and decide to deactivate the player then you may have to add a check in front of your view code to check like, "if instance_exists(player)" and another check to change the view, but this time say, "if not instance_exists(player)" So, something like this:
Code:
///Follow Player
if instance_exists(player) {
//Set view to follow player
}

///Follow Tank
if not instance_exists(player) {
//Set view to follow tank
}

If you have your view set to follow the player like I assume you might have, then you could have a global variable called, "global.intank" or something like that. Over the controls for your character you could add a check to see if that global variable = 1 or not and if it equals 0 then you can use your player controls, but if it equals 1 then you can use then. Now, this is where you get to be lazy. If you have a view set on following your player, then change your player sprite to something invisible with the x and y centered, then have your player's x & y set to the tank's x & y.

Might be more complex than what you're looking for.
 
C

Captain_ArrowWound

Guest
I have tried to set up something like this by creating the variable playerlocation. If it is equal to 0 the player should be able to move around as the player and if it is equal to 1 the player should move around as the tank, but instead when the player goes up to the tank and presses E they go into the tank but the tank wont move. I have tried a few different things but if one part works, another part wont work. Here is my code. Thanks for the help.

obj_tank-create


depth = 0;

playerlocation = 0;

allow = 1;
shoot = 0;
reload = 90;
up = 0;
tankbulletspeed = 12;
length = 37;

image_speed = 0;

obj_tank-step

if (playerlocation = 1)
{
sh = mouse_check_button_pressed(mb_left);

if (allow = 1)
{
pointdir = point_direction(x,y,mouse_x,mouse_y);
image_angle += sin(degtorad(pointdir-image_angle))*3

if (shoot == 0)
{
up += 1;
if (up >= reload)
{
shoot = 1;
}
}
if sh
{
if (shoot == 1)
{
sprite_index = spr_tankturretfire;
shoot = 0;
up = 0;
tankbullet = instance_create_layer(x + lengthdir_x(length,image_angle),y + lengthdir_y(length, image_angle),"Bullets",obj_tankbullet)
tankbullet.direction = image_angle;
tankbullet.speed = tankbulletspeed;

}
} else
{
sprite_index = spr_tankturret;
}
}
//End for Playerlocation
}
if (playerlocation = 1)
{
sh = mouse_check_button_pressed(mb_left);

if (allow = 1)
{
pointdir = point_direction(x,y,mouse_x,mouse_y);
image_angle += sin(degtorad(pointdir-image_angle))*3

if (shoot == 0)
{
up += 1;
if (up >= reload)
{
shoot = 1;
}
}
if sh
{
if (shoot == 1)
{
sprite_index = spr_tankturretfire;
shoot = 0;
up = 0;
tankbullet = instance_create_layer(x + lengthdir_x(length,image_angle),y + lengthdir_y(length, image_angle),"Bullets",obj_tankbullet)
tankbullet.direction = image_angle;
tankbullet.speed = tankbulletspeed;

}
} else
{
sprite_index = spr_tankturret;
}
}
//End for Playerlocation
}

obj_player-create


depth = 100;

playerlocation = 0;
playerspeed = 2.5;

hp = 3;

cooldown = 0;
collisionspeed = playerspeed + 2;

obj_player-step


//Player starts by controlling the player
if (playerlocation = 0)
{
//Movement
if (keyboard_check(ord("W"))) and place_free(x, y-collisionspeed)
{
y = y-playerspeed;
}

if (keyboard_check(ord("S"))) and place_free(x,y+collisionspeed)
{
y = y+playerspeed;
}

if (keyboard_check(ord("D"))) and place_free(x+collisionspeed,y)
{
x = x+playerspeed;
}

if (keyboard_check(ord("A"))) and place_free(x-collisionspeed,y)
{
x = x-playerspeed;
}

//Shooting
if (mouse_check_button(mb_left)) and (cooldown < 1)
{
instance_create_layer(x,y,layer,obj_bulletplayer);
cooldown = 100;
}

cooldown = cooldown - 1;

//Death

if (hp <= 0)
{
game_restart();
}
//end for playerlocation
}
 
C

Captain_ArrowWound

Guest
The aiming code for the tank is in the turret.
Here is the code for the turret

obj_turret - create

depth = 0;

playerlocation = 0;

allow = 1;
shoot = 0;
reload = 90;
up = 0;
tankbulletspeed = 12;
length = 37;

image_speed = 0;


obj_turret - step

if (playerlocation = 1)
{
sh = mouse_check_button_pressed(mb_left);

if (allow = 1)
{
pointdir = point_direction(x,y,mouse_x,mouse_y);
image_angle += sin(degtorad(pointdir-image_angle))*3

if (shoot == 0)
{
up += 1;
if (up >= reload)
{
shoot = 1;
}
}
if sh
{
if (shoot == 1)
{
sprite_index = spr_tankturretfire;
shoot = 0;
up = 0;
tankbullet = instance_create_layer(x + lengthdir_x(length,image_angle),y + lengthdir_y(length, image_angle),"Bullets",obj_tankbullet)
tankbullet.direction = image_angle;
tankbullet.speed = tankbulletspeed;

}
} else
{
sprite_index = spr_tankturret;
}
}
//End for Playerlocation
}
 
TheouAegis isn't saying show them the aiming code, they are pointing out that you do not have any movement code for the tank. If you want the tank to move, you have to have the movement code in the tank as well as the player. The game doesn't magically know you want the movement code in the player to apply to the tank, you have to duplicate the code (probably better to put it in a script, but whatever) and then have BOTH the movement and the aiming code in the tank object, inside an if (playerlocation == 1) statement.
 

TheouAegis

Member
Yes, here is our help:

Put in movement code inside the tank object. You have no movement code for the tank that we can see, so obviously it's not going to move!

Or if you mean the turret doesn't even turn, then say so.
 
C

Captain_ArrowWound

Guest
Oh I put a movement for the tank but still seems to be not working. Thanks for helping


if (keyboard_check_released(ord("E"))) and (distance_to_object(obj_player)<=100) and (playerlocation = 0)
{

playerlocation = 1;
object_set_solid(obj_player,false)
}

if (playerlocation == 1)
{


//Turning left
if keyboard_check_direct(ord("A"))
{
image_angle += 2;
direction += 2;
}
// Turning right
if keyboard_check_direct(ord("D"))
{
image_angle -= 2;
direction -= 2;
}
//Moving Foward
if (keyboard_check_direct(ord("W"))) and (place_free (x +tankspeed*cos(direction*pi/180), y - tankspeed*sin (direction*pi/180)))
{
x = x + tankspeed*cos(direction*pi/180);
y = y - tankspeed*sin (direction*pi/180);

obj_tankturrent.x = x;
obj_tankturrent.y = y;
}

//Moving backward
if (keyboard_check_direct(ord("S")))
{
x = x - tankspeed/3*cos(direction*pi/180);
y = y + tankspeed/3*sin (direction*pi/180);

obj_tankturrent.x = x;
obj_tankturrent.y = y;
}
obj_player.x = obj_tank.x;
obj_player.y = obj_tank.y;

//End for playerlocation
}
 

TheouAegis

Member
Do not use object_set_solid(). Just do obj_player.solid=false. This is most likely the reason why it's not moving. Change that first. The next suggestion below is just a speed consideration for you.

You can use dcos(direction) and dsin(direction) instead of converting to radians.
 
C

Captain_ArrowWound

Guest
Thanks guys I think I almost got it now but I am just having problem. I switched the variable playerlocation to a global variable and now it will move. But it will move for the one frame after the E key is pressed and then not move. So I think somehow the variable changing back to a zero as it was set as and staying as the 1 which controls the tank but I have no clue why it is doing this.
 

TheouAegis

Member
Maybe i am missing it, but where are you setting playerlocation BACK to 0 when he tries to exit the tank? Or can the player even exit the tank? I ask because often times this issue is one object reads the key pressed and sets the variable, then another object is STILL able to read the same key press and sets the variable back.
 
C

Captain_ArrowWound

Guest
I think at the same time that the player was entering the tank somehow the player was also leaving the tank with the same key press. So I added a variable as a timer so both operations would not be run at the same time. It works great now and thanks for all the help. This helped me learn a lot about gamemaker. Also congrats on 6000 posts.
 
Top