GameMaker: Studio

GameMaker: Studio

View Stats:
lemming Apr 14, 2018 @ 3:56am
hspeed and vspeed | cannot redeclare built in variables?
my code for movement is this:

hspeed = 4 * (right - left);
vspeed = 4 * (down - up);

right, left, down and up are variables i set:

var right = keyboard_check(ord('D'))
var left = keyboard_check(ord('A'))
var down = keyboard_check(ord('S'))
var up = keyboard_check(ord('W'))

and when i start the game, i get this error:

In Object player, in Event Step event number 0 at line 14 : cannot redeclare a builtin varable
In Object player, in Event Step event number 0 at line 14 : symbol } expected
In Object player, in Event Step event number 0 at line 14 : malformed assignment statement

anyone knows why? line 14 is vspeed = 4 * (down - up);
< >
Showing 1-6 of 6 comments
Blind Apr 14, 2018 @ 3:58am 
could use more information, it might be because you are using "var" for a variable you want to be persistant.
maras Apr 14, 2018 @ 4:19am 
I'd like to help but I have never seen this kind of math :D
I always use the easiest way where is impossible to do something wrong

if keyboard_check(ord("W")) vspd = -4;
if keyboard_check(ord("S")) vspd = 4;
if keyboard_check(ord("A")) hspd = -4;
if keyboard_check(ord("D")) hspd = 4;

y += vspd;
x += hspd;
Last edited by maras; Apr 14, 2018 @ 4:21am
Zappy Apr 14, 2018 @ 4:25am 
Originally posted by marasovec:
- I always use the easiest way where is impossible to do something wrong

if keyboard_check(ord("W")) vspd = 4;
if keyboard_check(ord("S")) vspd = -4;
if keyboard_check(ord("A")) hspd = 4;
if keyboard_check(ord("D")) hspd = -4;

y += vspd;
x += hspd;
That's something done wrong, though. You have it set up so if you press A, you move towards the right until you press D, which makes you move towards the left, even though D is to the right of A. And even then, you also have it set up so that letting go of both A and D will still send you in the direction you last pressed, due to the speed values never being reset outside of the key check commands, where it never gets set to 0. The same goes for W and S (which also seem to be set up oppositely of what they should, moving up forever when pressing S and down forever when pressing W). And as a result, once you start moving diagonally, you will never stop moving diagonally, not even if you want to.

But even if we look aside from the directions being the opposite of what they probably should be and the lack of a "don't move if not pressing anything" thing, you also have it set up so if pressing both A and D, you'll move by the direction of D, not not move, which is also something done wrong.

Edit: After you posted your comment, you did edit your comment to fix the directions being opposite (even before I finished writing and posting this comment), but you haven't fixed the rest of the things I mentioned.
Last edited by Zappy; Apr 14, 2018 @ 4:28am
maras Apr 14, 2018 @ 5:51am 
Originally posted by Zappy:
Originally posted by marasovec:
- I always use the easiest way where is impossible to do something wrong

if keyboard_check(ord("W")) vspd = 4;
if keyboard_check(ord("S")) vspd = -4;
if keyboard_check(ord("A")) hspd = 4;
if keyboard_check(ord("D")) hspd = -4;

y += vspd;
x += hspd;
That's something done wrong, though. You have it set up so if you press A, you move towards the right until you press D, which makes you move towards the left, even though D is to the right of A. And even then, you also have it set up so that letting go of both A and D will still send you in the direction you last pressed, due to the speed values never being reset outside of the key check commands, where it never gets set to 0. The same goes for W and S (which also seem to be set up oppositely of what they should, moving up forever when pressing S and down forever when pressing W). And as a result, once you start moving diagonally, you will never stop moving diagonally, not even if you want to.

But even if we look aside from the directions being the opposite of what they probably should be and the lack of a "don't move if not pressing anything" thing, you also have it set up so if pressing both A and D, you'll move by the direction of D, not not move, which is also something done wrong.

Edit: After you posted your comment, you did edit your comment to fix the directions being opposite (even before I finished writing and posting this comment), but you haven't fixed the rest of the things I mentioned.
Yeah i see...

if keyboard_check(ord("W")) vspd = 4;
if keyboard_check(ord("S")) vspd = -4;
if keyboard_check(ord("A")) hspd = 4;
if keyboard_check(ord("D")) hspd = -4;

if !keyboard_check(ord("W")) and !keyboard_check(ord("S")) vspd = 0;
if !keyboard_check(ord("A")) and !keyboard_check(ord("D")) hspd = 0;

y += vspd;
x += hspd;

Now its correct.
Zappy Apr 14, 2018 @ 6:05am 
Originally posted by marasovec:
Yeah i see...

if keyboard_check(ord("W")) vspd = 4;
if keyboard_check(ord("S")) vspd = -4;
if keyboard_check(ord("A")) hspd = 4;
if keyboard_check(ord("D")) hspd = -4;

if !keyboard_check(ord("W")) and !keyboard_check(ord("S")) vspd = 0;
if !keyboard_check(ord("A")) and !keyboard_check(ord("D")) hspd = 0;

y += vspd;
x += hspd;

Now its correct.
No, not quite. Firstly, you seem to have made A move right, D move left, W move down, and S move up again, even though you fixed that in the edit of your previous comment.

Besides that, while you've made it so not holding A while not holding D makes you not move horizontally, you haven't made it so holding both A and D at the same time makes you not move horizontally.
maras Apr 14, 2018 @ 8:25am 
Originally posted by Zappy:
Originally posted by marasovec:
Yeah i see...

if keyboard_check(ord("W")) vspd = 4;
if keyboard_check(ord("S")) vspd = -4;
if keyboard_check(ord("A")) hspd = 4;
if keyboard_check(ord("D")) hspd = -4;

if !keyboard_check(ord("W")) and !keyboard_check(ord("S")) vspd = 0;
if !keyboard_check(ord("A")) and !keyboard_check(ord("D")) hspd = 0;

y += vspd;
x += hspd;

Now its correct.
No, not quite. Firstly, you seem to have made A move right, D move left, W move down, and S move up again, even though you fixed that in the edit of your previous comment.

Besides that, while you've made it so not holding A while not holding D makes you not move horizontally, you haven't made it so holding both A and D at the same time makes you not move horizontally.
♥♥♥♥♥♥♥♥♥♥! Screw it :D
< >
Showing 1-6 of 6 comments
Per page: 1530 50

Date Posted: Apr 14, 2018 @ 3:56am
Posts: 6