GMS 2 Use a Car

D

Diorm

Guest
Hello all!
I'm trying to make a game where I can use a car.
I tried to use this tutorial:
But it doesnt seems to work in GMS 2 since I did exactly what he said.
When I press Spacebar, My character is teleported under the car, then I can still controle him and not the car.
Is this tutorial any good or is there a better way to do it?

Thank you!
 
Well, if you did exactly what he said, then it should work, so it seems that you didn't.

Post your code. People don't enjoy going looking through 10 minute tutorials just to reexplain them.
 
D

Diorm

Guest
So if you say that, that must mean you are sur that codes from an old version of gm work in gms2. Ok then what codes do you want to see first? The codes in the spr_player or the codes in spr_car ?
 
D

Diorm

Guest
My bad, what I meant was obj_player obj_car. Anyway here they all are:

obj_player
Create:
Code:
// Execute Code
alive=true
car=0
Step:
Code:
// Execute Code
if alive=true{
   visible=true
   if keyboard_check(vk_right){
       x += 5;

// Set Instance Scale
image_xscale = 1;
image_yscale = 1;

// Assign Variable
lewalk = 1;
   }
   if keyboard_check(vk_left){
       x += -5;

// Set Instance Scale
image_xscale = -1;
image_yscale = 1;

// Assign Variable
lewalk = 1;
   }
   if keyboard_check(vk_down){
       y += 5;

// Assign Variable
lewalk = 1;
   }
   if keyboard_check(vk_up){
       y += -5;

// Assign Variable
lewalk = 1;
   }
}else{
   visible=false
   x=car.x
   y=car.y
}
Key down Space:
Code:
// Execute Code
car=instance_nearest(x,y,obj_car)
if distance_to_object(obj_car)<64 && alive=true{
   alive=false
   car.alive=true
}
obj_car
Create:
Code:
// Execute Code
alive=false
Step:
Code:
// Execute Code
if alive=true{
   if keyboard_check(vk_right){
       x+=8
   }
   if keyboard_check(vk_left){
       x-=8
   }
   if keyboard_check(vk_down){
       y+=8
   }
   if keyboard_check(vk_up){
       y-=8
   }
}
Keydown space:
Code:
// Execute Code
if alive=true{
   obj_player.alive=true
   alive=false
   obj_player.x=x-32
   obj_player.y=y
}
 
Last edited by a moderator:
D

Diorm

Guest
Yeah I'm bumping this. I was kinda hoping someone over here could help me
 

darijs1

Member
That particular tutorial, though its good enough for people who understand gml enough to adapt it to their projects, is not the best in your case.
i recommend this tutorial instead:
it goes over a lot more than just getting in a car object and making the player follow it as you move it around.
As a matter of fact, even this tutorial isnt too great in this case, since it dosent go over how to get the player to get in and drive it.
You should familiarise yourself with basic gml and just make a bunch of attempts till you get it right.
Ofcourse this forum should be the place for asking help for specific problems, if there are any.
Im sure a bunch of people would gladly help.

No hard feelings, good luck.
 
Last edited:
D

Diorm

Guest
I saw that one too but since it's using one picture from a top down view I didnt want to use that since my character is not and it would look weird. It's really the part "Getting in the car" that is driving me nut (no pun intended). I'm trying to make a car with 8 ways directions (I already made the sprites).
It would look something like a Kunio-kun game (River City Ransom), with cars.
 
A

AndreDDSE

Guest
That particular tutorial, though its good enough for people who understand gml enough to adapt it to their projects, is not the best in your case.
i recommend this tutorial instead:
it goes over a lot more than just getting in a car object and making the player follow it as you move it around.
As a matter of fact, even this tutorial isnt too great in this case, since it dosent go over how to get the player to get in and drive it.
You should familiarise yourself with basic gml and just make a bunch of attempts till you get it right.
Ofcourse this forum should be the place for asking help for specific problems, if there are any.
Im sure a bunch of people would gladly help.

No hard feelings, good luck.
can someone please post the code from this tutorial? the video just doesnt load on my computer.
 

Ubu

Member
First of all, the code from that tutorial is terrible. The reason why it isn't working is because you check for space down in both objects so immediately after you've entered the car the space down check in the car object will throw you back out on the street. Having movement code in both objects isn't preferable either.

There are probably better ways to do it, but at least this should work better:

obj_player
Create
Code:
current_car = 0;
Step
Code:
if keyboard_check(vk_right)
   x += 5;
if keyboard_check(vk_left)
    x -= 5;
if keyboard_check(vk_down)
    y += 5;
if keyboard_check(vk_up)
    y -= 5;
if keyboard_check_pressed(vk_space)
{
   var car=instance_nearest(x,y,obj_car)

   if (distance_to_object(car)<64 && sprite_index = spr_player)
   {
       sprite_index = spr_car
       x = car.x
       y = car.y
       current_car = car;
       instance_deactivate_object(car);
   }
   else if (sprite_index = spr_car)
   {
       instance_activate_object(current_car);
       current_car.x = x;
       current_car.y = y;       
       sprite_index = spr_player;
       x -= 32;
   }
}

Then scrap all the code from the car object!
 
D

Diorm

Guest
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object obj_player:

Variable obj_player.spr_player(100003, -2147483648) not set before reading it.
 at gml_Object_obj_player_Step_0 (line 17) -    if (distance_to_object(car)<64 && sprite_index = spr_player)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_player_Step_0 (line 17)
Hmmm... It does this when I press SPACE near the car.
 

Ubu

Member
Either make sure your sprites are named spr_player and spr_car and that your car object is named obj_car or change the code to fit your naming.

Also, I will highly recommend you to go through the GMS2 tutorials. They are good and will prevent lots of frustration down the line. Programming is not about copying code, but understanding what you're doing:
https://www.yoyogames.com/learn
 
Hahahaha, 2 years later and i see this, first of all the code he wrote in the video is not good and has some small mistakes. @Ubu you did a great job and now my code is working :D thanks
Either make sure your sprites are named spr_player and spr_car and that your car object is named obj_car or change the code to fit your naming.

Also, I will highly recommend you to go through the GMS2 tutorials. They are good and will prevent lots of frustration down the line. Programming is not about copying code, but understanding what you're doing:
https://www.yoyogames.com/learn
 
Top