Charas-Project

  • Home
  • Help
  • Search
  • Calendar
  • Login
  • Register
*
Please login or register.

Login with username, password and session length
 

News:

New forum theme up and running!



  • Charas-Project »
  • Game Creation »
  • Requests »
  • RPG Maker Programming »
  • The X and Y Co-ordinate Variables
« previous next »
  • Print
Pages: [1]

Author Topic: The X and Y Co-ordinate Variables  (Read 2540 times)

Offline AsakuraHao2004


  • RAM-TORTOISE!!
  • Exemplar
  • *
  • Posts: 1,623
  • AIM: dakutenshisdemon YIM: desimodontidae
The X and Y Co-ordinate Variables
« on: December 05, 2005, 01:04:52 AM »
-========================================-
::::::::::::::::::::X and Y Co-ordinate Tutorial::::::::::::::::::::
-========================================-
-
--
---
-========-
::Contents::

1) Introduction

2) The Basics
--2a) Setup
--2b) Testing

3) Applied Knowledge
--3a) The Basic Rock-on-Button
--3b) Expanding
---I. Moving the Rock
---II. Hero Trigger Button
---III. Moving The Hero
--3c)Projectiles

4) Conclusion


-========-
---
--
-
-============-
::1) Introduction::
-============-
Welcome to another tutorial by yours truely. Let's get right down to it. Programming using the X and Y sprite co-ordinate variables can let you do a very wide array of things that normally wouldn't be possible with simple event actions. You'll need to already have an understanding of variables, switches, and just general RM2K3 common sense. And yes, this tutorial is written using the 2003 version of RPG Maker, seeing as how it handles variable options better than 2000 (in my experiences anyways), and it's a bit "cleaner".

-===========-
::2) The basics::
-===========-
What exactly are the X and Y co-ordinates you may ask? Well, when you go into "Variable Operations" in the event commands, in the "Operand" section, there is a "Sprite" option. When checked, there is a pull-down menu, in which you can select events on the current map. Next to that is another pull-down menu, which selects your options. You should see a total of 6 options, however, we're only concerned about "X co-ordinate" and "Y co-ordinate". The other ones arent important for this tutorial.

----------------
:2a) Setup:
----------------
Now I know we can get really crazy with these setups, but what we want to do is SET the co-ordinates. What we are about to do is make it to where the game will tell itself where the hero is on the map at any given time. Think of it as a GPS.

First, make 2 new variables, one called "Hero X", and one called "Hero Y".

Now, make a new event. Don't give it a graphic, make it under the hero, stationary. MAKE SURE THAT IT IS PARALELLE PROCESSED. That is very important. Now, inside the event commands box, choose "Variable Operations". In the "Variable to change" section, check "Single Variable", click [...] and chose "Hero X". Under the "Operations" section, check "Set Equal". Now, under the "Operand" section, check "Sprite", scroll down the first drop-down menu and select "Hero". Go to the second drop-down menu juxtaposed to the first and select "X co-ordinate".

We now have the hero's X co-ordinates set, now we need to make the Y co-ordinates set. Basically, you do the same thing as before, except instead of selecting "Hero X" in "Variable to Change", select "Hero Y". Down in "Operands", select "Y co-ordinate" instead of "X co-ordinate. Everything else should remain the same.

Script should look something like this:

<>Variable Oper: [0001: Hero X] Set, Hero X Coord.
<>Variable Oper: [0002: Hero Y] Set, Hero Y Coord.

Yep. It's that simple.

-------------------
:2b) Testing:
-------------------
Ok, assuming you know something about scripting and variables, this should be simple. Go into the database, go into common events, and make a new common event. Name it whatever you like, and make a key input processing. In the "Store Key Code in" section, make a new variable to store it in. For example [0003: Pressed Button]. Under "Options" only check "Wait until key pressed". Under "Keys to Process", only check "Decision Key (5)". Now, make a conditional branch.

Check "Variable", hit [...] and select [0003: Pressed Button]. Uncheck "Execute Custom Handler...". Now, inside the branch, make a message. "Your X position is V[0001]. Your Y position is V[0002]."

Why we do this is because whenever you put a "V" in a message, it tells the game to display the value of a variable. Which variable that is comes next in brackets. You put the variable's number in the brackets that you want to display, and since the hero's X is kept track of in variable number [0001], if we have a message that says "Your X position is V[0001].", it will read "Your X position is {number}." in the game.

Script should look something like this:

<>Key Input Proc: [0003: Pressed Button]
<>Branch if Var [0003: Pressed Button] is 5
-<>Message: Your X position is \V[0001]. Your Y position is \V[0002].
-<>
:End
<>

Now test play the game. Hit the decision key (whichever one you prefer) and a message should display something like "Your X position is 12. Your Y position is 4." Now move somewhere else and hit the decision key again. If you did it right, you should get the same message, but with different numbers.

If you're familiar at all with RPG Maker scripting, you should recognize this as a basic key input process and message display. Also note that you can make this an event on the map instead of a common event.

-=================-
::3) Applied Knowledge::
-=================-
Now that we have down how to set variable for the hero, what other fun and exciting things can we use this bit of scripting knowledge for? Well, several things. I'll explain by slowly introducing one of the most commonly used actions in RPG's..

----------------------------------------------
:3a) The Basic Rock-on-Button:
----------------------------------------------
Aye, the very same. You have to move a rock somewhere in the room and set it on a little button in another part of the room to make a door open. This is one of the simplest things to code with the X and Y co-ordinates, and yet opens your mind to so many more possibilities once you understand how this works. In fact, I'd say a good 90% of all the "advanced" ABS and CBS systems are modeled after the basic principle of this script.

Anyways, lets get down to it. First, you'll need to make a new switch. We will call it [0001: Rock On Button].

First, make a rock event somewhere on the map. Name this event "Rock" You can adjust the speed if you want, but make sure that it's the same level as the hero. Under "triggered by" set it to "Action Key". It could also be set to "On hero touch", but I reccomend using the former. In the event commands, chose "Move Event", from the pull-down menu select "This event" and click "Move away from hero". Check "Ignore Impossible Moves".

Now make another event, this event should be the button. It doent need anything except a button graphic and it needs to be set to "Below Hero". Also, name it "Button".

The first event we made back in section 2a should still be on the map. Open it up. Now, remember how we set the X and Y co-ordinates for the hero? Of course you do! Well, we can also set the co-ordinates of other events on the map, not just the hero. In case you forgot, Ill go through it in detal.

First make 4 new variables. "Rock X", "Rock Y", "Button X", and "Button Y".

Inside the event commands box (should be under the hero's X and Y variable settings), choose "Variable Operations". In the "Variable to change" section, check "Single Variable", click [...] and chose "Rock X". Under the "Operations" section, check "Set Equal". Now, under the "Operand" section, check "Sprite", scroll down the first drop-down menu and select "Rock". Go to the second drop-down menu juxtaposed to the first and select "X co-ordinate".

Now do the same thing as before, except instead of selecting "Rock X" in "Variable to Change", select "Rock Y". Down in "Operands", select "Y co-ordinate" instead of "X co-ordinate. Everything else should remain the same.

Do this again twice setting the X and Y variables of the "Button" event.

Now the coding should look like this:

<>Variable Oper: [0001: Hero X] Set, Hero X Coord.
<>Variable Oper: [0002: Hero Y] Set, Hero Y Coord.
<>Variable Oper: [0003: Rock X] Set, Rock X Coord.
<>Variable Oper: [0004: Rock Y] Set, Rock Y Coord.
<>Variable Oper: [0005: Button X] Set, Button X Coord.
<>Variable Oper: [0006: Button Y] Set, Button Y Coord.

Now, make a new event on the map. This will be the door or gate event. Give this event 2 pages. On the first page, just give it the door graphic and set it to "Same level as hero". Thats it. You could make it say something like "Locked" in the event commands if you want, but that's all you need for now. Go to the second page and under the "Preconditions" options, check "Switch" and select [0001: Rock on Button].

Now for the final event that will make it all work. Make a new event, no graphic, below hero, and all that good stuff. 2 pages, the second one check "Switch" and select [0001: Rock on Button]. On the first page, make a condition branch. Check variable and select [0003: Rock X]. Underneath that, check "Variable Referance", select [0005: Button X]. Down at the bottom, from the drop down menu, select "Equal to". Uncheck "Execute Custom Handler..."

What that does is makes a new event if the X position of the Rock is the same as the X position of the Button. But we dont want to do that, we want the make the exact positions the same, so inside that branch, make a condition branch. Check variable and select [0004: Rock Y]. Underneath that, check "Variable Referance", select [0006: Button Y]. Down at the bottom, from the drop down menu, select "Equal to". Uncheck "Execute Custom Handler..."

That will make a new event if the X and Y position of the rock is equal to the X and Y position of the button. But what should that event do? Simple. Inside that branch, make an event that turns on our [0001: Rock on Button] switch.

The script should look like this:

<>Brach if Var [0003: Rock X] is V[0005] Equal
-<>Brach if Var [0004: Rock Y] is V[0006] Equal
--<>Switch Operation: [0001: Rock on Button] ON
--<>
End
-<>
End
<>

Now go test it out.

-----------------------
:3b) Expanding:
-----------------------
That will get the job done, but what if we wanted the door to close back up if the hero pushed the rock off of the button. Or say we also wanted the door to open if the hero stands on the button, but immediatly shut back up the second he steps off. Well, here's how we might do that.

---------------------------
I. Moving the Rock
---------------------------
I'm not gonna be nice about explaining what to do anymore, You should know what I mean by now. Go into the last event we made in section 3a, second page. The "Rock on Button" switch should already be preconditioning this event. If it's not, just set it. Now make a new conditional branch: "If variable Rock X is not equal to variable Button X." Inside that branch, turn off the "Rock on Button" variable. No custom handler.

Now, dont put this next event inside that branch, or you'll have to move the rock twice (or more) to make the door shut. Once vertical and once horizontal. So after that event's end, make another conditional branch: "If variable Rock Y is not equal to variable Button Y." Inside that branch, turn off the "Rock on Button" variable. No custom handler.

Script should look like:

<>Brach if Var [0003: Rock X] is V[0005] Not Equal
-<>Switch Operation: [0001: Rock on Button] OFF
-<>
End

<>Brach if Var [0004: Rock Y] is V[0006] Not Equal
-<>Switch Operation: [0001: Rock on Button] OFF
-<>
End

----------------------------------
II. Hero Triggers Button
----------------------------------
In the last event in section 3a, we made it to where the door will open if the rock is on the switch. Using that info, we can make it so that the hero can open the door by walking on the switch.

<>Brach if Var [0001: Hero X] is V[0005] Equal
-<>Brach if Var [0002: Hero Y] is V[0006] Equal
--<>Switch Operation: [0001: Rock on Button] ON
--<>
End
-<>
End
<>

Now, if that's all he has to do is simply step on it, then replace that with the event in section 3a with this one. If not, than just plug this one in underneath that event, and continue reading..

-----------------------------
III. Moving The Hero
-----------------------------
Just like moving the rock. On the second page of the last event in 3a, underneath the script, we can plug in a similar one for the hero.

<>Brach if Var [0001: Hero X] is V[0005] Not Equal
-<>Switch Operation: [0001: Rock on Button] OFF
-<>
End

<>Brach if Var [0002: Hero Y] is V[0006] Not Equal
-<>Switch Operation: [0001: Rock on Button] OFF
-<>
End

Now if the hero moves from his location, the door will close back up, telling him that he has to find another way to open that door.

-----------------------
:3c) Projectiles:
-----------------------
Now the fun stuff. Let's say you wanted an arrow to shoot across a room when a hero steps on a release button, and if it hits the hero, he takes damage. If you've been paying attention, you should immediatly know that you'll need at least 6 variables: the X and Y co-ordinates for the hero, the button, and the arrow. The event should look something like this:

<>Variable Oper: [0001: Hero X] Set, Hero X Coord.
<>Variable Oper: [0002: Hero Y] Set, Hero Y Coord.
<>Variable Oper: [0003: Arrow X] Set, Arrow X Coord.
<>Variable Oper: [0004: Arrow Y] Set, Arrow Y Coord.
<>Variable Oper: [0005: Button X] Set, Button X Coord.
<>Variable Oper: [0006: Button Y] Set, Button Y Coord.

Now we need to make an event to start the arrow's journey when the button is stepped on. First, make an arrow event, name it "Arrow", and set it anywhere on the page you'd like to. I suggest it be somewhere facing the direction of the trigger button. On page one of the event, simply set the graphic to a blank set. Please note that it's not nesscery to set it to blank, but it works better if you do. Now make a new page, and check "Switch" under "Preconditions" and make a new switch for the event, such as [0001: Arrow on]. Also, you can set the event's speed up to "5: Twice Normal", so the arrow isn't crawling around like a turtle when it actually moves.

Now make another event. Make sure it's Paralelle Processed, blank graphic. First, make a fork condition. Check variable [0001: Hero X]. "Variable Referance", select [0005: Button X], "Equal to", no custom handler. Then do the same for the Hero and Button's Y. Inside both Conditional Branches, turn on the [0001: Arrow on] switch on.

Now go back to the arrow event, on the second page. First of all, make sure that the event layer is set to "Above Hero". For this example, we'll say that the arrow shoots downwards towards the hero. So, from the Event Commands box, put in a Move Event. Select "Arrow" and make it move down 5 times, then hit "Switch OFF" and select [0001: Arrow on]. After that, put in a 0.4 second wait. Then, turn [0001: Arrow on] off, then put in "Change Event Location", select "Arrow", then check "Specific Location", hit [...] and mark the location where you originally put the arrow event. This will set the arrow back to it's starting place once it has traveled its distance.

Please note that if you have the arrow traveling longer distances, or faster, or slower, the wait time will be different. Just test it out to see what works.

With that done, all that's left is to make an event for the colision of the arrow with the hero. So, make a new event, Paralelle Processed. Check "Switch" under "Preconditions" and select [0001: Arrow on]. By now, you should know that we have to Conditional Branch the Hero's and Arrow's co-ordinates. Then you have to turn off the [0001: Arrow on] switch, and re-set it's location. You can omit that part if you want the arrow to pass through the hero. If not, to begin with, it needs to look something like this:

<>Brach if Var [0001: Hero X] is V[0003] Equal
-<>Brach if Var [0002: Hero Y] is V[0004] Equal
--<>Switch Operation: [0001: Arrow on] OFF
--<>Change Event Location: Arrow, (009, 003)
End
-<>
End
<>

What happens next is completly up to you, but as an example, let's put in a basic hitting effect. Go in both branches, and first, go to "Flash Sprite", "Target" Hero, 3 tenths of a second, set the color to whatever you please, and uncheck "Halt other processes while flashing". Now put in a Move Event,  Hero, Lock Facing, Move down. Lastly, for you CBS users, You can throw in a Variable Operation to subtract health from the hero.

Alternatively, as in, for people not using CBS's, you can have the hero move backwards, or teleport him back to a starting point if it's a maze-type thing. Just experiment. Open up your project and test it out.

-===========-
::4) Conclusion::
-===========-
Well, I hope you've enjoyed this little tutorial, and that it's given you a better understanding of RPG Maker. The X and Y co-ordinate variables are some of the most important concepts of RPG Maker, and understanding it opens up a wide variety of things to try. Don't feel limited to just what you've read here. Mix the X and Y co-ordinates with other events and event commands. Don't be afraid to try new things. And most importantly, never think that something is too difficult to script. That's something I've learned. I used to look at CBS's of games and think that it was forever out of my ability, but now I see that a lot if it is simple. And if you're like that, all you need to do is open up RPG Maker and experiment. It can be a lot of fun, and who knows what you'll come up with in the process. All I can say is that I hope I've helped in some small way. If you need any further help, please contact me. Thanks for your time, and keep coding.

-RE
Logged
AKA Desimodontidae. If you're seeing this profile, Im probably at school.

If i were a clown, would you hold me when I'm down?/I wish I had someone to make me drown/So many people don't know that it's so damn hard to be a clown/I am the clown with the giant frown/My heart is in a state of being upside down...

Offline Ian-TheResourceMaker

  • Associate
  • *
  • Posts: 181
(No subject)
« Reply #1 on: December 05, 2005, 05:36:14 PM »
 :bend:
Logged
Photo Sharing and Video Hosting at Photobucket

Offline BlackIceAdept

  • Teleporting is fun ^_^
  • Zealot
  • *
  • Posts: 586
  • What you wanted a book?
(No subject)
« Reply #2 on: December 08, 2005, 10:58:49 AM »
Quote
Originally posted by Ian-TheResourceMaker
:bend:
Logged
Image hosting by Photobucket

Offline coasterkrazy

  • June 2005 - September 2008... January 2011?
  • Exemplar
  • *
  • Posts: 1,915
  • Hello...
(No subject)
« Reply #3 on: December 09, 2005, 05:19:14 PM »
Very nice tut! It's especially good for me since this is an area of coding in RM that I'm not too familiar with.
Logged

Offline DarkFlood2

  • Back from the dead.
  • Zealot
  • *
  • Posts: 778
(No subject)
« Reply #4 on: December 11, 2005, 04:09:07 PM »
18 starz!!!!1!!one!!1!
Good job! Now I know how to make my ABS bettah!

Edit: I think this should be stickied.
Logged
Zeex - Level 70 Undead Warlock, Burning Legion Server.

Offline MrMister

  • damn u vile woman
  • Royal
  • *
  • Posts: 3,506
(No subject)
« Reply #5 on: December 11, 2005, 04:53:54 PM »
Nothing I didn't know before, but helpful to a lot of newbies.
You should add a section about moving shooting with a movable crosshair.
Logged
you look like an orphan

Offline AsakuraHao2004


  • RAM-TORTOISE!!
  • Exemplar
  • *
  • Posts: 1,623
  • AIM: dakutenshisdemon YIM: desimodontidae
(No subject)
« Reply #6 on: December 12, 2005, 08:25:46 PM »
Quote
Originally posted by MrMister
Nothing I didn't know before, but helpful to a lot of newbies.
You should add a section about moving shooting with a movable crosshair.


Yes, one of the goals was to help out the newbs.

I didn't want to add any more sections, I think that what I have covers the main points enough.
Logged
AKA Desimodontidae. If you're seeing this profile, Im probably at school.

If i were a clown, would you hold me when I'm down?/I wish I had someone to make me drown/So many people don't know that it's so damn hard to be a clown/I am the clown with the giant frown/My heart is in a state of being upside down...

  • Print
Pages: [1]
« previous next »
  • Charas-Project »
  • Game Creation »
  • Requests »
  • RPG Maker Programming »
  • The X and Y Co-ordinate Variables
 

  • SMF 2.0.10 | SMF © 2015, Simple Machines
  • XHTML
  • 2O11
  • RSS
  • WAP2
  • Simple Machines Forum