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

GameMaker Quite new to coding and I need a bit of help with my RPG game any help would be greatly appreciated

B

blandanablandana

Guest
btw happy new year!
thank you, you too. I have it working now so the up direction and the down direction sprite stay consistent when you press up or down but the left and right ones change still change (Ill have to fix those now if I can). I'll do my best to make them work the same way and see if it is what you need. I might record a quick clip and post it on youtube also to show you what it looks like if you can implement it right and it works if I get it working just right.

It might be similar to something you already have but maybe it will be just slightly different.

If I get all four directions to have a "lock" for the sprites and get it to work I don't think you need to use a grid either, you can probably just adapt the code. (or I might simplify it if I get it working in the end)
I know your problem it's frustrating. There's no real tutorials on this "direction lock" thing.
 
Last edited by a moderator:
B

bhoyle753

Guest
hmm yes i know it is very annoying i was playing around with my code a while ago and managed to get the directions working great the bug only happens now when two diagonal buttons are literally pressed at the exact same time.
 
B

blandanablandana

Guest
I got it working awesome. I think it's what you need. It locks the direction and saves it on all four axis in your "major" direction.

I am very very happy with the result. I'll see if I can adapt it to be used without a grid.

I just tried it with the analog stick though and it doesn't get the same result.

Very unfortunate. Not sure how to fix that at the moment. I'll show you what I have soon though.
If you end up switching the controller movement to the dpad it will work with the controller though and you wont have to do any adjustment. I'm not sure if that's ok with you though.
 
Last edited by a moderator:
B

bhoyle753

Guest
I got it working awesome. I think it's what you need. It locks the direction and saves it on all four axis in your "major" direction.

I am very very happy with the result. I'll see if I can adapt it to be used without a grid.

I just tried it with the analog stick though and it doesn't get the same result.

Very unfortunate. Not sure how to fix that at the moment. I'll show you what I have soon though.
If you end up switching the controller movement to the dpad it will work with the controller though and you wont have to do any adjustment. I'm not sure if that's ok with you though.
Nice! sure i can see what i can do with it
 
B

blandanablandana

Guest
Nice! sure i can see what i can do with it
Ok great. I'll have to just post the version I have and then simplify it later so you don't have to make grids if it's possible.

I'll also try to figure out how to make it work with the analog. (It makes it so you have to get it -precisely- on 0,90,180,270 degrees in order for the sprite to change but at least we know what the problem is there if you really want analog control. I'm sure it's fixable)
 
B

bhoyle753

Guest
Ok great. I'll have to just post the version I have and then simplify it later so you don't have to make grids if it's possible.

I'll also try to figure out how to make it work with the analog. (It makes it so you have to get it -precisely- on 0,90,180,270 degrees in order for the sprite to change but at least we know what the problem is there if you really want analog control. I'm sure it's fixable)
yes indeed it isn't totally necessary to have analog control but it makes gameplay feel much more precise and enjoyable to me anyways
 
B

bhoyle753

Guest
and i dont know if i asked you this before but do you know how to add sprinting with a certain time duration?
 
B

blandanablandana

Guest
hmm darn. I did notice one thing it still plays the first idle frame in each direction. instead of skipping right to the first walking sprite on keypress. It's very close. I might have to just post what I have now and work with you on it later if i can.

sprinting seems like a good idea.
 
B

bhoyle753

Guest
hmm darn. I did notice one thing it still plays the first idle frame in each direction. instead of skipping right to the first walking sprite on keypress. It's very close. I might have to just post what I have now and work with you on it later if i can.

sprinting seems like a good idea.
aw thats unfortunate, and yeah sprinting do you know how to do it?
 
B

blandanablandana

Guest
Ok here is what I have. You may have to make a new project or copy your character so you can make a backup and have one you can delete the original code from and add your original code back if it ends up helping.


Code:
// create event


characterWalkSpeed=3;

// define x_frame in terms of sprite width and height 0 for first, 32 for second, etc.
x_frame = 0;

//y_frame 64 starts the character facing down (second box down in sprite sheet)
// adjust if you want a different direction on spawn
y_frame = 64;

// make sure this is the same as your starting sprite direction
// but I don't think it matters much 0=right 90=up 180=left 270=down
playerFacing= 270;


// using the sprite sheet, collisions get messed up for some reason, these are used to fix it
// the bottom of the draw event calculates this offset and replaces
// the sprite draw location to the proper player location (+ half sprite width and height)
playerCollisionAdjust_X=16;
playerCollisionAdjust_Y=16;
Code:
// step event

right_key = keyboard_check(vk_right) or (keyboard_check(ord("D")));
left_key = keyboard_check(vk_left) or (keyboard_check(ord("A")));
up_key = keyboard_check(vk_up) or (keyboard_check(ord("W")));
down_key = keyboard_check(vk_down) or (keyboard_check(ord("S")));



// define deadzone of gamepad_axis_value(controller_ID, horizontal axis) to be 0.2
// abs means absolute value (always positive if analog stick is tilted)
if (abs(gamepad_axis_value(0,gp_axislh)) > 0.2
 or abs(gamepad_axis_value(0,gp_axislv)) > 0.2)

{
    left_key = abs(min(gamepad_axis_value(0,gp_axislh),0))
    right_key = max(gamepad_axis_value(0,gp_axislh),0);
    up_key = -max(gamepad_axis_value(0,gp_axislv),0);
    down_key= -abs(min(gamepad_axis_value(0,gp_axislv),0));

 
 
}

hspeed = (right_key-left_key) * characterWalkSpeed;
vspeed = (down_key-up_key) * characterWalkSpeed;






// horizontal collision
if (place_meeting(x+hspeed, y, obj_wallBasic))
{
    while (!place_meeting(x+sign(hspeed), y, obj_wallBasic))
    {
        x = x + sign(hspeed);
    }
    hspeed = 0;
 
}


// vertical collision
if (place_meeting(x, y+vspeed, obj_wallBasic))
{
    while (!place_meeting(x, y+sign(vspeed), obj_wallBasic))
    {
        y = y + sign(vspeed);
    }
    vspeed = 0;

 
}
Code:
// draw event

var anim_length = 4;
var frame_size = 32;
var anim_speed = 7;



// down
if (vspeed > 0) and    abs(hspeed)==0
{
    playerFacing = 270;
    y_frame = 64;
    if right_key or left_key
    {y_frame = 64;}
}
if (vspeed == 0 and playerFacing == 270)
    {x_frame = 0}
 
// up
if (vspeed < 0) and abs(hspeed)==0
{
    playerFacing = 90;
    y_frame = 32;
    if right_key or left_key
    {y_frame = 32;}
}
if (vspeed == 0 and playerFacing == 90)
    {x_frame = 0}
 
 
//l eft
 if (hspeed < 0) and abs(vspeed)==0
{
    playerFacing = 180;
    y_frame = 96;
    if up_key or down_key
    {y_frame = 96;}
}
    if (hspeed == 0 and playerFacing == 180)
    {x_frame = 0}
 
// right
 if (hspeed > 0) and abs(vspeed)==0
{
    playerFacing = 0;
    y_frame = 128;
    if up_key or down_key
    {y_frame = 128;}
}
    if (hspeed == 0 and playerFacing == 0)
    {x_frame = 0}





//variables adjusting improper collision box
var xx = x-playerCollisionAdjust_X;
var yy = y-playerCollisionAdjust_Y;

// this draws a sprite "part" from the sheet using these arguments (sprite,subimg, left, top, width, height, x, y)
// x and y are replaced with the collision offsets because it messes up for some reason
draw_sprite_part(spr_player_grid, 0 , floor(x_frame)*frame_size, y_frame, frame_size, frame_size, xx, yy)

// 60 represents the frames per second of your game in anim_speed/60, change if different.
if (x_frame < anim_length +1) {x_frame += anim_speed/60}
else {x_frame = 1}


// delete the "//" from the line below this one to show the collision box of your character
//draw_rectangle_color(bbox_left,bbox_top, bbox_right, bbox_bottom, c_yellow,c_yellow,c_yellow,c_yellow, true)
Here is a .rar file I made which has the project I made
https://www.dropbox.com/s/0n4piu3drpy5jx7/bhoyleGame.rar?dl=0
Here is a png of the sprite grid, you just have to have it named "spr_player_grid" (without quotations) in your sprites and it should work.

Here is also a link to the dropbox I made for it where I uploaded the psd file as well so you can use it and your sprites will snap to the grid.
https://www.dropbox.com/sh/fdudw3q02k08dcf/AADnHMzFb_7mZgZby_ZSzaDha?dl=0

Hopefully this might help. I definitely put some effort into but I know it's not perfect.

As for sprinting, Ill try to make something that works and add it in a bit. It's not too difficult. Just figuring out how to make a timer that counts down on a key press is the tough part.
 
Last edited by a moderator:
B

blandanablandana

Guest
woops, I messed up the spelling in the variables on the "collision correction" but it still works.



**edit i changed just a few things that I forgot to delete or spell right but they weren't causing any errors, it was just a spelling thing. Let me know if this helps you at all. It's sooooooooo close. I wish it didnt play the idle frames. I'm sure we'll figure out a way to make it simpler or fix it though.
 
Last edited by a moderator:
B

bhoyle753

Guest
as for sprinting, i would like to later make a stamina bar so that when the stamina bar reaches zero or the player lets go of the sprint button the spd and image_speed returns to standard
 
B

blandanablandana

Guest
wow this is great, thanks a lot!
You are welcome, most of all I'm just really proud we could figure out the whole "direction lock" thing together and get it working in all four directions.
I haven't seen any tutorials out there so we might have sort of invented it. Thanks for your patience. I probably wont be able to help as much now but I'm going to try to make it a bit better or simpler if I can and see if I can help you with sprinting eventually.

it definitely has to do with making a variable and then counting it down on a keypress by like roomspeed/3600 (which usually gets me "1" unit per second at 60 fps).
 
B

blandanablandana

Guest
ok this looks like it works pretty well
I'm not sure if it glitches, I thought it did at first, just make sure to add the code I also posted two comments down at the end because there are times where the characters movement wouldn't stop after the timer gets to zero if you just paste in this.

it uses shift for sprint.

Code:
// create event
//add this variable to your create event

sprintTimer = 3;
Code:
// step event
// add this at the bottom of your step event
if (keyboard_check(vk_shift) and sprintTimer > 0)
{
    characterWalkSpeed = 6;
    sprintTimer -= room_speed/3600
}

if (!keyboard_check(vk_shift) or sprintTimer < 0)
{
    characterWalkSpeed = 3;
    sprintTimer += room_speed/3600 *(1/3)
}

if(sprintTimer >=3)
{
    sprintTimer = 3;
}
Code:
//DrawGUI
//optional, add a DrawGUI to your character
// and then add these lines so you can see your sprint timer
//you can also add it to your camera DrawGUI since it directly references the character

draw_set_colour(c_white);
// x, y, string  (usually put in drawGUI)
draw_text(20,20,"sprintTimer: " +string(obj_player.sprintTimer))

If you do all of that minus the camera thing you can see it pretty much works.

I'm not sure if it works if I just .rar the folder I made the project in but here is an updated version with what I have where the sprinting variable is displayed on the screen.
https://www.dropbox.com/s/oqskuss8h4ky6zl/bhoyleGame_withSprint.rar?dl=0

I'm not sure if I can fix the idle sprites really right now but that should probably be enough for now, thanks for your patience.
 
Last edited by a moderator:
B

blandanablandana

Guest
Ok I guess the code works for sprinting if you type it in the same but there are moments where it will get to zero and your speed wont stop.
It's pretty close. I think that's about all I can do for now. Thank you very much. I will try to get back to you in a few days if I can get those idle sprites fixed. Send me a message if you would like but I'm not sure I have much time to help too much more. Thank you very much it has been pretty fun figuring this one out.
 
B

blandanablandana

Guest
add this line to the bottom of the code I shared in your step event and it should fix any problems that you have about your character not slowing down at the end of the timer.

Code:
if (sprintTimer <= 0)
{
characterWalkSpeed = 3;
}
talk to you later friend, that's about all I can share for now, it's been pretty fun.
here is a link to the final version of the project I have at the moment.
until next time
https://www.dropbox.com/s/z6k8bfdfvdkfp00/bhoyleGame_jan_1.rar?dl=0

I'll have to work on my game for a bit definitely but I will do what I can to see if I can figure out something that works like this and doesn't play the idle sprites first. Let me know if you manage to figure that out in the meantime. That's the only thing really wrong with it. ttyl.
 
Last edited by a moderator:
B

blandanablandana

Guest
Good news. I just managed to fix it with experimenting and got it to play directly into the walking animation instead of playing the idle sprite first. Now everything is perfect. I'll upload it soon. I think you'll finally be happy with it. You'll just have to mess with the numbers to fit your animation or add special cases because the math I use is under the impression all of your up down left right animations are all the same length (which they probably should be though)
 
B

blandanablandana

Guest
ok this should work if you replace your step event in your charcter with this code (or just download the project from the link again) I only changed the very end where it is using the draw_sprite_part function and added a few if statements. The only thing that doesn't work is if you change the anim_speed to be too high. I think there is still a slight glitch when it gets to the end of the animations but it's not noticeable unless you turn up the anim_speed really high. It has to do with values having decimals or going over a limit. I'm pretty sure the farm video mentions a fix I'll have to find but it's pretty perfect now. very nice.


Code:
var anim_length = 4;
var frame_size = 32;
var anim_speed = 3;



// down
if (vspeed > 0) and    abs(hspeed)==0
{
    playerFacing = 270;
    y_frame = 64;
    if right_key or left_key
    {y_frame = 64;}
}
if (vspeed == 0 and playerFacing == 270)
    {    x_frame = 0    }
 
// up
if (vspeed < 0) and abs(hspeed)==0
{
    playerFacing = 90;
    y_frame = 32;
    if right_key or left_key
    {y_frame = 32;}
}
if (vspeed == 0 and playerFacing == 90)
    {x_frame = 0}
 
 
//left
 if (hspeed < 0) and abs(vspeed)==0
{
    playerFacing = 180;
    y_frame = 96;
    if up_key or down_key
    {y_frame = 96;}
}
    if (hspeed == 0 and playerFacing == 180)
    {x_frame = 0}
 
// right
 if (hspeed > 0) and abs(vspeed)==0
{
    playerFacing = 0;
    y_frame = 128;
    if up_key or down_key
    {y_frame = 128;}
}
    if (hspeed == 0 and playerFacing == 0)
    {x_frame = 0}





//variables adjusting improper collision box
var xx = x-playerCollisionAdjust_X;
var yy = y-playerCollisionAdjust_Y;

// this draws a sprite "part" from the sheet using these arguments (sprite,subimg, left, top, width, height, x, y)
// x and y are replaced with the collision offsets because it messes up for some reason
draw_sprite_part(spr_player_grid, 0 , floor(x_frame)*frame_size, y_frame, frame_size, frame_size, xx, yy)

// 60 represents the frames per second of your game in anim_speed/60, change if different.



if (abs(vspeed) or abs(hspeed)>0)

{    //prevents the idle animation from being played if the character is moving
    if x_frame ==0
    {x_frame = 1}
 
    //determines when the animation is finished (when it gets to a number 1 higher)
    if x_frame = anim_length+1
    {x_frame = 1}
    // animated the sprite when the character is moving (multiply by a number to increase speed)
    x_frame += anim_speed/60


}




//draw_rectangle_color(bbox_left,bbox_top, bbox_right, bbox_bottom, c_yellow,c_yellow,c_yellow,c_yellow, true)

here is the project with the fixed code although you can just copy and paste it for sure this time.
https://www.dropbox.com/s/lfw7jblercfp4ws/bhoyleGame_animationFix.rar?dl=0
 
Last edited by a moderator:
B

blandanablandana

Guest
ok i fixed the glitch...oh wait nm...Ill get back to you. Lol.. sorry.

****edit
Ok right on the glitch is gone, the farm video had the right information.

I'll post the last version for you in my next comment because I feel like we figured out everything we wanted and now the sprites work perfectly with your direction lock and also you have sprinting for 3 seconds with shift.
 
Last edited by a moderator:
B

blandanablandana

Guest
sorry I might have mentioned this isn't the step event actually if I said it was that I'm typing in, this is the draw event.
I reccomend just downloading the project now so you can see exactly what I have if you cant get it right but here is the final code.
There -might- still be an error but I think it's because I just drew the sprites sort of fast (it looks perfect on up and down) but I'm not entirely sure.
If you turn the anim_speed up now though the glitch that was happening is gone so its 99% the way there now if not finished.
here is the link https://www.dropbox.com/s/0bfq3n6rhnkc5ij/bhoyleGame_final.rar?dl=0

It sounds like this definitely helped a bit I'm glad. I think that's about all I can do though. This system should get you going if it works better for you in the end. Thanks for chatting.


I'll do my best to get back to you in a week or so and make something simpler. Most of what you should probably take away from this is the parts where is declares what direction the character is facing which is the top of the code. I wish it could be better.

Code:
// draw event
var anim_length = 4;
var frame_size = 32;
var anim_speed = 4;



// down
if (vspeed > 0) and    abs(hspeed)==0
{
    playerFacing = 270;
    y_frame = 64;
    if right_key or left_key
    {y_frame = 64;}
}
if (vspeed == 0 and playerFacing == 270)
    {    x_frame = 0    }
 
// up
if (vspeed < 0) and abs(hspeed)==0
{
    playerFacing = 90;
    y_frame = 32;
    if right_key or left_key
    {y_frame = 32;}
}
if (vspeed == 0 and playerFacing == 90)
    {x_frame = 0}
 
 
//left
 if (hspeed < 0) and abs(vspeed)==0
{
    playerFacing = 180;
    y_frame = 96;
    if up_key or down_key
    {y_frame = 96;}
}
    if (hspeed == 0 and playerFacing == 180)
    {x_frame = 0}
 
// right
 if (hspeed > 0) and abs(vspeed)==0
{
    playerFacing = 0;
    y_frame = 128;
    if up_key or down_key
    {y_frame = 128;}
}
    if (hspeed == 0 and playerFacing == 0)
    {x_frame = 0}





//variables adjusting improper collision box
var xx = x-playerCollisionAdjust_X;
var yy = y-playerCollisionAdjust_Y;

// this draws a sprite "part" from the sheet using these arguments (sprite,subimg, left, top, width, height, x, y)
// x and y are replaced with the collision offsets because it messes up for some reason
draw_sprite_part(spr_player_grid, 0 , floor(x_frame)*frame_size, y_frame, frame_size, frame_size, xx, yy)

// 60 represents the frames per second of your game in anim_speed/60, change if different.



if (abs(vspeed) or abs(hspeed)>0 )

{    //prevents the idle animation from being played if the character is moving
    if x_frame ==0
    {x_frame = 1}
 
    //determines when the animation is finished (when it gets to a number 1 higher)
    if x_frame == anim_length+1
    {x_frame = 1}
    // animated the sprite when the character is moving (multiply by a number to increase speed)
    if( x_frame+(anim_speed/60)<anim_length+1)
    {x_frame += anim_speed/60}
    else{x_frame = 1}
 


}




//draw_rectangle_color(bbox_left,bbox_top, bbox_right, bbox_bottom, c_yellow,c_yellow,c_yellow,c_yellow, true)
 
Last edited by a moderator:
B

blandanablandana

Guest
Hey I still think theres something slightly wrong with the code, it has a glitch you can barely notice when you're moving left and right for some reason even though it loops right now I really dont know if its a code thing. It should be possible to make it work without a grid which is what I would prefer. I ll talk to you in a week or so if I get a simpler version working. I appreciate you letting me paste all that code, I needed the practice.
 
B

bhoyle753

Guest
Haha, no problem and yeah this rpg is a real challenge, getting everything to work correctly is super hard! I will definitely use something out of all of these brilliant pieces of code you sent me. all the best! bye!
 
B

blandanablandana

Guest
hey I think I just got it working without a grid

Here is the final version I added space on the right side so the camera follows also if you walk far enough. This the best version of rpg movement I've ever made. I'm glad it works without a grid. It's so much easier. https://www.dropbox.com/s/ojes82u10c57h0t/bhoyleGame_Final_perfect.rar?dl=0
Code:
// step event
// sprite direction lock

// down
if (vspeed > 0) and    abs(hspeed)==0
{
    playerFacing = 270;
    obj_player.sprite_index = spr_player_down;
    if right_key or left_key
    {obj_player.sprite_index = spr_player_down;}
}
if (vspeed == 0 and playerFacing == 270)
    {obj_player.sprite_index = spr_player_down_idle}
 
// up
if (vspeed < 0) and abs(hspeed)==0
{
    playerFacing = 90;
    obj_player.sprite_index = spr_player_up;
    if right_key or left_key
    {    obj_player.sprite_index = spr_player_up;}
}
if (vspeed == 0 and playerFacing == 90)
    {obj_player.sprite_index = spr_player_up_idle;}
 
 
//left
 if (hspeed < 0) and abs(vspeed)==0
{
    playerFacing = 180;
    obj_player.sprite_index = spr_player_left;
    if up_key or down_key
    {obj_player.sprite_index = spr_player_left;}
}
    if (hspeed == 0 and playerFacing == 180)
    {obj_player.sprite_index = spr_player_left_idle;}
 
// right
 if (hspeed > 0) and abs(vspeed)==0
{
    playerFacing = 0;
    obj_player.sprite_index = spr_player_right;
    if up_key or down_key
    {obj_player.sprite_index = spr_player_right;}
}
    if (hspeed == 0 and playerFacing == 0)
    {obj_player.sprite_index = spr_player_right_idle;}
 
Last edited by a moderator:
B

blandanablandana

Guest
Here is the final project file in case you don't catch the link in my last comment. I think this one is solved. It doesn't glitch and it keeps your movement.
Finally perfect. https://www.dropbox.com/s/ojes82u10c57h0t/bhoyleGame_Final_perfect.rar?dl=0 Thanks friend.

Also, heres the sprites if you want to use them (or for anyone else reading this)
be sure to name the ones with 4 with _strip4 suffix when you import them (or _strip# however many frames you have if you use other sprites) and it will auto import the animations.



Moral of the story: just use the sprite editor and sprite_indexes instead of using a grid. It seems like it works better and it 100% easier.

On a side note, I think the version we made using the grid actually does work I just had the camera following a copy of the player that I hadn't put in the room so it was making a weird glitch with animations.
Sorry about that. I think I'd still prefer to do it without a grid personally. Good luck with your game.
 
Last edited by a moderator:
B

bhoyle753

Guest
Here is the final project file in case you don't catch the link in my last comment. I think this one is solved. It doesn't glitch and it keeps your movement.
Finally perfect. https://www.dropbox.com/s/ojes82u10c57h0t/bhoyleGame_Final_perfect.rar?dl=0 Thanks friend.

Also, heres the sprites if you want to use them (or for anyone else reading this)
be sure to name the ones with 4 with _strip4 suffix when you import them (or _strip# however many frames you have if you use other sprites) and it will auto import the animations.



Moral of the story: just use the sprite editor and sprite_indexes instead of using a grid. It seems like it works better and it 100% easier.

On a side note, I think the version we made using the grid actually does work I just had the camera following a copy of the player that I hadn't put in the room so it was making a weird glitch with animations.
Sorry about that. I think I'd still prefer to do it without a grid personally. Good luck with your game.
This is perfect, im glad there are people like you to help guide me through these challenges, thanks and good luck with your game too, i hope it goes as planned!
 
Top