What is wrong with this code for keyboard movement?

Started by tbook, Thu 07/03/2024 12:35:52

Previous topic - Next topic

tbook

Hi folks, I'm trying to help myself with AGS scripting using ChatGPT but I keep getting error messages.
I thought that it will be easier this way, but maybe that's a mistake after all...

Code: ags
// Define a function to handle character movement
function HandleCharacterMovement() {
    if (IsKeyPressed(eKeyDownArrow)) {
        cPiotr.Animate(1, eOnce, eNoBlock); // Change '1' to the view number for walking down animation
        cPiotr.y += 2; // Adjust the value to control the movement speed
    }
    else if (IsKeyPressed(eKeyUpArrow)) {
        cPiotr.Animate(1, eOnce, eNoBlock); // Change '2' to the view number for walking up animation
        cPiotr.y -= 2; // Adjust the value to control the movement speed
    }
    else if (IsKeyPressed(eKeyRightArrow)) {
        cPiotr.Animate(1, eOnce, eNoBlock); // Change '3' to the view number for walking right animation
        cPiotr.x += 2; // Adjust the value to control the movement speed
    }
    else if (IsKeyPressed(eKeyLeftArrow)) {
        cPiotr.Animate(1, eOnce, eNoBlock); // Change '4' to the view number for walking left animation
        cPiotr.x -= 2; // Adjust the value to control the movement speed
    }
    
    
}




// called on every game cycle, even when the game is blocked
function repeatedly_execute_always()
{
  HandleCharacterMovement();
}

Khris

You don't describe what the problem is. Anyway, do not use ChatGPT to write AGS code.
One issue with your code is that the Animate command expects the loop and delay as the first two parameters. You're also supposed to lock in a view first.

AGS comes with a KeyboardMovement module that is part of the Sierra template for instance. Setting the movement mode to pressing will do exactly what you're looking for.

In general, doing a hold down to walk movement code means you need to watch for a) the user starting to hold down a key b) the key being released again.
Event a) means you send off the character in the appropriate direction using a non-blocking walk command. Event b) means you stop the character. That way you can use the built-in walk behavior, which is by far the easiest way to implement this.

The way I'd implement this is to keep track of two values:
int x = IsKeyPressed(eKeyRightArrow) - IsKeyPressed(eKeyLeftArrow);
and
int y = IsKeyPressed(eKeyDownArrow) - IsKeyPressed(eKeyUpArrow);
(if you force bools into additions / subtractions, they get cast to 0 / 1)

These formulas will produce -1, 0 or 1 for both x and y. By keeping the previous values for both x and y, you can now check in rep_exe if they have changed since the previous frame.
If either x or y has changed you can stop the player (since we need to do this regardless). Then you calculate new target coordinates and send them off again using player.Walk(player.x + 10000*x, player.y + 10000*y);

Here's code:

Code: ags
int prevX, prevY;
function HandleCharacterMovement() {
  int x = IsKeyPressed(eKeyRightArrow) - IsKeyPressed(eKeyLeftArrow);
  int y = IsKeyPressed(eKeyDownArrow) - IsKeyPressed(eKeyUpArrow);
  if (x != prevX || y != prevY) {
    player.StopMoving();
    player.Walk(player.x + 10000*x, player.y + 10000*y);
  }
  prevX = x;
  prevY = y;
}

Crimson Wizard

I suppose that ChatGPT may be used to draft the code, but you cannot use it as-is, you must always check the manual for functions in the code, because ChatGPT is often wrong about how they are used and what they do.

If you look in the manual for Animate function, you would see that ChatGPT is using them incorrectly, and its commentary is also wrong:
https://adventuregamestudio.github.io/ags-manual/Character.html#characteranimate

eri0o


SMF spam blocked by CleanTalk