TIGSource Forums

Developer => Technical => Topic started by: gggfhfdh on August 11, 2013, 04:57:26 PM



Title: More movement issues!! Hooray!!
Post by: gggfhfdh on August 11, 2013, 04:57:26 PM
I slightly redid the movement code today so that states were based off of direction rather than what the previous sprite_index was so that adding combat would be uh

NOT a pain to do
and this happened
(http://puu.sh/3ZGSU.gif)

here's some incredibly unoptimized code
Code:
var Playerdir;
Playerdir = 0;

//moving
if keyleft
{
    {hmove = Playerhspeed * -1;}
    Playerdir = 4;
    state = "walking"
}
if keyright
{
    {hmove = Playerhspeed;}
    Playerdir = 6;
    state = "walking"
}
if keyup
{
    {vmove = Playervspeed * -1;}
    Playerdir = 8;
    state = "walking"
}
if keydown
{
    {vmove = Playervspeed;}
    Playerdir = 2;
    state = "walking"
}
if keyupleft
{
    {vmove = Playerdiagspeed *-1; hmove = Playerdiagspeed *-1;}
    Playerdir = 7;
    state = "walking"
}
if keyupright
{
    {vmove = Playerdiagspeed *-1; hmove = Playerdiagspeed;}
    Playerdir = 9;
    state = "walking"
}
if keydownleft
{
    {vmove = Playerdiagspeed; hmove = Playerdiagspeed *-1;}
    Playerdir = 1;
    state = "walking"
}
if keydownright
{
    {vmove = Playerdiagspeed; hmove = Playerdiagspeed;}
    Playerdir = 3;
    state = "walking"
}

//not moving
if (!keyleft and !keyright) {hmove=0;}
if (!keyup and !keydown) {vmove=0;}
if (!keyupleft) {hmove =0; vmove=0;}
if (!keyupright) {hmove =0; vmove=0;}
if (!keydownleft) {hmove =0; vmove=0;}
if (!keydownright) {hmove =0; vmove=0;}

 // return to standing state
if (!keyleft) {state = "standing";}
if (!keyright) {state = "standing";}
if (!keyup) {state = "standing";}
if (!keydown) {state = "standing";}
if (!keyupleft) {state = "standing";}
if (!keyupright) {state = "standing";}
if (!keydownleft) {state = "standing";}
if (!keydownright) {state = "standing";}

//defines what walking and standing mean actually mean
switch (state)
{
    case "walking":
        if keyleft then sprite_index = sPlayerwalkleft;
        if keyright then sprite_index = sPlayerwalkright;  
        if keyup then sprite_index = sPlayerwalkup;
        if keydown then sprite_index = sPlayerwalkdown;
        if keyupleft then sprite_index = sPlayerwalkupleft;
        if keyupright then sprite_index = sPlayerwalkupright;
        if keydownleft then sprite_index = sPlayerwalkdownleft;
        if keydownright then sprite_index = sPlayerwalkdownright;
    break;
        
    case "standing":
        if Playerdir = 4 then sprite_index = sPlayerleft;
        if Playerdir = 6 then sprite_index = sPlayerright;          
        if Playerdir = 8 then sprite_index = sPlayerup;
        if Playerdir = 2 then sprite_index = sPlayerdown;  
        if Playerdir = 7 then sprite_index = sPlayerupleft;
        if Playerdir = 9 then sprite_index = sPlayerupright;
        if Playerdir = 1 then sprite_index = sPlayerdownleft;
        if Playerdir = 3 then sprite_index = sPlayerdownright;        
    break;          
}

I started picking things apart and splitting them into separate sections to try and figure out if maybe having things grouped together was causing conflicts or something (or maybe just make me notice the problem having it all laid out like that) but that did absolutely nothing

i'm clueless at this point. every time i *thought* i had found the problem it fixed the movement, but i was stuck in standing state. or i couldn't revert to standing once i entered walking


Title: Re: More movement issues!! Hooray!!
Post by: rundown on August 11, 2013, 05:03:37 PM
I don't know gamemaker although maybe you could try || isntead of and.

It looks to me your variables hmove and vmov don't seem to reach 0.


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 11, 2013, 05:38:27 PM
yeah i thought using 'and' rather than || (i tried && too just to hit all the bases) might be causing problems when a friend suggested that bit but it somehow changes absolutely nothing, you still can't move

also unless i'm missing something the hmove and vmove variables *should* be hitting 0 right here?
Code:
if (!keyleft and !keyright) {hmove=0;}
if (!keyup and !keydown) {vmove=0;}
if (!keyupleft) {hmove =0; vmove=0;}
if (!keyupright) {hmove =0; vmove=0;}
if (!keydownleft) {hmove =0; vmove=0;}
if (!keydownright) {hmove =0; vmove=0;}

maybe I should tweak that part to something like
Code:
if hmove != 0 then hmove = 0;


Title: Re: More movement issues!! Hooray!!
Post by: Chis on August 11, 2013, 05:39:01 PM
I think the problem is here:

if (!keyleft and !keyright) {hmove=0;}
if (!keyup and !keydown) {vmove=0;}
if (!keyupleft) {hmove =0; vmove=0;}
if (!keyupright) {hmove =0; vmove=0;}
if (!keydownleft) {hmove =0; vmove=0;}
if (!keydownright) {hmove =0; vmove=0;}

say you want to go up and you press the up key. that means only keyup will be true. but that block of code says that if any of keyupleft, keyupright, keydownleft, or keydownright are not true, then set movement to 0 anyway, which is not quite what you want. If you're not dealing with acceleration, I would do something like this:

Code:
//initialize everything to neutral first
var Playerdir;
Playerdir = 0;
hmove = 0;
vmove = 0;
state = "standing";

//moving
if keyleft
{
    {hmove = Playerhspeed * -1;}
    Playerdir = 4;
    state = "walking"
}
...
...
if keydownright
{
    {vmove = Playerdiagspeed; hmove = Playerdiagspeed;}
    Playerdir = 3;
    state = "walking"
}

this way, if any of the directional keys are pressed, hmove/vmove will have values; otherwise, they'll remain 0. note that you shouldn't have any of the if (!key) statements if you do this.


Title: Re: More movement issues!! Hooray!!
Post by: Xion on August 11, 2013, 05:54:39 PM
alternatively, you could just move that block of code to the start. Even so most of it would still be pretty redundant. Might as well just set all speeds to 0 at the start. Should still work though, since you're setting the speeds to nonzero only after you stop him, but still before you actually reposition him based on hmove/vmove (I assume).


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 11, 2013, 05:59:03 PM
snip
I actually tried that earlier! Taking those variables out of the create event and slapping them at the top of the movement script didn't do anything (although i'm not sure why it would. would it? idk) but removing the !key statements brings me back to this problem:
(http://puu.sh/3ZNIp.gif)
you can move, but you're stuck in the walking state

and I tried this too just now
adding back *just* the !key statements about returning to the standing state lets you move around also, but you're permanently in the standing state


Title: Re: More movement issues!! Hooray!!
Post by: poe on August 11, 2013, 06:05:08 PM
Okay try putting this in create:

Code:
Playerdir = 0;
state = "standing"

Then change the step to this:

Code:
state = "standing"

//moving
if keyleft
{
    {hmove = Playerhspeed * -1;}
    Playerdir = 4;
    state = "walking"
}
if keyright
{
    {hmove = Playerhspeed;}
    Playerdir = 6;
    state = "walking"
}
if keyup
{
    {vmove = Playervspeed * -1;}
    Playerdir = 8;
    state = "walking"
}
if keydown
{
    {vmove = Playervspeed;}
    Playerdir = 2;
    state = "walking"
}
if keyupleft
{
    {vmove = Playerdiagspeed *-1; hmove = Playerdiagspeed *-1;}
    Playerdir = 7;
    state = "walking"
}
if keyupright
{
    {vmove = Playerdiagspeed *-1; hmove = Playerdiagspeed;}
    Playerdir = 9;
    state = "walking"
}
if keydownleft
{
    {vmove = Playerdiagspeed; hmove = Playerdiagspeed *-1;}
    Playerdir = 1;
    state = "walking"
}
if keydownright
{
    {vmove = Playerdiagspeed; hmove = Playerdiagspeed;}
    Playerdir = 3;
    state = "walking"
}

//not moving
if (!keyleft and !keyright) {hmove=0;}
if (!keyup and !keydown) {vmove=0;}
if (!keyupleft) {hmove =0; vmove=0;}
if (!keyupright) {hmove =0; vmove=0;}
if (!keydownleft) {hmove =0; vmove=0;}
if (!keydownright) {hmove =0; vmove=0;}

//defines what walking and standing mean actually mean
switch (state)
{
    case "walking":
        if keyleft then sprite_index = sPlayerwalkleft;
        if keyright then sprite_index = sPlayerwalkright;   
        if keyup then sprite_index = sPlayerwalkup;
        if keydown then sprite_index = sPlayerwalkdown;
        if keyupleft then sprite_index = sPlayerwalkupleft;
        if keyupright then sprite_index = sPlayerwalkupright;
        if keydownleft then sprite_index = sPlayerwalkdownleft;
        if keydownright then sprite_index = sPlayerwalkdownright;
    break;
       
    case "standing":
        if Playerdir = 4 then sprite_index = sPlayerleft;
        if Playerdir = 6 then sprite_index = sPlayerright;         
        if Playerdir = 8 then sprite_index = sPlayerup;
        if Playerdir = 2 then sprite_index = sPlayerdown; 
        if Playerdir = 7 then sprite_index = sPlayerupleft;
        if Playerdir = 9 then sprite_index = sPlayerupright;
        if Playerdir = 1 then sprite_index = sPlayerdownleft;
        if Playerdir = 3 then sprite_index = sPlayerdownright;       
    break;         
}

You don't need to check if nothing's pressed to return to standing, with my code you're now assuming every frame your standing until something is pressed. Also you aren't resetting the direction each time now.

Let me know if I misinterpreted/it's still not working.


Title: Re: More movement issues!! Hooray!!
Post by: Chis on August 11, 2013, 06:09:07 PM
did you test it with those variables at the top? the reason I set them to 0/standing is because, say you move left and your state gets set to "walking". but then you stop, but nowhere in the code says the state should go back to "standing". so by setting things to neutral first, you're effectively saying, be neutral on this frame unless the player pressed something.


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 11, 2013, 06:15:44 PM
did you test it with those variables at the top? the reason I set them to 0/standing is because, say you move left and your state gets set to "walking". but then you stop, but nowhere in the code says the state should go back to "standing". so by setting things to neutral first, you're effectively saying, be neutral on this frame unless the player pressed something.
yep! tried it both with those variables at the top of the movement script and with them back in the create event and the same thing happened both times

also, heres the full create event since a few of the suggestions have been to put things in there that are already in there
Code:
//speed variables
Playerhspeed = 4;
Playervspeed = 4;
Playerdiagspeed = 4 * .707

//movement variables
hmove = 0;
vmove = 0;

//default state
state = "standing";

//direction variable
Playerdir = 0;

also @ poe just tried that, and
(http://puu.sh/3ZOy5.gif)

i'm not entirely sure if that counts as a step forward or back. states are working, but movement isnt


Title: Re: More movement issues!! Hooray!!
Post by: Chis on August 11, 2013, 06:19:11 PM
hmm, I am not sure. did you delete this part as well?

 // return to standing state
if (!keyleft) {state = "standing";}
if (!keyright) {state = "standing";}
if (!keyup) {state = "standing";}
if (!keydown) {state = "standing";}
if (!keyupleft) {state = "standing";}
if (!keyupright) {state = "standing";}
if (!keydownleft) {state = "standing";}
if (!keydownright) {state = "standing";}

ps. I think movement is not working for the reason I just described <:U


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 11, 2013, 06:21:30 PM
yeah,
(http://puu.sh/3ZNIp.gif)
happens if you do

if you keep that part in though, you just kind of slide around stuck in the standing state



~programming~


Title: Re: More movement issues!! Hooray!!
Post by: Chis on August 11, 2013, 06:29:49 PM
loool.
okay, sanity check! can you try replacing all of it with this block?

var Playerdir;
Playerdir = 0;
hmove = 0;
vmove = 0;
state = "standing";

//moving
if keyleft
{
    {hmove = Playerhspeed * -1;}
    Playerdir = 4;
    state = "walking";
}
if keyright
{
    {hmove = Playerhspeed;}
    Playerdir = 6;
    state = "walking";
}
if keyup
{
    {vmove = Playervspeed * -1;}
    Playerdir = 8;
    state = "walking";
}
if keydown
{
    {vmove = Playervspeed;}
    Playerdir = 2;
    state = "walking";
}
if keyupleft
{
    {vmove = Playerdiagspeed *-1; hmove = Playerdiagspeed *-1;}
    Playerdir = 7;
    state = "walking";
}
if keyupright
{
    {vmove = Playerdiagspeed *-1; hmove = Playerdiagspeed;}
    Playerdir = 9;
    state = "walking";
}
if keydownleft
{
    {vmove = Playerdiagspeed; hmove = Playerdiagspeed *-1;}
    Playerdir = 1;
    state = "walking";
}
if keydownright
{
    {vmove = Playerdiagspeed; hmove = Playerdiagspeed;}
    Playerdir = 3;
    state = "walking";
}

//defines what walking and standing mean actually mean
switch (state)
{
    case "walking":
        if keyleft then sprite_index = sPlayerwalkleft;
        if keyright then sprite_index = sPlayerwalkright;   
        if keyup then sprite_index = sPlayerwalkup;
        if keydown then sprite_index = sPlayerwalkdown;
        if keyupleft then sprite_index = sPlayerwalkupleft;
        if keyupright then sprite_index = sPlayerwalkupright;
        if keydownleft then sprite_index = sPlayerwalkdownleft;
        if keydownright then sprite_index = sPlayerwalkdownright;
    break;
       
    case "standing":
        if Playerdir = 4 then sprite_index = sPlayerleft;
        if Playerdir = 6 then sprite_index = sPlayerright;         
        if Playerdir = 8 then sprite_index = sPlayerup;
        if Playerdir = 2 then sprite_index = sPlayerdown; 
        if Playerdir = 7 then sprite_index = sPlayerupleft;
        if Playerdir = 9 then sprite_index = sPlayerupright;
        if Playerdir = 1 then sprite_index = sPlayerdownleft;
        if Playerdir = 3 then sprite_index = sPlayerdownright;       
    break;         
}


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 11, 2013, 06:34:54 PM
the result:
(http://puu.sh/3ZPfo.gif)



P R O G R A M M I N G

e: it's getting late though so i'm gonna disappear for a few hours here
i hope you're all laughing as hard at this situation as i am because there is no way the solution for this isn't some stupid obvious thing at this point


Title: Re: More movement issues!! Hooray!!
Post by: feminazi on August 11, 2013, 09:56:39 PM
Code:
//create
playerDirection = 0; //needs to be remembered

//start from right, counterclockwise
spriteWalk[0] = sPlayerwalkright;
spriteWalk[1] = sPlayerwalkupright;  
spriteWalk[2] = sPlayerwalkup;
spriteWalk[3] = sPlayerwalkupleft;
spriteWalk[4] = sPlayerwalkleft;
spriteWalk[5] = sPlayerwalkdownleft;
spriteWalk[6] = sPlayerwalkdown;
spriteWalk[7] = sPlayerwalkdownright;

spriteStanding[0] = sPlayerright;
spriteStanding[1] = sPlayerupright;  
spriteStanding[2] = sPlayerup;
spriteStanding[3] = sPlayerupleft;
spriteStanding[4] = sPlayerleft;
spriteStanding[5] = sPlayerdownleft;
spriteStanding[6] = sPlayerdown;
spriteStanding[7] = sPlayerdownright;

Code:
//step
var hmove = 0;
var vmove = 0;
var state = "standing";
var playerSpeed = 3;

//moving
if (keyboard_check(vk_left))
{
    hmove = -1;
}
else if (keyboard_check(vk_right))
{
    hmove = 1;
}
if (keyboard_check(vk_up))
{
    vmove = -1;
}
else if (keyboard_check(vk_down))
{
    vmove = 1;
}

//normalize corrects your diagonals length
var magnitude = sqrt((hmove * hmove) + (vmove * vmove));

//check if you even moved
if (magnitude > 0)
{
    //correct move vector then set speed.
    hmove = (hmove / magnitude) * playerSpeed;
    vmove = (vmove / magnitude) * playerSpeed;
    
    //only set direction if you moved.
    playerDirection = point_direction(0, 0, hmove, vmove);
    
    state = "walking";
}

//switch is dum
var i;
for (i=0; i<8; i++)
{
    var dirCheck = 45*i;
    if (playerDirection >= dirCheck - 22.5 && playerDirection < dirCheck + 22.5)
    {
        if (state == "walking")
            sprite_index = spriteWalk[i];
        else if (state == "standing")
            sprite_index = spriteStanding[i];
    }
}

x += hmove;
y += vmove;


Title: Re: More movement issues!! Hooray!!
Post by: Impmaster on August 11, 2013, 10:31:30 PM
the result:
(http://puu.sh/3ZPfo.gif)



P R O G R A M M I N G

e: it's getting late though so i'm gonna disappear for a few hours here
i hope you're all laughing as hard at this situation as i am because there is no way the solution for this isn't some stupid obvious thing at this point

I can really see your frustration in this image. :D


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 12, 2013, 02:37:26 AM
boop
for (i=0; i<8; i++) causes an error, should that be for (i=0; i<8; i+=1) instead?
also standing/walking become unknown variables and cause their own problems


Title: Re: More movement issues!! Hooray!!
Post by: rundown on August 12, 2013, 02:46:11 AM
boop
for (i=0; i<8; i++) causes an error, should that be for (i=0; i<8; i+=1) instead?
also standing/walking become unknown variables and cause their own problems

Normally that should do the exact same thing.  ???


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 12, 2013, 02:59:02 AM
yeah i had to look that up after i asked but for some reason GM gets really upset if you leave it as just i++

here lemme slap that part back on there
(http://puu.sh/404CK.png)

(http://puu.sh/404DD.png)
you have to change the way the vars are written too because it gets whiny about that but it doesn't affect the error with the actual for statement


Title: Re: More movement issues!! Hooray!!
Post by: nikki on August 12, 2013, 03:18:57 AM
don't know GM, for for this kind of issue, debugging would be alot simpler if you just started with 2 directions instead of 8,

just get 2 working, the add another 2 then 4 more.

now I can feel the panicky/clumpsy rewrites of repepititve code all over here, that must introduce some more bugs in the mix


Title: Re: More movement issues!! Hooray!!
Post by: ஒழுக்கின்மை on August 12, 2013, 03:24:48 AM
no offense, but this is the worst GML code i've ever seen, hahahaha. comparing strings to check states?  :waaagh:

but anyway, yeah, i++ doesn't work in gm7 or gm8. however it does work in studio


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 12, 2013, 03:27:17 AM
don't know GM, for for this kind of issue, debugging would be alot simpler if you just started with 2 directions instead of 8,

just get 2 working, the add another 2 then 4 more.

now I can feel the panicky/clumpsy rewrites of repepititve code all over here, that must introduce some more bugs in the mix
no offense, but this is the worst GML code i've ever seen, hahahaha. comparing strings to check states?  :waaagh:

but anyway, yeah, i++ doesn't work in gm7 or gm8. however it does work in studio
yeah ahaha

its such a jumbled mess of I don't even know what at this point redoing everything from scratch would probably just be the simplest thing


Title: Re: More movement issues!! Hooray!!
Post by: rundown on August 12, 2013, 03:34:32 AM
That is so wierd.
In c++ it would be
for(var i=0; i<8; i++){
}

the scope for I would only be within the for loop.


Title: Re: More movement issues!! Hooray!!
Post by: gggfhfdh on August 12, 2013, 03:50:16 AM
actually whoops okay i put evelyn's code back in and the problem with the state variables was

in the draw event (for having the shadow follow you) because state is defined in the step event rather than create in their example

switching that around fixes everything up (THANK YOU. BY THE WAY.) i'm still going to redo everything from scratch just for the sake of practice, but at least i've got something competently written to reference now lmao
but anyway, yeah, i++ doesn't work in gm7 or gm8. however it does work in studio
also yeah re: this i should probably update to studio sooner rather than later


Title: Re: More movement issues!! Hooray!!
Post by: nikki on August 12, 2013, 05:24:00 AM
Quote
That is so wierd.
In c++ it would be
for(var i=0; i<8; i++){
}
the scope for I would only be within the for loop.

you and your fancy block-scope ;)




Title: Re: More movement issues!! Hooray!!
Post by: Ant on August 12, 2013, 05:32:27 AM
also yeah re: this i should probably update to studio sooner rather than later

They're currently doing a deal for GM 7/8 users where you can upgrade to Studio for $10, dunno how long it will last.


Title: Re: More movement issues!! Hooray!!
Post by: feminazi on August 13, 2013, 12:39:34 PM
gm studio lets u declare var i = dum; and i++;