Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Kinoko on Thu 17/06/2004 06:58:32

Title: Character running - SOLVED
Post by: Kinoko on Thu 17/06/2004 06:58:32
I've implemented the script that allows action-style movement into my script and it works perfectly. My character walks when direction keys are held down with no problems.

Now, though, I'd like to be able to have the character run when I hold down the 'A' button. Basically, if I hold down 'A', and then press a direction key. I want him to run, OR if I am holding a direction key (ie walking) and while doing that, I hold down 'A', I want him to start running from the walk.

I realise you cannot change a characters speed while he is moving, so I was trying to achieve this by having him stop for a fraction of a second and then start running (a little jerkiness is a small sacrifice, I think). So, I've been mussing around with the code for awhile and I can get him to run when the 'A' button is pressed, but only after it has been let go. If I walk and then hold the A button, EGO stops until I release 'A' and then runs.

Also, if I let go of the direction button after that (stopping the character) and then hit a direction button again, he'll still be in run mode. I'm not sure exactly where I should reset his speed back to normal.

Here's the entire repeatedly execute script in my global script:
Quote
function repeatedly_execute() {
Ã,  // put anything you want to happen every game cycle here

// --- keyboard control ---
int CharId, Direction, dx, dy;
// neue Richtung ermitteln
if ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_UP_LEFT;
else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (377) > 0))) Direction = DIR_UP_RIGHT;
else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_DOWN_LEFT;
else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (377) > 0))) Direction =DIR_DOWN_RIGHT;
else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP;
else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT;
else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP;
else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT;
else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN;
else Direction = DIR_STOP;
// Vergleich mit aktueller Richtung
if (PrevDirection != Direction)
{
PrevDirection = Direction;
CharId = GetPlayerCharacter ();
if (Direction == DIR_STOP) { StopMoving (CharId); } // 5 Stop (numeric pad)
else
{
if (Direction == DIR_UP_LEFT) { dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; } // 7 Home (numeric pad)
else if (Direction == DIR_UP) { dx = 0; dy = -DIR_DISTANCE; } // 8 Up arrow
else if (Direction == DIR_UP_RIGHT) { dx = DIR_DISTANCE; dy = -DIR_DISTANCE; } // 9 PgUp (numeric pad)
else if (Direction == DIR_LEFT) { dx = -DIR_DISTANCE; dy = 0; } // 4 Left arrow
else if (Direction == DIR_RIGHT) { dx = DIR_DISTANCE; dy = 0; } // 6 Right arrow
else if (Direction == DIR_DOWN_LEFT) { dx = -DIR_DISTANCE; dy = DIR_DISTANCE; } // 1 End (numeric pad)
else if (Direction == DIR_DOWN) { dx = 0; dy = DIR_DISTANCE; } // 2 Down arrow
else if (Direction == DIR_DOWN_RIGHT) { dx = DIR_DISTANCE; dy = DIR_DISTANCE; } // 3 PgDn (numeric pad)
MoveCharacterStraight (CharId, character [CharId].x + dx, character [CharId].y + dy);
}
}

Ã,  if (IsKeyPressed(65)==1) { //RUNNING - 'A' button held down
Ã,  Ã, 
Ã,  Ã,  Ã, if (Direction == DIR_UP_LEFT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; }
Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_UP) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = 0; dy = -DIR_DISTANCE; } // 8 Up arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_UP_RIGHT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = DIR_DISTANCE; dy = -DIR_DISTANCE; } // 9 PgUp (numeric pad)
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_LEFT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = -DIR_DISTANCE; dy = 0; } // 4 Left arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_RIGHT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = DIR_DISTANCE; dy = 0; } // 6 Right arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_DOWN_LEFT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = -DIR_DISTANCE; dy = DIR_DISTANCE; } // 1 End (numeric pad)
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_DOWN) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = 0; dy = DIR_DISTANCE; } // 2 Down arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_DOWN_RIGHT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = DIR_DISTANCE; dy = DIR_DISTANCE; } // 3 PgDn (numeric pad)
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, MoveCharacterStraight (EGO, character [EGO].x + dx, character [EGO].y + dy);
Ã,  Ã,  Ã, }
Ã,  Ã, 
}

I'm sure it's as messy as hell but I've been steadily making progress up until now. I can't really think of how to get around this though I'm sure it's obvious. Can anyone help?
Title: Re: Character running
Post by: Scorpiorus on Thu 17/06/2004 08:51:25
Something like that should make the trick, but let us know how (and if) it works:

int isRunning = 0;

function repeatedly_execute() {
Ã,  // put anything you want to happen every game cycle here

// --- keyboard control ---
int CharId, Direction, dx, dy;
// neue Richtung ermitteln
if ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_UP_LEFT;
else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (377) > 0))) Direction = DIR_UP_RIGHT;
else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_DOWN_LEFT;
else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (377) > 0))) Direction =DIR_DOWN_RIGHT;
else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP;
else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT;
else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP;
else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT;
else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN;
else Direction = DIR_STOP;


if (IsKeyPressed(65)==1) { //RUNNING - 'A' button held down

   if (isRunning==0) {
      StopMoving(GetPlayerCharacter());
      SetCharacterSpeed(GetPlayerCharacter(), RUN_SPEED);
      isRunning = 1;
      Direction = DIR_STOP;
   }

} else {

   if (isRunning==1) {
      StopMoving(GetPlayerCharacter());
      SetCharacterSpeed(GetPlayerCharacter(), WALK_SPEED);
      isRunning = 0;
      Direction = DIR_STOP;
   }
}



// Vergleich mit aktueller Richtung
if (PrevDirection != Direction)
{
PrevDirection = Direction;
CharId = GetPlayerCharacter ();
if (Direction == DIR_STOP) { StopMoving (CharId); } // 5 Stop (numeric pad)
else
{
if (Direction == DIR_UP_LEFT) { dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; } // 7 Home (numeric pad)
else if (Direction == DIR_UP) { dx = 0; dy = -DIR_DISTANCE; } // 8 Up arrow
else if (Direction == DIR_UP_RIGHT) { dx = DIR_DISTANCE; dy = -DIR_DISTANCE; } // 9 PgUp (numeric pad)
else if (Direction == DIR_LEFT) { dx = -DIR_DISTANCE; dy = 0; } // 4 Left arrow
else if (Direction == DIR_RIGHT) { dx = DIR_DISTANCE; dy = 0; } // 6 Right arrow
else if (Direction == DIR_DOWN_LEFT) { dx = -DIR_DISTANCE; dy = DIR_DISTANCE; } // 1 End (numeric pad)
else if (Direction == DIR_DOWN) { dx = 0; dy = DIR_DISTANCE; } // 2 Down arrow
else if (Direction == DIR_DOWN_RIGHT) { dx = DIR_DISTANCE; dy = DIR_DISTANCE; } // 3 PgDn (numeric pad)
MoveCharacterStraight (CharId, character [CharId].x + dx, character [CharId].y + dy);
}
}

}

EDIT: to add StopMoving(GetPlayerCharacter()).
Title: Re: Character running
Post by: Kinoko on Thu 17/06/2004 09:11:38
Pending...
Title: Re: Character running
Post by: Scorpiorus on Thu 17/06/2004 13:56:24
I've tested the script and just wanted to mention that there is also the StopMoving(GetPlayerCharacter()); function should be invoked before changing the speed. I updated the previous post to reflect that fact. :)
Title: Re: Character running
Post by: Kinoko on Thu 17/06/2004 15:05:37
I just managed to test it (had that other typing problem before stopping me from testing this one) and was about to say the exact same thing ^_^

It works GREAT now, thanks heaps! Scorpiorus, you're aaaaaaallright. ^_-
Title: Re: Character running - SOLVED!
Post by: Kinoko on Fri 25/06/2004 06:33:14
*groan* I have to dredge this one up again.

Everything works fine except for running up/left. Walking up/left works, but if I try to do it while running, it won't (it was just continue to go either straight left or up).


// --- keyboard control ---
int CharId, Direction, dx, dy;
// neue Richtung ermitteln
if ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_UP_LEFT;
else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (377) > 0))) Direction = DIR_UP_RIGHT;
else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_DOWN_LEFT;
else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (377) > 0))) Direction =DIR_DOWN_RIGHT;
else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP;
else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT;
else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP;
else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT;
else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN;
else Direction = DIR_STOP;

if (IsKeyPressed(65)==1) { //RUNNING - 'A' button held down

Ã,  Ã, if (isRunning==0) {
Ã,  Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  Ã,  SetCharacterSpeed(GetPlayerCharacter(), 12);
Ã,  Ã,  Ã,  isRunning = 1;
Ã,  Ã,  Ã,  Direction = DIR_STOP;
Ã,  Ã, }

} else {

Ã,  Ã, if (isRunning==1) {
Ã,  Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  Ã,  SetCharacterSpeed(GetPlayerCharacter(), 6);
Ã,  Ã,  Ã,  isRunning = 0;
Ã,  Ã,  Ã,  Direction = DIR_STOP;
Ã,  Ã, }
}

// Vergleich mit aktueller Richtung
if (PrevDirection != Direction)
{
PrevDirection = Direction;
CharId = GetPlayerCharacter ();
if (Direction == DIR_STOP) { StopMoving (CharId); } // 5 Stop (numeric pad)
else
{
if (Direction == DIR_UP_LEFT) { dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; } // 7 Home (numeric pad)
else if (Direction == DIR_UP) { dx = 0; dy = -DIR_DISTANCE; } // 8 Up arrow
else if (Direction == DIR_UP_RIGHT) { dx = DIR_DISTANCE; dy = -DIR_DISTANCE; } // 9 PgUp (numeric pad)
else if (Direction == DIR_LEFT) { dx = -DIR_DISTANCE; dy = 0; } // 4 Left arrow
else if (Direction == DIR_RIGHT) { dx = DIR_DISTANCE; dy = 0; } // 6 Right arrow
else if (Direction == DIR_DOWN_LEFT) { dx = -DIR_DISTANCE; dy = DIR_DISTANCE; } // 1 End (numeric pad)
else if (Direction == DIR_DOWN) { dx = 0; dy = DIR_DISTANCE; } // 2 Down arrow
else if (Direction == DIR_DOWN_RIGHT) { dx = DIR_DISTANCE; dy = DIR_DISTANCE; } // 3 PgDn (numeric pad)
MoveCharacterStraight (CharId, character [CharId].x + dx, character [CharId].y + dy);
}
}
Title: Re: Character running
Post by: Gilbert on Fri 25/06/2004 06:53:55
Hmmm did it not work in all of the below key combinations, or just one of them? (I think you understand what they mean)

1) A+L+U
2) A+LU (ie. Home)

If it only failed on 1), it's possible that it's a hardware limitation (in some keyboards it may not be able to recognized the whole combination if 3 or more keys are pressed at the same time).

--EDIT--
I think I misread something, it's seems that the 'A' key toggle between walk and run mode, not running while it's held. Forget above lines then.
Title: Re: Character running
Post by: Kinoko on Fri 25/06/2004 07:13:21
Ah yeah, the A key needs to be held to run, but holding A doesn't make the character run. A along with a directional key need to be held for the character to run.
Title: Re: Character running
Post by: Gilbert on Fri 25/06/2004 07:17:53
Actually I didn't read the whole thing fully, but I have some questions on it, wouldn't the current arrangement of the "if (IsKeyPressed(65)==1) " part made the character always stops when "A" was being pressed, even if the direction keys are pressed with it? Maybe I had misread something.
Title: Re: Character running
Post by: Scorpiorus on Fri 25/06/2004 14:50:11
Kinoko: Hmm, I gave it a test and all seem to work fine. I think Gilbert is right, it may be the keyboard can't handle a combination of three certain keys that are being held down at the same time. Try setting another key instead of 'A' or replacing the whole if (IsKeyPressed(65)==1) with if(IsButtonDown(RIGHT)==1) to test if works with a right mouse button being held down.

Gilbert: There is a isRunning variable that prevents the speed changing script to run multiple times.
Title: Re: Character running
Post by: Kinoko on Sun 27/06/2004 14:40:34
Hmm, seems you're right. I didn't realise the keyboard worked that way before now. My downstairs keyboard is fine but my other computer has the problem. It won't work when I push 'A', left arrow and up arrow at the same time. Using the numberic keypad is fine though. Thanks both of you, anyway.
Title: Re: Character running - SOLVED
Post by: Hollister Man on Fri 02/07/2004 04:01:52
Sorry to post in a solved thread, but is either the ctrl or alt button used?  These only activate the isbuttonpressed action, and are frequently wired differently so as to be used for exactly this kind of action (well, sorta)
Title: Re: Character running - SOLVED
Post by: Kinoko on Tue 17/08/2004 07:27:39
I have to pull this old thread up once more. It all works fine, but I've noticed that if a character is walking and another key is pressed (say to attack or something), the character will stop and do that new action (which is great) but then won't continue walking.

Basically, instead of the game engine detecting whenever the direction keys are held down (and another action isn't being performed), it's only detecting when the direction keys are actually pushed. If I'm walking, do another action, but am still holding down a direction key after the action has completed, I'd like the character to continue walking. As it is, it feels very jerky and is rather annoying to constantly have to repush direction keys in battle.
Title: Re: Character running
Post by: Pumaman on Wed 18/08/2004 20:00:45
This is probably due to the PrevDirection variable you have, that stops it restarting the move if it's in the same direction. Perhaps you could try this:

REPLACE:

if (PrevDirection != Direction)

WITH:

if ((PrevDirection != Direction) || (character[EGO].walking == 0))
Title: Re: Character running
Post by: Kinoko on Fri 20/08/2004 01:32:06
Thanks heaps, that worked perfectly.
Title: Re: Character running - SOLVED
Post by: R4L on Fri 20/08/2004 04:36:44
I just want to know. Do any functions have to be imported?
Title: Re: Character running - SOLVED
Post by: Kinoko on Fri 20/08/2004 13:39:26
You should just have to stick that final code in your repeatedly_execute section and voila. So, no.
Title: Re: Character running - SOLVED
Post by: prowler on Fri 20/08/2004 23:53:43
i know this has nothing to do with your problem, but i was thinking, maybe instead of A (65 is it?) you could declare an int which the player would have the possibility to redefine?

i hate games where you can't redefine your keys; especially when there's a lot of them...

just a suggestion  :)
Title: Re: Character running - SOLVED
Post by: Kinoko on Sun 22/08/2004 04:48:53
No problem there, I am considering that for the future. For now though, I'm just getting my basics worked out.