In this video I demonstrate a technique to achieve the gird based movement seen in many JRPG games, tactics games, etc. Twitter: / gmwolftuts Facebook: / gmwolftuts
This is a very nice video, I have learnt a lot from it. I love the format too - finally a tutorial that isn't too slow, nor too long. Thanks a lot man, that's definitely a subscribe!
Good tutorial, I have been trying to figure out this on my own but didn't manage to get that complex, you have enlighten me and given me a starting point, thanks.
Thanks. Next, consider some videos on 3D. I am currently almost finished with a Rubik's Cube composed of prims. It is almost functional. One future project I'm thinking of is to re-create the opening stage of the SNES version of Star Fox. It would very fascinating to see a re-creation run at 60fps in HD. Just a thought.
Looking into crafting a system similar to the South Park games where distance from the camera changes perspective a little bit. I think this will help and then applying an isometric view but making the player think it’s a 2-D room..
Amazing video, really helped me! I was just wondering how the collision test could be adapted to stop the player moving through certain objects too? I know this may be obvious, but I've just started with gamemaker and any help would be really appreciated!
Sorry, tell me how you can implement the execution of the action on the animation frame, for example, the script "blow with the sword" should be called on frame 3, before that I used an integer currentFrame, now it is fractional. Perhaps it is worthwhile to call the script not in the frame but after some time since the beginning of the animation? how best to implement it? Forced to use lerp in order to have the possibility of growth of character skills, movement speed, attack speed, and so on, without float values or how not to do. Sorry do you have an e-mail?
This is awesome! Exactly what I was looking for. I did have a question though. If I'm next to a wall, but not facing towards it, is there a way to make the player character look towards the wall? For example, if I'm there is a wall to my left, and I'm facing down currently and press left, is there a way to make the character face left while being stationary other than the animation change?
great tutorial but the collision isnt working on mine, my character gets stuck as soon as it enters the level. Im using my own assets but I cant see any reason why it shouldnt be working. Maybe something to do with collision masking?
This is a wonderful tutorial. I have some programming experience but am new to GameMaker Studio 2, and this was a great lesson in a variety of functions. Not sure if you still check these comments, but I did have one question. Some games alternate animations every other step. First step would be: idle, left foot, idle. Second step would be: idle, right foot, idle. And so forth. Rather than playing all animation frames with each step. (Final Fantasy VI is a great example.) If I wanted to do something similar in GMS2, would I need to create two separate sprites and alternate between them? What would be the best method in your opinion? Thanks!
I would keep track of what step to perform next. Then use a single sprites with both animations. If I recall correctly GM will actually automatically remove duplicate frames and handle that for you. But if you like to be explicit, you could keep an array of image indices for each animation.
@@GMWolf Thank you for your reply! Could you illustrate for me what this index would look like? I've been studying the example of "frames = [...]" from your tutorial. But I'm not sure how to translate this into a system that alternates between two sets of animations depending on which step comes next. Thank you again.
You would have two arrays like so: left_step = [...]; right_step = [...]; Then also have a variable keep track of what step to do: step_type = 0; //0 for left. 1 for right. Then when you take a step: if (step_type == 0) { //Use left_step } else { //Use right_step } When you end a step: if (step_type == 0) { step_type = 1; else { step_type = 0; }
Hello! Very nice tutotorial. Only problem I'm having is with the collision. Going left lets you go very close to the wall, but right keeps you farther away.
Is the distance more than a tile? If so it could be you are checking for collisions multiple tiles away. Otherwise it could be that your sprite origin needs adjusting.
If it is just the sprite origin you just have to adjust it. Sprite origin and offset will be something to consider no matter which tile collision system you use.
So I'm having an issue with trying to get the collisions to work right on my game. Instead of the wall tiles stopping movement, my player object keeps moving, and passes through the walls. I don't understand what I'm doing wrong, and need help figuring it out. My current solution is to try and set the state to idle, after I set delta time/1000000 to be multiplied by 0, rather than 2, the default speed I've chosen in for the object, however, this results in the object still moving, and they still pass through the walls, which I've made objects for.
@@GMWolf I tried =/ but not success ,I looked for tutorials and I can't find anything
3 роки тому+1
Hello! Nice tuto! I Have a question. I do exactly that you show. But when I try start the game, I have an error in move script: state is not defined. But works if I put the content of move script directly in key down - right event.
Amazing tutorial! I am trying to make a Pokémon fan game using sprites from the GBA games (Fire Red / Leaf Green). I had a few questions: I might just be dense, but I am not sure how I would increase the speed to match the actual Pokémon games? I didn't see an obvious variable in which to do so. Also, I am having some trouble lining up the animations with the sprites I have (3 frames per direction like yours, but it doesn't look right lol) any tips?
Hi. Iirc this tutorial uses a time based approach, where each step takes a certain amount of time to cross. You can increase the movement speed by decreasing that time. Alternatively you can increase the time that gets accumulated in the t variable.
As for the animation, it will depend on the frames you have. I think the sequence I have matches the Pokémon sequence too but I could be wrong. One alternative is to ditch the custom array and just do it in the sprite editor. GM will remove duplicate frames for you.
Thank you for this tutorial. I have never coded before, but following along definitely gives good experience with a number of different components of Game Maker. I am getting an error when I try to run the game which states that I am "trying to index a variable that is not an array". The error message tells me it's related to line 5 of the move script. I've looked, but I can't find what I've done wrong. Any idea where I might have made a mistake that would give me the error message I've described?
Hi, glad you like the tutorials! The error you get is because somewhere, you typed something[aVariable]. This is used to access arrays. So if "something" isn't an array, you will get an error.
Thanks for the reply! I initially thought there might be a problem with the "dir" variable, but if I understand you correctly it seems the issue might be with my global.components array. I've set up the gml_pragma line as instructed, but I have to imagine something about what I've typed is incorrect. I'll keep looking. Worse comes to worst I'll start over and try again - would be good to get the extra practice in anyway. Thanks again.
If you need help, I suggest heading over to the GMC at forum.yoyogames.com If you make a post with your code explaining the issue, there are a lot of people willing to help, myself included.
Its been a month, so you definitely probably figured this out, but for anyone else looking to figure out this problem I may have a solution since it happened to me too. at the ~~~global.components = []~~~ section I typed "=" after global.components for the next four lines of code. Mine originally; global.components = []; global.components = [directions.right] = [1, 0]; global.components = [directions.left] = [-1, 0]; global.components = [directions.up] = [0, -1]; global.components = [directions.down] = [0, 1]; the proper way; global.components = []; global.components [directions.right] = [1, 0]; global.components [directions.right] = [1, 0]; global.components [directions.right] = [1, 0]; global.components [directions.right] = [1, 0];
My player, who is 16 wide, will not move through a 16x16 hallway space, they get stuck :( Also, I am looking for the best way to add that when you press a direction, even if you are up against a wall, you will turn that direction (in the idle state)
Hmmm, the collision detection piece does not seem to function for me. Has something changed since this tutorial was published? I have placed tiles on the Walls layer but still my character passes right through them.
Hard to tell what the issue could be. As far as I'm aware the code should still be working fine. Why not head to Reddit or the GMC (forum.yoyogames.com) and post the relevant code along with the question. Plenty of people there to help. If you mention me on the GMC (@GMWolf) then Ill get a notification and can give it a look.
When creating the tilemap variable, my tilemap id is -1 according to the debugger and any time I use tilemap_get to get the tile at an x/y coord it returns -1 as well. It's in the create script for my player object. Do you know what I could be doing wrong?
Thanks for the response. I double checked and the layer name matches what it shows on the room1 tab. For reference, the following is what I used, and t1 and t2 below are the variables I used to test in the debugger since I have a wall tile at 0, 0 but not at 1, 1: var tile_layer_walls = layer_get_id("walls") var tile_map_walls = layer_tilemap_get_id(tile_layer_walls) t1 = tilemap_get(tile_map_walls, 0, 0) t2 = tilemap_get(tile_map_walls, 1, 1)
Hmmm, at this point I would recommend heading over to the forums (forum.yoyogames.com) and post your code there, myself and others will be able to better help you there.
I really appreciate the time you spent even to make this video. After a week of trying other people's codes and my own combination of codes, I came across this. It has helped, but, I still can not accomplish certain things. I am trying to rebuild "Sabin's House" from FF6 as a learning experience. It is a pretty small area. In the real game, when the player moves in front of a particular plant (which covers about 15 pixels height, across two tiles), the player covers the plant. (the players 24 pixels tall). When the player goes behind that same plant, the player is covered. I am not sure how I can manage the collision detection to make it so you can't walk through the plant while simultaneously being capable of standing on either of the two tiles that have the plant drawn on them (you can walk in from left or right, but, not up or down) AND at the same time have the plant sprites render correctly, above or below the player... Do I need to set up multiple layers and collisions and activate / deactivate them as the player passes certain areas of the map? Maybe detection based on object would help instead? Before I started using your code, I noticed with place_meeting I could "accidentally" slip the player into occupied squares, while I was using an object that contained a sprite less than my grid size (like a 4x4 in a 16x16 grid), successfully blocking me from coming in from certain directions. ALSO how can I switch the walking animation every other time you command the player to walk? I noticed in the game, the walking animations alternate every time you let go of walk, and continue walking again. you will start with right hand, then if you let go and start again, you will start with left hand and so on. Hope that made sense :) please help, I am up till 2 in the morning every night after work, till I fall asleep on my keyboard and feel like a zombie. I may post on game maker forums in a bit as suggested below, but, right now I need a 15 minute break.
Then you will need a more complex way to check collisions. A good starting point is to consider the tiles the player is already occupying, and where they will end up if he moves.
Hi , just wanted to know , how to add the sprint functionality , i change the walk_anim_length when sprint key is pressed , the player move fast but it make some jerk like movement. can you help me on this . thankyou
It's probably because we reset the animation time to 0 for every grid step. What we should be doing is carrying over any excess when we get to the end of one step, into the beginning of the next.
+@@GMWolf Can you elaborate further on this, please (allowing for collision checking with objects, that is)? I've been trying to figure out how exactly to do this, but I've been having trouble with it. I've tried a number of ways to get it to work, but the current potential solution I've been working with is simply adding another if statement to the Move script in-between checking the "Walls" layer and the actual code that allows the player to move that looks like this: if !(tilemap_get(tile_map, x_pos + dx, y_pos + dy)) { if (position_empty(x_pos + dx, y_pos + dy)) { x_from = x_pos; y_from = y_pos; (So on and so forth) The issue that I'm having is that the coordinates provided for position_empty are never actually detecting if an object's collision mask is in that spot. It works if I give it direct coordinates on or off an object in the world, though. So I'm not entirely sure what's going on with it.
-Update- I've actually found a solution to this, although it seems as though it has the potential for bugs, so I'm working on seeing if it could cause issues with certain situations. But for now, here's what I've got so far. Sorry about the indentation as well, you can thank UA-cam for butchering that. (Additions to "MACRO" in bold. Note: The variables are global mostly for debugging purposes. gml_pragma("global", "MACRO()"); #macro tile_width 16 #macro tile_height 16 *globalvar directionX;* *globalvar directionY;* *directionX = 0;* *directionY = 0;* enum directions { up, left, down, right } global.components = []; global.components[directions.up] = [0,-1]; global.components[directions.left] = [-1,0]; global.components[directions.down] = [0,1]; global.components[directions.right] = [1,0]; (Additions to "move" in bold.) var dir = argument0; var components = global.components[dir]; var dx = components[0]; var dy = components[1]; *if (dir == directions.left)* *{* *directionX = ann.x - 16;* *directionY = ann.y;* *}*
Good afternoon, thank you for your contribution to the development of the GM community and interesting examples. I have a question, I did everything according to your lesson, but in GM 8.1, strange things happen because of the binding to deltaTime :) If we add the condition image_index = 2 (we will not go deep into the details) add instance_create (in general, any less costly) action) the character abruptly moves to the end of the animation. What can it be connected with? Perhaps it is necessary to refuse deltaTime and use other method?
no matter what i do, i get this error: DoConv :1: illegal undefined/null use at gml_GlobalScript_move (line 2) - var components = global.components[dir]; ############################################################################################ gml_GlobalScript_move (line 2) cant figure out why. afaik i copied you exactly. i feel like it has to do with gm's recent update thing
If you are using the new GML (GMS2.3+) make sure you are using functions correctly. Writing scripts directly without wrapping them in functions isn't valid anymore.
Sorry for commenting here on this incredible old video, but I'm having the stuttering issue a couple people have mentioned in the comments, only instead of happening at every tile, it seems to be happening every frame. I tried making a post on the GMC forum about this, but they don't make posts visible until moderators accept them. Any idea as to what this could be? If need be I can show the code.
Every frame? That is quite odd. I can't recall any possible issues outside the tile->tile transition. Does the stutter happen with any other animated objects? It's hard to discuss code in yt comments. What forum subsection did you post to? AFAIK only the tutorials section needs moderator approval.
@@GMWolf Wow, you responded really fast! I'm assuming it's every frame because of how consistent it is, but it's just very common, so I can't say for sure. All sections need moderator approval for me, since I had made an account to post this, and the first 5 posts on a GMC forum account require moderator approval. I tried other animated objects, and it's just this object, so it's definitely not lag. Is there any good way for me to send my code to you that isn't youtube comments?
I figured it out, I had messed with viewports and it made it look like it was shuffling back and forth when in reality it was just the camera not keeping perfectly on center with the character. Sorry about that!
Fantastic Tutorial, I have it all up and running perfectly and smoothly in my initial room, but once I transition to the next room it no longer detects the tile layer, and the collisions stop working, do you know of any way to fix this? It is not a naming issue as all the tile sets and tile layers are named exactly the same as the previous room. If anyone has any ideas please let me know! Thanks again :)
I think you would need to get the layer id and tilemap id again because you changed room. I would make those actual variables in the player object and in your transition object, re assign those variables to what's in the room you are going to.
What I did to fix this problem, was move: var tile_layer = layer_get_id(“Wall”); tile_map = layer_tilemap_get_id(tile_layer) out of the create event of the player, and into the step event of the player. This way it checks every step of the game, instead of just at the beginning of its creation
Also, could you let me know how you got the room transition working properly? Mine works for the most part, but it puts the player at the wrong x and y coords.
Alright got this system down, just wondering if theres a way I can set a movement speed as I move within the tiles? With just this code alone it makes the player move too fast.
Cool! Does anyone have an idea how to implement this for non-player objects, like enemies and the like? Specifically I'm trying to do something turn-based.
The move functions that initiate the movement are still the same. You just have to call them in different places. For a Player object you call it on keyboard press events. For NPCs you could do it on a timer (for Pokémon style random movement) or when it's their turn for a turn based game.
The #macro syntax is the new way of defining macros. You can still do it that way : docs.yoyogames.com/source/dadiospice/001_advanced%20use/010_macros.html But note this tutorial was really designed for the new tilemap system that doesn't exist in 1.4
I am so confused on all these videos I watch for GMS2 everyone says it works perfectly but everytime I emulate these videos I get bugs that won't let me do anything. Is there something outside what the video describes that I need to do to prep for these videos? I am brand new just trying to get a few specifics done for me game it seems like D&D really is the only method that works for me as every code I try inputting I apparently do wrong somehow.
Since this video game out, gml changed significantly. I recommend you check out some other videos that came out recently to learn gml first, and then come back to these videos and adapt the code.
Hi. This is a great tutorial, and it works really well. But at the end I always crash using this line of code: tile_map = layer_tilemap_get_id(tile_layer); . Can you tell me some reasons why this would happen? My tileset is sparse, but it does exist in the layer, and I've double and triple checked my layer name on the previous line. Thanks for the great tutorial. I would just like to see it through to completion.
Hard to tell without more information. Could you give me the actual error message? Also, might be helpful if you head over to the GMC (forum.yoyogames.com) and make a new post outlining the issue. Plenty of people there willing to help. If you mention me (@GMWolf) I'll be able to take a look too.
Its a great tutorial. I just have one question I did exactly as you did in the video. My character movement is a bit shaky when it finally reaches a grid block Is there any way to make it smooth?
The best solution is probably to keep an accumulator. When you reach the end of a movement step, there may be some time leftover. Keep track of that to put it back into the start of the next step.
This tutorial is great! However, when walking, the sprite can sometimes snap back to its previous location for a split-second, causing stuttering. I noticed it in your video, and it occurs in my project as well. Have you found any possible solution? I played with the animation speeds and walk times, but to no avail. Thanks!
My code is: if (t >= 1) { walk_anim_time = 0; t = 1; state = states.idle; } It's not the animation itself, as it seems to run just fine. The error I'm having has something to do with the sprite returning to its previous position at the start of another run animation, causing the sprite to shake in the movement direction.
Ah, ok. if by previous position, you mena the position in previous frame, rather than previous grid position, then yeah, I have noticed that. I think the best solution is to have a carry over varaible. So when you set your t = 0, keep track of how much t was left over, and use that in the next frame.
t -= 1; accu = t; t = 0; In the code that starts the movement (forgot the name) t = accu. UA-cam comments make it hard to share code, if you need more help it's probably easier if you head over to forum.yoyogames.com and make a thread. If you mention me (@gmwolf) I'll be able to help you there. Other people will also be able to help.
All languages follow the same basic principles, as GM Wolf said, it's pretty easy to adapt code from one language to another, learn about Algorithms and Pseudo Code, it will make your life easier. :)
Followed your code to the letter and am getting this error: Variable .state(100003, -2147483648) not set before reading it. at gml_GlobalScript_Move (line 5) - if (state == states.idle) { What am I doing wrong here?
I know this is almost a 5 year old video now, but I also followed this guide exactly as shown. It would instantly crash all the time stating that Variable .state(100003, -2147483648) not set before reading it. at gml_GlobalScript_move (line 4) - if (state == states.idle) Neither me or my professor could figure out why it's happening, but the solution was to put the move script directly in the key down. Any idea why? Thanks for the video regardless, lots of great info!
Are you using gms2.3? If so make sure you are using the new GML function syntax. Other than that kinda hard to debug over UA-cam comments, if you head over to the GMC (forum.yoyogames.com) and post a few more details I might be able to help you
Previously each script was its own function. Notice I just write the instructions directly into the script file without defining a function. In the latest version of GML, you have to wrap the statements in a function declaration. I would recommend you read about functions in the manual. It should show you the syntax to define functions.
Looking into crafting a system similar to the South Park games where distance from the camera changes perspective a little bit. I think this will help and then applying an isometric view but making the player think it’s a 2-D room..
Ty, for all the teaching man.
Very clear and helpful tutorial. 👍🏼
love your video! learn a lot!
This tutorial helped me a lot thanks
This is a very nice video, I have learnt a lot from it. I love the format too - finally a tutorial that isn't too slow, nor too long. Thanks a lot man, that's definitely a subscribe!
Good tutorial, I have been trying to figure out this on my own but didn't manage to get that complex, you have enlighten me and given me a starting point, thanks.
glad you found it useful!
Hope you keep making more tutorials :D
Good and succinct tutorial. I'm glad you didn't go off on tangents. Also, having a fully edited video helps a lot!
+Harbinger thanks. Glad you like the format. I think the extra work is worth it.
Thanks. Next, consider some videos on 3D. I am currently almost finished with a Rubik's Cube composed of prims. It is almost functional.
One future project I'm thinking of is to re-create the opening stage of the SNES version of Star Fox. It would very fascinating to see a re-creation run at 60fps in HD. Just a thought.
holy heck dude this is great but did you speed the footage up 4x this is crazy fast
Looking into crafting a system similar to the South Park games where distance from the camera changes perspective a little bit.
I think this will help and then applying an isometric view but making the player think it’s a 2-D room..
thank you for gamemaker video
Amazing video, really helped me! I was just wondering how the collision test could be adapted to stop the player moving through certain objects too? I know this may be obvious, but I've just started with gamemaker and any help would be really appreciated!
Sorry, tell me how you can implement the execution of the action on the animation frame, for example, the script "blow with the sword" should be called on frame 3, before that I used an integer currentFrame, now it is fractional. Perhaps it is worthwhile to call the script not in the frame but after some time since the beginning of the animation? how best to implement it? Forced to use lerp in order to have the possibility of growth of character skills, movement speed, attack speed, and so on, without float values or how not to do. Sorry do you have an e-mail?
Thanks!
Cool!!!!!!!!!!!! HM ... are there any more grid alignment techniques ... like in Blackthorne !! ;)
This is awesome! Exactly what I was looking for. I did have a question though. If I'm next to a wall, but not facing towards it, is there a way to make the player character look towards the wall? For example, if I'm there is a wall to my left, and I'm facing down currently and press left, is there a way to make the character face left while being stationary other than the animation change?
When we check if we can move into the new spot, you can add an else block and change the sprite there.
@@GMWolf That worked perfectly and was very simple. Thanks a bunch!
great tutorial but the collision isnt working on mine, my character gets stuck as soon as it enters the level. Im using my own assets but I cant see any reason why it shouldnt be working. Maybe something to do with collision masking?
This is a wonderful tutorial. I have some programming experience but am new to GameMaker Studio 2, and this was a great lesson in a variety of functions.
Not sure if you still check these comments, but I did have one question. Some games alternate animations every other step. First step would be: idle, left foot, idle. Second step would be: idle, right foot, idle. And so forth. Rather than playing all animation frames with each step. (Final Fantasy VI is a great example.)
If I wanted to do something similar in GMS2, would I need to create two separate sprites and alternate between them? What would be the best method in your opinion? Thanks!
I would keep track of what step to perform next.
Then use a single sprites with both animations.
If I recall correctly GM will actually automatically remove duplicate frames and handle that for you.
But if you like to be explicit, you could keep an array of image indices for each animation.
@@GMWolf Thank you for your reply! Could you illustrate for me what this index would look like? I've been studying the example of "frames = [...]" from your tutorial. But I'm not sure how to translate this into a system that alternates between two sets of animations depending on which step comes next. Thank you again.
You would have two arrays like so: left_step = [...]; right_step = [...];
Then also have a variable keep track of what step to do:
step_type = 0; //0 for left. 1 for right.
Then when you take a step:
if (step_type == 0) {
//Use left_step
} else {
//Use right_step
}
When you end a step:
if (step_type == 0) {
step_type = 1;
else {
step_type = 0;
}
Really great tutorial ty. Its not too long nor rushed, anyone with minimum gm experience can follow allong. I learned alot from it.
Hello! Very nice tutotorial. Only problem I'm having is with the collision. Going left lets you go very close to the wall, but right keeps you farther away.
Is the distance more than a tile? If so it could be you are checking for collisions multiple tiles away. Otherwise it could be that your sprite origin needs adjusting.
@@GMWolf Yeah it seems that it is due to the sprite origin. I might try another tile collision method.
If it is just the sprite origin you just have to adjust it. Sprite origin and offset will be something to consider no matter which tile collision system you use.
So I'm having an issue with trying to get the collisions to work right on my game. Instead of the wall tiles stopping movement, my player object keeps moving, and passes through the walls. I don't understand what I'm doing wrong, and need help figuring it out. My current solution is to try and set the state to idle, after I set delta time/1000000 to be multiplied by 0, rather than 2, the default speed I've chosen in for the object, however, this results in the object still moving, and they still pass through the walls, which I've made objects for.
before moving the object check if he will be moving into a wall...
I copied the code verbatum but the player is moving to the next tile instantly, instead of slowly moving there. Any idea what would cause that?
very good, would you have a tutorial on how to make this movement, with another character following him like the phantasy star rpgs 2, 3 4, etc?
You can just reuse the inputs but delayed by how ever many steps behind you want the NPC to follow
@@GMWolf I tried =/ but not success ,I looked for tutorials and I can't find anything
Hello! Nice tuto!
I Have a question. I do exactly that you show. But when I try start the game, I have an error in move script: state is not defined. But works if I put the content of move script directly in key down - right event.
If you are using gms2.3 or above make sure you are using the new function syntax.
i typed the code the way it was shown on screen and got the following error int the move script: cannot redeclare a bullutin variable
Amazing tutorial! I am trying to make a Pokémon fan game using sprites from the GBA games (Fire Red / Leaf Green). I had a few questions:
I might just be dense, but I am not sure how I would increase the speed to match the actual Pokémon games? I didn't see an obvious variable in which to do so.
Also, I am having some trouble lining up the animations with the sprites I have (3 frames per direction like yours, but it doesn't look right lol) any tips?
Hi. Iirc this tutorial uses a time based approach, where each step takes a certain amount of time to cross.
You can increase the movement speed by decreasing that time.
Alternatively you can increase the time that gets accumulated in the t variable.
As for the animation, it will depend on the frames you have. I think the sequence I have matches the Pokémon sequence too but I could be wrong.
One alternative is to ditch the custom array and just do it in the sprite editor. GM will remove duplicate frames for you.
If you meant lining up spacially, you will have to play around with the origin of the sprite.
I cannot seem to manage to get this right, the movement always stutters unless I set walk_anim_length to be 1 second or slower.
Thank you for this tutorial. I have never coded before, but following along definitely gives good experience with a number of different components of Game Maker.
I am getting an error when I try to run the game which states that I am "trying to index a variable that is not an array". The error message tells me it's related to line 5 of the move script. I've looked, but I can't find what I've done wrong. Any idea where I might have made a mistake that would give me the error message I've described?
Hi, glad you like the tutorials!
The error you get is because somewhere, you typed something[aVariable].
This is used to access arrays. So if "something" isn't an array, you will get an error.
Thanks for the reply! I initially thought there might be a problem with the "dir" variable, but if I understand you correctly it seems the issue might be with my global.components array. I've set up the gml_pragma line as instructed, but I have to imagine something about what I've typed is incorrect.
I'll keep looking. Worse comes to worst I'll start over and try again - would be good to get the extra practice in anyway.
Thanks again.
If you need help, I suggest heading over to the GMC at forum.yoyogames.com
If you make a post with your code explaining the issue, there are a lot of people willing to help, myself included.
Thanks. If I continue to struggle I'll definitely head there for assistance!
Its been a month, so you definitely probably figured this out, but for anyone else looking to figure out this problem I may have a solution since it happened to me too.
at the ~~~global.components = []~~~ section I typed "=" after global.components for the next four lines of code.
Mine originally;
global.components = [];
global.components = [directions.right] = [1, 0];
global.components = [directions.left] = [-1, 0];
global.components = [directions.up] = [0, -1];
global.components = [directions.down] = [0, 1];
the proper way;
global.components = [];
global.components [directions.right] = [1, 0];
global.components [directions.right] = [1, 0];
global.components [directions.right] = [1, 0];
global.components [directions.right] = [1, 0];
My player, who is 16 wide, will not move through a 16x16 hallway space, they get stuck :(
Also, I am looking for the best way to add that when you press a direction, even if you are up against a wall, you will turn that direction (in the idle state)
Might be worth checking the origin on your player sprites. Make sure it's the same as the origin for your wall sprites :)
when i write sPlayerRight or any of the other sPlayer directions it thinks its a variable
help pls
Hmmm, the collision detection piece does not seem to function for me. Has something changed since this tutorial was published? I have placed tiles on the Walls layer but still my character passes right through them.
Hard to tell what the issue could be. As far as I'm aware the code should still be working fine.
Why not head to Reddit or the GMC (forum.yoyogames.com) and post the relevant code along with the question. Plenty of people there to help.
If you mention me on the GMC (@GMWolf) then Ill get a notification and can give it a look.
When creating the tilemap variable, my tilemap id is -1 according to the debugger and any time I use tilemap_get to get the tile at an x/y coord it returns -1 as well. It's in the create script for my player object. Do you know what I could be doing wrong?
Maybe you have the wrong tilemap layer name. It needs to be written the same as in the room editor.
Thanks for the response. I double checked and the layer name matches what it shows on the room1 tab. For reference, the following is what I used, and t1 and t2 below are the variables I used to test in the debugger since I have a wall tile at 0, 0 but not at 1, 1:
var tile_layer_walls = layer_get_id("walls")
var tile_map_walls = layer_tilemap_get_id(tile_layer_walls)
t1 = tilemap_get(tile_map_walls, 0, 0)
t2 = tilemap_get(tile_map_walls, 1, 1)
Hmmm, at this point I would recommend heading over to the forums (forum.yoyogames.com) and post your code there, myself and others will be able to better help you there.
I really appreciate the time you spent even to make this video.
After a week of trying other people's codes and my own combination of codes, I came across this. It has helped, but, I still can not accomplish certain things.
I am trying to rebuild "Sabin's House" from FF6 as a learning experience. It is a pretty small area.
In the real game, when the player moves in front of a particular plant (which covers about 15 pixels height, across two tiles), the player covers the plant. (the players 24 pixels tall). When the player goes behind that same plant, the player is covered.
I am not sure how I can manage the collision detection to make it so you can't walk through the plant while simultaneously being capable of standing on either of the two tiles that have the plant drawn on them (you can walk in from left or right, but, not up or down) AND at the same time have the plant sprites render correctly, above or below the player...
Do I need to set up multiple layers and collisions and activate / deactivate them as the player passes certain areas of the map? Maybe detection based on object would help instead? Before I started using your code, I noticed with place_meeting I could "accidentally" slip the player into occupied squares, while I was using an object that contained a sprite less than my grid size (like a 4x4 in a 16x16 grid), successfully blocking me from coming in from certain directions.
ALSO
how can I switch the walking animation every other time you command the player to walk?
I noticed in the game, the walking animations alternate every time you let go of walk, and continue walking again. you will start with right hand, then if you let go and start again, you will start with left hand and so on.
Hope that made sense :)
please help, I am up till 2 in the morning every night after work, till I fall asleep on my keyboard and feel like a zombie.
I may post on game maker forums in a bit as suggested below, but, right now I need a 15 minute break.
What if i want my player to have a custom size (like 64x128) ? I tested it on my own but he just collides into the walls :(
Then you will need a more complex way to check collisions.
A good starting point is to consider the tiles the player is already occupying, and where they will end up if he moves.
Hi , just wanted to know , how to add the sprint functionality , i change the walk_anim_length when sprint key is pressed , the player move fast but it make some jerk like movement. can you help me on this . thankyou
It's probably because we reset the animation time to 0 for every grid step. What we should be doing is carrying over any excess when we get to the end of one step, into the beginning of the next.
So in the video, you have your walls set as tiles, what if I used objects instead?
+Mason H. Not much difference: when you check for collision, use the object colision functions instead
+@@GMWolf
Can you elaborate further on this, please (allowing for collision checking with objects, that is)? I've been trying to figure out how exactly to do this, but I've been having trouble with it.
I've tried a number of ways to get it to work, but the current potential solution I've been working with is simply adding another if statement to the Move script in-between checking the "Walls" layer and the actual code that allows the player to move that looks like this:
if !(tilemap_get(tile_map, x_pos + dx, y_pos + dy))
{
if (position_empty(x_pos + dx, y_pos + dy))
{
x_from = x_pos;
y_from = y_pos;
(So on and so forth)
The issue that I'm having is that the coordinates provided for position_empty are never actually detecting if an object's collision mask is in that spot. It works if I give it direct coordinates on or off an object in the world, though. So I'm not entirely sure what's going on with it.
-Update-
I've actually found a solution to this, although it seems as though it has the potential for bugs, so I'm working on seeing if it could cause issues with certain situations. But for now, here's what I've got so far. Sorry about the indentation as well, you can thank UA-cam for butchering that.
(Additions to "MACRO" in bold. Note: The variables are global mostly for debugging purposes.
gml_pragma("global", "MACRO()");
#macro tile_width 16
#macro tile_height 16
*globalvar directionX;*
*globalvar directionY;*
*directionX = 0;*
*directionY = 0;*
enum directions
{
up, left, down, right
}
global.components = [];
global.components[directions.up] = [0,-1];
global.components[directions.left] = [-1,0];
global.components[directions.down] = [0,1];
global.components[directions.right] = [1,0];
(Additions to "move" in bold.)
var dir = argument0;
var components = global.components[dir];
var dx = components[0];
var dy = components[1];
*if (dir == directions.left)*
*{*
*directionX = ann.x - 16;*
*directionY = ann.y;*
*}*
*if (dir == directions.right)*
*{*
*directionX = ann.x + 16;*
*directionY = ann.y;*
*}*
*if (dir == directions.up)*
*{*
*directionX = ann.x;*
*directionY = ann.y - 16;*
*}*
*if (dir == directions.down)*
*{*
*directionX = ann.x;*
*directionY = ann.y + 16;*
*}*
if (state == states.idle)
{
sprite_index = sprite[dir];
if !(tilemap_get(tile_map, x_pos + dx, y_pos + dy))
{
*if (position_empty(directionX, directionY))*
{
x_from = x_pos;
y_from = y_pos;
x_to = x_pos + dx;
y_to = y_pos + dy;
x_pos = x_to;
y_pos = y_to;
state = states.walking;
//sprite_index = sprite[dir];
}
}
}
Good afternoon, thank you for your contribution to the development of the GM community and interesting examples.
I have a question, I did everything according to your lesson, but in GM 8.1, strange things happen because of the binding to deltaTime :) If we add the condition image_index = 2 (we will not go deep into the details) add instance_create (in general, any less costly) action) the character abruptly moves to the end of the animation. What can it be connected with? Perhaps it is necessary to refuse deltaTime and use other method?
Hi, I was wondering, do I have to arrange any agreements for royalties if I make a game using code from this video and release it for purchase?
No it's free to use. I didn't include any license so I appreciate you asking :)
no matter what i do, i get this error:
DoConv :1: illegal undefined/null use
at gml_GlobalScript_move (line 2) - var components = global.components[dir];
############################################################################################
gml_GlobalScript_move (line 2)
cant figure out why. afaik i copied you exactly. i feel like it has to do with gm's recent update thing
If you are using the new GML (GMS2.3+) make sure you are using functions correctly. Writing scripts directly without wrapping them in functions isn't valid anymore.
Sorry for commenting here on this incredible old video, but I'm having the stuttering issue a couple people have mentioned in the comments, only instead of happening at every tile, it seems to be happening every frame. I tried making a post on the GMC forum about this, but they don't make posts visible until moderators accept them. Any idea as to what this could be? If need be I can show the code.
Every frame? That is quite odd.
I can't recall any possible issues outside the tile->tile transition. Does the stutter happen with any other animated objects?
It's hard to discuss code in yt comments.
What forum subsection did you post to? AFAIK only the tutorials section needs moderator approval.
@@GMWolf Wow, you responded really fast! I'm assuming it's every frame because of how consistent it is, but it's just very common, so I can't say for sure.
All sections need moderator approval for me, since I had made an account to post this, and the first 5 posts on a GMC forum account require moderator approval. I tried other animated objects, and it's just this object, so it's definitely not lag. Is there any good way for me to send my code to you that isn't youtube comments?
I figured it out, I had messed with viewports and it made it look like it was shuffling back and forth when in reality it was just the camera not keeping perfectly on center with the character. Sorry about that!
Great! Glad you figured it out!
Happy deving!
Fantastic Tutorial, I have it all up and running perfectly and smoothly in my initial room, but once I transition to the next room it no longer detects the tile layer, and the collisions stop working, do you know of any way to fix this?
It is not a naming issue as all the tile sets and tile layers are named exactly the same as the previous room.
If anyone has any ideas please let me know!
Thanks again :)
I think you would need to get the layer id and tilemap id again because you changed room. I would make those actual variables in the player object and in your transition object, re assign those variables to what's in the room you are going to.
What I did to fix this problem, was move:
var tile_layer = layer_get_id(“Wall”);
tile_map = layer_tilemap_get_id(tile_layer)
out of the create event of the player, and into the step event of the player. This way it checks every step of the game, instead of just at the beginning of its creation
Also, could you let me know how you got the room transition working properly? Mine works for the most part, but it puts the player at the wrong x and y coords.
Alright got this system down, just wondering if theres a way I can set a movement speed as I move within the tiles? With just this code alone it makes the player move too fast.
Yeah, I'm fairly sure I even set a variable to control the speed...
Hm, might have missed it then. I'll watch again.
alright i figured out my issue, so basically in the step event of the player i did "t >= 0" instead of "t >=1"
Ah yes, that would be it! :)
Cool! Does anyone have an idea how to implement this for non-player objects, like enemies and the like? Specifically I'm trying to do something turn-based.
The move functions that initiate the movement are still the same. You just have to call them in different places.
For a Player object you call it on keyboard press events.
For NPCs you could do it on a timer (for Pokémon style random movement) or when it's their turn for a turn based game.
Using GMS 1.4, anyone know how to do the tile_width and tile_height thing? It doesn't like the #macro syntax.
The #macro syntax is the new way of defining macros. You can still do it that way : docs.yoyogames.com/source/dadiospice/001_advanced%20use/010_macros.html
But note this tutorial was really designed for the new tilemap system that doesn't exist in 1.4
I am so confused on all these videos I watch for GMS2 everyone says it works perfectly but everytime I emulate these videos I get bugs that won't let me do anything. Is there something outside what the video describes that I need to do to prep for these videos? I am brand new just trying to get a few specifics done for me game it seems like D&D really is the only method that works for me as every code I try inputting I apparently do wrong somehow.
Since this video game out, gml changed significantly.
I recommend you check out some other videos that came out recently to learn gml first, and then come back to these videos and adapt the code.
Hi. This is a great tutorial, and it works really well. But at the end I always crash using this line of code: tile_map = layer_tilemap_get_id(tile_layer); . Can you tell me some reasons why this would happen? My tileset is sparse, but it does exist in the layer, and I've double and triple checked my layer name on the previous line. Thanks for the great tutorial. I would just like to see it through to completion.
Hard to tell without more information. Could you give me the actual error message?
Also, might be helpful if you head over to the GMC (forum.yoyogames.com) and make a new post outlining the issue. Plenty of people there willing to help. If you mention me (@GMWolf) I'll be able to take a look too.
Can anyone link me to a tutorial on how you would click on a grid square and have an object move there
Whilst not exactly a tutorial, I did do a livestream where I prototype an RTS engine. If you are still looking for a good source, it could help.
Its a great tutorial. I just have one question
I did exactly as you did in the video. My character movement is a bit shaky when it finally reaches a grid block
Is there any way to make it smooth?
The best solution is probably to keep an accumulator.
When you reach the end of a movement step, there may be some time leftover. Keep track of that to put it back into the start of the next step.
I am sorry, I am new to Gamemaker, so i dont know what an accumulator is...
Just a variable. In this case it would keep track of all the "unused" time.
Again.. I am pretty new, so i dont know how to do that..
If possible can you discord me the code segment needed?
my discord tag: XXpoomaanXX#1899
I wouldn't worry about it. Or ask about it on the forums. forum.yoyogames.com
This tutorial is great! However, when walking, the sprite can sometimes snap back to its previous location for a split-second, causing stuttering. I noticed it in your video, and it occurs in my project as well. Have you found any possible solution? I played with the animation speeds and walk times, but to no avail. Thanks!
I didnt notice it, have you made sure to clamp you t value to 1 maximum?
Or did you mean with the animation?
My code is:
if (t >= 1) {
walk_anim_time = 0;
t = 1;
state = states.idle;
}
It's not the animation itself, as it seems to run just fine. The error I'm having has something to do with the sprite returning to its previous position at the start of another run animation, causing the sprite to shake in the movement direction.
Ah, ok. if by previous position, you mena the position in previous frame, rather than previous grid position, then yeah, I have noticed that.
I think the best solution is to have a carry over varaible. So when you set your t = 0, keep track of how much t was left over, and use that in the next frame.
How would I go about doing that, actually? I've spent the past couple hours trying different methods, but none of them seem to grab the left over t.
t -= 1;
accu = t;
t = 0;
In the code that starts the movement (forgot the name) t = accu.
UA-cam comments make it hard to share code, if you need more help it's probably easier if you head over to forum.yoyogames.com and make a thread. If you mention me (@gmwolf) I'll be able to help you there. Other people will also be able to help.
There is a way to do the same with gamepad?
Yes just do the same, but instead of calling the move scripts on keyboard events do so with a gamepad button check.
@@GMWolf oh, i understand, so i have to put the logic in the step event?
Yeah I think so
@@GMWolf Ok, really thanks :)
Great tutorial but like 4x as fast as I can hear, think, and type. Slow down a lot, and pause between steps too.
At first it walks normally but then it's too fast
Oh it's gms2.
where you looking for other engines?
I think the person was hoping the tutorial was for gms1
The movement works fine for me in gms1.4 xD probably doesn't matter now, im a year late.
uncomplit
Wish this was unity.
You should be able to adapt this code to unity fairly easily.
Im no coding pro though and I don't understand this language
If only this was in C# :/
Well, you can easily adapt the code to c# if you like:)
All languages follow the same basic principles, as GM Wolf said, it's pretty easy to adapt code from one language to another, learn about Algorithms and Pseudo Code, it will make your life easier. :)
You move way too fast
I encourage anyone to pause and ponder while watching any tutorials :)
Followed your code to the letter and am getting this error:
Variable .state(100003, -2147483648) not set before reading it.
at gml_GlobalScript_Move (line 5) - if (state == states.idle) {
What am I doing wrong here?
I know this is almost a 5 year old video now, but I also followed this guide exactly as shown. It would instantly crash all the time stating that
Variable .state(100003, -2147483648) not set before reading it.
at gml_GlobalScript_move (line 4) - if (state == states.idle)
Neither me or my professor could figure out why it's happening, but the solution was to put the move script directly in the key down. Any idea why? Thanks for the video regardless, lots of great info!
Are you using gms2.3? If so make sure you are using the new GML function syntax.
Other than that kinda hard to debug over UA-cam comments, if you head over to the GMC (forum.yoyogames.com) and post a few more details I might be able to help you
@@GMWolf Oh wow thanks for the fast reply. I'm only familiar with the latest version of Gms, what is the syntax change you are referring to?
Previously each script was its own function. Notice I just write the instructions directly into the script file without defining a function.
In the latest version of GML, you have to wrap the statements in a function declaration.
I would recommend you read about functions in the manual. It should show you the syntax to define functions.
@@GMWolf Ah, this makes more sense now! Thanks so much for all the information, I'll be reading into it shortly.
Looking into crafting a system similar to the South Park games where distance from the camera changes perspective a little bit.
I think this will help and then applying an isometric view but making the player think it’s a 2-D room..