Code error

Q

Qing Donnie Yoshi

Guest
Ok so I believe I managed to find a code to work work Tile collision but once I tried to run the program this pops up:


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_Player_1:

Variable obj_Player_1.xx(100008, -2147483648) not set before reading it.
at gml_Object_obj_Player_1_Step_0 (line 24) - x += xx
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_Player_1_Step_0 (line 24)

does anyone know what this means and what I should do?
 
Q

Qing Donnie Yoshi

Guest
It means you didn't set xx. Somewhere in that code you probably missed a line that starts with

var xx =

Or you put that line in the wrong code block.
OK so i got that problem fixed but now the player is just moving endlessly.
 
J

JON213

Guest
There is no condition telling it when to run. So consiquently it is run endlessly.
 
Q

Qing Donnie Yoshi

Guest
There is no condition telling it when to run. So consiquently it is run endlessly.
ok so i set the xx n yy to 0 and it's not moving like it said. so what I'm I'm trying to do is have Hspeed n Vspeed activeto have the player move like a traditional pac-man does. where do i go from there?
 

2Dcube

Member
Can you explain more precisely what you want?
Do you want the player to move with keyboard keys in 4 directions?
Or just by itself?
 
Q

Qing Donnie Yoshi

Guest
Can you explain more precisely what you want?
Do you want the player to move with keyboard keys in 4 directions?
Or just by itself?
in 4 directions but i want it to keep moving just like how pac-man does
 

TheouAegis

Member
If you don't want to use the built-in variables, then you will need to make your own variables to substitute for them.

Create:
HSP = 0;
VSP = 0;
DIR = 2;
newDIR = DIR;

Step:
Code:
var h= keyboard-check(vk_right) - keyboard_check(vk_left);
var v = keyboard_check(vk_down) - keyboard_check(vk_up);

//Prevent conflicting diagonals
if h != 0 && v != 0 {
    h = 0;
    v = 0;
}

if h | v != 0 {        //check if either h or v is set
   var xx = 2 - h,
        yy = 1 + v;

    //Allow reversing direction at any time
    if DIR = (xx + 2) mod 4
    {
        DIR = xx;
        newDIR = DIR;
    }
    else
    if DIR == (yy + 2) mod 4
    {
        DIR = yy;
        newDIR = DIR;
    }
    else
    if DIR != xx and DIR != yy {
        if h != 0
            newDIR = xx;
       else
           newDIR = yy;
    }
}

if (x-32 + y-32) mod 64 == 0 {       //If at an intersection
    if newDIR != DIR {
        var n;
        switch newDIR {
            case 0: //set n to 1 if there is a tile above;
                         break;
            case 1: //set n to 1 if there is a tile to the right;
                         break;
            case 2: //set n to 1 if there is a tile below;
                         break;
            case 3: //set n to 1 if there is a tile to the left;
                         break;
        }
        if n
            newDIR = DIR;
        else
            DIR = newDIR;
    }
    else
    switch DIR {
        case 0: //if there is a tile above, set DIR to -1;
                     break;
        case 1: //if there is a tile to the right, set DIR to -1;
                     break;
        case 2: //if there is a tile below, set DIR to -1;
                     break;
        case 3: if there is a tile to the left, set DIR to -1;
                     break;
    }
}

//Replace the "1" with your speed
    switch DIR {
        case 0: y -= 1; break;
        case 1: x += 1; break;
        case 2: y += 1; break;
        case 3: x -= 1; break;
    }

I could speed things up a tad with arrays, but this is slightly easier to understand. The "//if there is a tile" parts I left out because you'll need to write your own tile proximity checks. The (x-32 + y-32) mod 64 part assumes your player sprite's origin is centered and that your walls are 64x64 in size.
 

2Dcube

Member
Nice code, but incomprehensible to someone unfamiliar with basic error messages. They won't be able to adjust it to their liking or know how to handle "tile proximity checks".

in 4 directions but i want it to keep moving just like how pac-man does
I'm not sure how Pac-man moves but do you mean when he's at a corner he will automatically turn and continue?

Firstly try to get it moving on a grid. For example, move by a speed of 2 with
xx += 2 // to move right
xx -= 2 // to move left
yy += 2 // to move down
yy -= 2 // to move up
Let's say the grid is 32x32
Make sure the player starts at something you can divide by 32, for example start at x:320 y:320 (put the player object there in the room)

Now move it in a direction every step (that's in the Step Event) with one of the lines above.
Since we're moving at a speed of 2, at some point we will be exactly in the middle of a next cell of the grid.
You want Pac-man to move on this grid and not in between, so he should only be able to change directions when you're exactly in the middle of a tile.
To check if you are you can use "mod" such as TheouAegis above me did, for example:
Code:
if xx mod 32 = 0 and yy mod 32 = 0
{
   show_message("I'm in the middle of a grid-cell now!")
}
mod is like doing a division and returning the remainder, look up "modulo" if interested.

Maybe a good start?
 

TheouAegis

Member
He should eventually be able to handle tile proximity checks because this is the second thread he's posted about it. I went over some of this stuff with him in his other thread about the ghost AI.
 
Q

Qing Donnie Yoshi

Guest
He should eventually be able to handle tile proximity checks because this is the second thread he's posted about it. I went over some of this stuff with him in his other thread about the ghost AI.
wait you did? i didn't get a notif of your reply on the first post.
 
Q

Qing Donnie Yoshi

Guest
Nice code, but incomprehensible to someone unfamiliar with basic error messages. They won't be able to adjust it to their liking or know how to handle "tile proximity checks".


I'm not sure how Pac-man moves but do you mean when he's at a corner he will automatically turn and continue?

Firstly try to get it moving on a grid. For example, move by a speed of 2 with
xx += 2 // to move right
xx -= 2 // to move left
yy += 2 // to move down
yy -= 2 // to move up
Let's say the grid is 32x32
Make sure the player starts at something you can divide by 32, for example start at x:320 y:320 (put the player object there in the room)

Now move it in a direction every step (that's in the Step Event) with one of the lines above.
Since we're moving at a speed of 2, at some point we will be exactly in the middle of a next cell of the grid.
You want Pac-man to move on this grid and not in between, so he should only be able to change directions when you're exactly in the middle of a tile.
To check if you are you can use "mod" such as TheouAegis above me did, for example:
Code:
if xx mod 32 = 0 and yy mod 32 = 0
{
   show_message("I'm in the middle of a grid-cell now!")
}
mod is like doing a division and returning the remainder, look up "modulo" if interested.

Maybe a good start?
So if I want him to move along the grid would his collision mask have to be full image?
 

2Dcube

Member
That would make sense I think yeah if you want to use collisions for movement.
There are also ways to do it without collisions, for example by using a grid data structure and storing where walls are, but it's a lot more complicated.
The other thing is if you use a grid data structure you'd have to build levels in code or make your own editor,
so unless you want to go very advanced using collisions and the Room Editor is probably easiest.

Then, when you are in the middle of a grid-cell, check for collisions on the next cell, and if there is a wall you know you can't move forward.
 
Q

Qing Donnie Yoshi

Guest
He should eventually be able to handle tile proximity checks because this is the second thread he's posted about it. I went over some of this stuff with him in his other thread about the ghost AI.
hey do happen to somehow still have same code for said ghost ai by any chance?
 
Top