Need Game Maker Advice, Please Help!

K

kiodoko

Guest
Hi I'm new. I am creating my first game with the newest game maker program. I cannot for the life of me get one object to move towards another. This should sound really simple, but for some reason it won't work no matter what I try. I'm creating a real time strategy game and I'm trying to get a soldier to move to a way point. What I'm trying to do is: make a soldier_object and make an event that activates when the enter button is pressed. Then inside the event, I use the set point direction drag and drop command to try and get the soldier to move towards the way point. I even tried using the game maker language code approach and used four commands and none of them worked.

such as:

move_towards_point( way_point_object.x, way_point_object.y, 4 );

that:

direction = point_direction(x,y,way_point_object.x,way_point_object.y);

that:

if instance_exists(obj_player)
{
move_towards_point( way_point_object.x, way_point_object.y, 4 );
}
else instance_destroy();

and that:

if instance_exists(way_point_object)
{
var dir = point_direction(x, y, way_point_object.x, way_point_object.y);

{
speed = 4;
direction = dir;

Please help this is really giving me the blues.
 

Attachments

jonjons

Member
I should be very easy with all the video tutorials available, you can try these commands.

if ( instance_exists(obj_player) ) //----moves towards player
{
move_towards_point(obj_player.x, obj_player.y, 2);
image_angle = direction;
}


//---------------------//------------------------

var mButton = mouse_check_button_pressed(mb_right);
if ( mButton )
{
move_towards_point(mouse_x, mouse_y, 2);//---mouse-right-click-and-moves were the mouse is
image_angle = direction;
}
 

JackTurbo

Member
Hey man.

Sounds like you're pretty new to both game maker and programming in general. If that is the case I'd really caution against trying to make a strategy game as your first project. Strategy games are pretty complicated beasts and are often pretty data driven so a lot of the coding involved can be a little abstract and isn't really all that newbie friendly.

I'd recommend trying something super simple to start with, similar in scope to classic arcade games (pong, space invaders, pacman etc). I know you've probably got an idea of the kind of project you want to make, but setting that to one side while you develop your skills with gms/gml will only increase your odds of making something you're proud of, when you come back to it.
 
D

Drepple

Guest
I agree with JackTurbo, but moving in a direction is something you'd also be using in simple games like pong, so you should know how it works.
Like jonjons said, you could move towards a point, but you can also move towards an object or in the direction of that object (which is all the same thing really, but the last one will allow for more control)

This can be done by getting the direction and then moving towards it. In the unit's step event, put this to get the direction from your own x and y to the target's x and y:
Code:
var dir = point_direction(x,y,obj_target.x,obj_target.y);
Then, to move towards it you could use:
Code:
motion_set(dir, speed);
If you want more control you can set the individual x and y speed:
Code:
x += lengthdir_x(speed,dir);
y += lengthdir_y(speed,dir);
 
This is generally a quite straightforward thing to implement, so if it isn't working for you it may be because of not understanding the difference between objects and instances.

Say I have an object called "obj_way_point", and there is only one instance of it in a room. I can use the object within commands, such obj_way_point.x, and it will be treated "correctly". However, that is only because there is no other instance of it within the room.

Get more than one instance of it, and you need to start referring to specific instances - rather than the object.

var inst = instance_nearest(x, y, obj_way_point);
direction = point_direction(x, y, inst.x, inst.y)
speed = 2;

Would work because it has an instance id from instance_nearest. inst.x / inst.y are now getting the specific x / y of that instance.


direction = point_direction(x, y, obj_way_point.x,obj_way_point.y)
speed = 2;

Won't work, at least insomuch as intended, because it will do (I think) the last instance of obj_way_point that was created in the room. Regardless of which one it actually finds, the results are not at all reliable.
 
Top