Gw Temp

Menu

Tutorial - 'Walk to X/Y Location' by Guest

An item about RPGMaker 2000 posted on

Blurb

Learn how to move your character to a certain X/Y Location.

Body


I apologise if this is spaced out wrongly this tutorial submit form limits width.

Ok basically this tutorial is to help you implement features like being able to place a cursor onto a location
on a map and having your character walk there.
In this tutorial i will show you how to code it so that you can walk to a location with
x and y coordinates that are specified by two variables.

There are two ways of going about making the movement event; you can do the plain old step up,
left, down e.t.c. commands or you can use the step up-right and so on commands which look better
but involve a little bit more programming.
In this tutorial i will show you how to create the latter better version.

To start off with you will need four variables: DestinationX, DestinationY, CurrentX and CurrentY,
after you have created these we can begin.

You need an event which lets you store to values into the destination x and destination y variables.
(do this with the input no command on page 1 of the action list and make sure you make a text box
show which variable you are entering the value into.
Now create a common event called 'goto coordinates' leave it on call.
Now after entering the variables have it call the common event we have just made.
(at this point put in fork conditions to tell if the coordinates entered are offscreen or over
a river or something so that the event dosn't try to walk into impossible terrain therfore
locking up the rpg)

..ready? OK.
First i will show you the code, don't worry if it doesn't mean much to you yet - all will be revealed.


<>Label No1
<>Comment: Get Actual X,Y coordinates
----------------
<>Set Variable[CurrentX] to hero X coordinate
<>Set Variable[CurrentY] to hero Y coordinate
<>Comment: Check if you are at destination
----------------
<>Fork Condition: Var[CurrentX] the same as Var[DestinationX]
<>Fork Condition: Var[CurrentY]is the same as Var[DestinationY]
<>Comment: Finish Event
-------------------------
<>Goto Label No2
<>EndCase
<>EndCase

<> Comment: Y is Bigger
---------------
<>Fork Condition: Var[CurrentY] is bigger than Var[DestinationY]
<>Fork Condition: Var[CurrentX] is bigger than Var[DestinationX]
<>MoveEvent: Hero: Left-Up
<>MoveAll
<>Goto Label No1
<>ElseCase
<>Fork Condition: Var[CurrentX] is smaller than Var[DestinationX]
<>MoveEvent: Hero: Right-Up
<>MoveAll
<>Goto Label No1
<>ElseCase
<>MoveEvent: Hero: Up
<>MoveAll
<>Goto Label No1
<>EndCase
<>EndCase
<>EndCase
<> Comment: Y is Smaller
---------------
<>Fork Condition: Var[CurrentY] is smaller than Var[DestinationY]
<>Fork Condition: Var[CurrentX] is bigger than Var[DestinationX]
<>MoveEvent: Hero: Left-Down
<>MoveAll
<>Goto Label No1
<>ElseCase
<>Fork Condition: Var[CurrentX] is smaller than Var[DestinationX]
<>MoveEvent: Hero: Right-Down
<>MoveAll
<>Goto Label No1
<>ElseCase
<>MoveEvent: Hero: Down
<>MoveAll
<>Goto Label No1
<>EndCase
<>EndCase
<>EndCase

<> Comment: X is Smaller
---------------
<>Fork Condition: Var[CurrentX] is smaller than Var[DestinationX]
<>MoveEvent: Hero: Left
<>MoveAll
<>Goto Label No1
<>EndCase

<> Comment: X is Bigger
---------------
<>Fork Condition: Var[CurrentX] is bigger than Var[DestinationX]
<>MoveEvent: Hero: Right
<>MoveAll
<>Goto Label No1
<>EndCase

<>Label No2
<>


And there it is!
Although the code might look a little complex, it isn't.
First things first, at first we put in a label, this is a point the program can loop forward to or jump backwards to
in the code, we are putting onw here so that every time the hero has moved we can jump back to a spot
were the hero x and y coordinates are reset.

Now we have to variable changes, this is setting the current x and y positions of the hero into variables.

Now we test if we are at the location already by checking if the current x ands y coordinates are the
same as the destination x and y coordinates and if they are we jump to label no 2 which is at the end
of the programming, thereby ending the walk event.

Now we find out if the current y is larger than the target y, if this is the case we then know that the
hero is lower on the screen then we want him to be, no wecheck the x coorsdinate as well to see
if we can get twice as near to the target by using diagonal movement, if x is bigger than the
target x then we are too far to the right of our objective and there foe we are below and to the right of the
destination, so you then tell it to move Up-Left, the rest of the coding contains the rest of the alternative directions
you can now probably understand it yourself.

The moveall after moving is the the hero can move before the x and y coordinates are updated so you don't
overshoot the destination.