POINTERS IN RPG MAKER 2K(3)

How to use pointers in Rpg Maker 2000 and 2003

  • Kazesui
  • 07/17/2011 09:09 PM
  • 16620 views
Apparently, there doesn't seem to be a tutorial on pointers / indexing in rm2k(3) here, so I thought I'd make one for future references whenever I make tutorials including them. This tutorial will also include how to create a simple puzzle using pointers

If you're thinking about making some custom system in rpg maker, you should consider learning about how to use pointers, since they can be incredibly handy to use. They will often allow you to do the same tasks writing much less code and can in that sense also prove to make more efficient code.

By "pointers" I'm referring to to these two options:


To explain them, I want to do so by showing an example of an operation using them.
We'll start with this one:



As you can see we here add the value within variable 0001 to where the variable 0002 is pointing. Given this chart over current variable values it might get easier to understand.



Since variable 0002 has the value 4 while being used as a pointer, we can say that it "points" to the variable of ID 0004.
This means that in this operation we will add the value given in variable 0001, which is 2, to variable 4 which results in it getting the value 3 after the operation is done. What's important to understand here is that it's variable 0002 which tells which variable should be added to.

Using the other option is pretty similar




Let's assume the same values within the variables as we started out with last time. What the operation does now, is that it adds the value within the variable being pointed to by variable 0001 to the variable 0002. Since Variable 0001 contains the number 2 variable 0002 will add it's own value to itself, which will result in it getting the value 8 in the end.

And this is basically what pointers are all about. But just having read this might not make you realize what you could do using pointers, so I will provide a practical example to help stimulate your ideas


Remember The Sequence Puzzle




We'll be making a simple puzzle utilizing pointers. The puzzle works in terms of you walk onto the middle switch and then you can watch the 4 other switches being "pushed" down in a random sequence which you have to remember. After having been shown the sequence, the player has to walk on the switches in the same order as the sequence.

The event for the middle switch looks like this:



The first switch is turned on to switch the current event page of the middle switch to one where it's pushed down. This is followed by turning the switches 1 to 4 off. These are the switches for whether the 4 other switches is pushed down or not. If the player choose the wrong switch it will create an error sound and all switches are turned on, leaving the player no choice but to hit the middle switch again to reset the process.



Here we have a list of the variables we're going to use. We want to store the sequence which the player has to remember in the Step 1 to 6. Step 1 has the ID 11, so we set the variable Step Ptr to 11 so that it can refer to the first step. Followed by this we use a label. A label is a point in the code which you can jump to be using a "jump to label" of the same number. This is practical for creating loops which continue until some criteria is met. Now we set the variable which Step Ptr is pointing to, to a random value between 1 and 4. Since Step Ptr has 11 now, it will be Step 1 which recieves a random value between 1 and 4.

Now we store the value of the variable which Step Ptr is pointing to in Step Tmp. This will return the value which was set in Step 1. We then use this value to turn on the switch with the same ID. We can do this because the switches used here have the ID 1 to 4. If the switches had been set to 10,11,12,13 then we'd have to add 9 to the Step Tmp variable after getting the value in Step 1. 9 + 1 would give 10, which would turn on the first switch, 10.

After the switch is turned on, we wait a little, to make the player see which one has been turned on, then we turn it off and add 1 to the Step Ptr variable itself, so that it now points to the variable of ID 12. We then check if the Step Ptr is 16 or lower, meaning that it still hasn't set all variables from Step 1 to 6, and then returns to the label to repeat the process. Since it's now pointing to variable 12, that is Step 2, it will do all the same things to that variable. In the end Step 1 to 6 will all have a random value between 1 and 4. Finally at the end we set the Step Ptr back to 11. This allows us to more easily continue onto the next four events:



This is one of the 4 switches which the player has to step on to replicate the sequence. The first thing done is to set the value pointed at by Step Ptr into Step Tmp. After just having hit the middle switch, this would be Step 1. Since this is Switch #1 we check if Step Tmp got the value 1. If that's the case, then the player has stepped on the correct switch, if not then the player stepped on the wrong switch and has to restart, which is simply done by turning the 4 switches on and the middle switch "steppable" again.

If the it was the correct switch, we would first want to see if the sequence is complete, since handling that would take priority. If Step Ptr is 16 or higher, meaning that you just stepped correctly for the last step, we'll turn on all switches, play a victory sound and put in whatever else is supposed to happen upon finishing the puzzle. If it was not the last step we simply add 1 to Step Ptr so that it now points to the next Step.

The only additional thing you have to do after this is to add 4 almost identical events, only changing which number you're checking which Step Tmp should be equal to, and the switches turned on and off. And that's all you need for this puzzle when using pointers to create it.

To make sure you've understood it correctly and to make sure that you'll actually be able to use them for something useful, I'll recommend that you try to recreate this puzzle on your own, using pointers. You could create a variant of this puzzle where you have 6 different switches, or where you have to remember 8 Steps instead of 6. Try it out!

Here's a sample project which you can use as reference: Sample Project

Posts

Pages: 1
Man I really love your tutorials :D
isitoktorequestatutorialaboutcoordinatesandtilerangesincethosearealsoveryrareandithinkwillhelpmanypeople
What kind of tile range are you thinking about? My first thought when thinking about range would be something going along the lines of this one:
Proximity Detection for Events

Meaning there's already a decent tutorial on it. An alternative tile range technique would be along the lines of one you would expect in a TBS, showing all the tiles any given event can move to, and that's certainly rare to find in a tutorial (and I just happen to already have a working sample of that).
author=Kazesui
Meaning there's already a decent tutorial on it. An alternative tile range technique would be along the lines of one you would expect in a TBS, showing all the tiles any given event can move to, and that's certainly rare to find in a tutorial (and I just happen to already have a working sample of that).


This is exactly what I was thinking about :)
I thought this was going to be like basic tips and pointers on using RPG Maker, but then noticed who the author was and decided to take a gander. This is a very good tutorial and a good way to introduce the mechanism you call "pointers" (which may be an actual definition, as I am no programmer).

Understanding how pointers work is crucial for anybody wanting to implement more complex systems like dynamic/scrolling custom menus and custom battle systems.
How do you make games with RPG Maker 2003?
I don't know why but I cannot grasp how to make a pointer in RPG Maker 2003, I understand what a pointer is and does outside of RPG Maker in the programming realm. I understand that a pointer points to a variable and uses the value in that variable, E.g. *ptr = &number; But I cannot grasp how to make a pointer in RPG maker...
chana
(Socrates would certainly not contadict me!)
1584
I really can't say I undersood the whole thing, I get it MORE OR LESS globally, I would certainly be absolutely unable to reproduce this sequence.
Could you tell me what "Tmp" and Tpr" stand for, it would probably help a bit (I would, If I could, make 4 interesting minigames, for my game, but I'm TOTALLY stuck there, at worse I can do quizz type of stuff... bah!)
author=supremewarrior
I don't know why but I cannot grasp how to make a pointer in RPG Maker 2003, I understand what a pointer is and does outside of RPG Maker in the programming realm. I understand that a pointer points to a variable and uses the value in that variable, E.g. *ptr = &number; But I cannot grasp how to make a pointer in RPG maker...


In rpgmaker pointers are just regular variables which gets used differently. Since you're familiar with programming, you could think of them as indexing the array of variables. say you have one variable which you simply call "ptr" with some arbitrary value. Basically you're just using it as:


Variables[ptr] = value;
or
value = Variables[ptr];
or
Variables[ptr_1] = Variables[ptr_2];


Where ptr is simply the value of the index in the array you want to access, and Variables is the array of all variables.

author=chana
I really can't say I undersood the whole thing, I get it MORE OR LESS globally, I would certainly be absolutely unable to reproduce this sequence.
Could you tell me what "Tmp" and Tpr" stand for, it would probably help a bit (I would, If I could, make 4 interesting minigames, for my game, but I'm TOTALLY stuck there, at worse I can do quizz type of stuff... bah!)


Tmp is just short for temporary, meaning that the value of the variable is used for calculations, and once the calculations is done it may contain some arbitrary value which is of no interest.
Ptr is short for Pointer, and this is the variable which contains the ID of a variable we want to access.

Step Ptr is used to point to the variables Step 1 to Step 6, and in the first code segment the Step tmp is used to get the value which was set randomly, so that it can be used to trigger the switch with equal ID. this only works if the ID of the switches is equal to the values you've chosen to represent the different switches.

in the second piece of code, Step tmp is again used to get the value stored in the variable with same ID as Step Ptr, so that we can compare that value with the one representing that very switch.

I hope this helped a little, and if not you could keep asking. You could also ask more specific questions related to one of the minigames you want to make which involves use of pointers, and I could possibly give you more specific help how to do it, i.e. how to apply the pointers, unless it's too complex or so.
chana
(Socrates would certainly not contadict me!)
1584
No, I think I could finally understand... Thanks, yes this has helped. For the mini games, I thought of reproducing a very, very short melody (and that would look like those switches, such note at a precise time in the global order of the notes, everything resetting as soon as a error is made.{souvenir of a similar minigame which I had much enjoyed!)So I guess the best thing would to try your sort of exercise and see where I get stuck ?
Well, I can try...
Good explanation, it's great that your doing these tutorial on pointers because for most people who are using the program don't have programming knowledge or have enough of it and tend to go the long hard way to making things work.
I'm having a hard time figuring out real applications for pointers, in relation to what I want to do in my game. Right now, I want a character to have a pool of moves where they pick four of them to use on the field and a menu that knows what moves you picked from the pool. Eventually I want to create a CBS that takes place on the maps, and I'm hoping pointers will make this easier.

I pretty much understand how this puzzle works. I followed Narcodis' tutorial for CMS(today), so that I can then follow your tutorial on creating the item submenu(tomorrow). All in the hopes of understanding the applications of pointers. In doing that, would you say that it's the best way to understand it?

I'm only just starting to try and wrap my head around all this. Perhaps you could write up other general applications for pointers. Since you so familiar with them I'm sure you'll be able to see possibilities that Ptr Newbs won't be able to see for a long time.
Going through how to make an item submenu is probably a good way of learning how to use pointers as it makes good use of pointers, yeah.

As for general applications for pointers, it will often end up to abstract or too specific, which makes it hard. Most of the relevant key points is presented in the puzzle implementation. Usually pointers are used within the context of something more complex, often custom systems of different kinds.

Pointer's are good for storing data into an unknown or adjustable amount of variables, and are also good for retrieving the same numbers without using tons of branches.

Let's take an ABS as an example. Let's say that the enemy hp is stored in variable 21 to 40 and that variable 21 refers to the hp of enemy 1, 22 to enemy 2 and so on.
Let's also say that you create the enemy events systematically, so that their ID's are consecutive and making the event ID's from 11 to 30.

Now, when you attack, you can each time check for all 20 different enemies if you've landed an attack with lot's of branches. However, this isn't very effective and it's a bit of work as well. Something one could do instead is to make use of the "get event ID" on the coordinates of the attack. If there's an enemy there, you'll get it's ID. We just have 2 branches now, which checks if the event ID was from 11 to 30. Now we need to make sure the add/subtract hp from the variable of the enemy you just hit. If we take the ID you just got, and add 10 to it, we'll have a number in the range 21 to 40, which would correspond to the variables with the enemy hp! By using this value as a pointer, we can work on the correct variable with hardly any code (as opposed to using 40 branches to check for each possible enemy)

And this is only a very simple implementation, which doesn't consider a lot of things like defense of enemy or so, but to add all that would turn it into an ABS tutorial instead. This is a reason why I don't have very many examples on pointers in this tutorial, since the principles are basically the same. I'm planning to create more tutorials which make use of pointers as well, including my take on ABS systems as well.

The truth is that I only made this tutorial so that I could reference it in the other tutorials I make since they often include pointers. So don't worry, more examples are coming. Hopefully some a bit less complicated than the TBS ones
In the context of menus, I use pointers to populate a spell menu in Necropolis's custom battle system. Since spells can be learned in a few different orders and taking up multiple pages, I needed a way to present them cleanly, without gaps. Unfortunately you still have to manually set the pictures, but pointers let me store spell IDs/costs/etc more easily overall. The spells are split into pages so that the interface matches another menu, but it can easily be switched to scrolling (think: item menu, for example) since doing menus that way is fairly flexible so long as the variables know where to look. That's just an example of how they can be used.

I find pointers very confusing to use and easily lose track of what I'm doing and getting my brain wires crossed (I have a poor short-term, working memory). The best way I've found to mitigate this is to duplicate all the variable events in Excel, so I can rapidly visualize and track what I'm doing before eventing it in RM2K3. That way I can punch a number into a field and see how everything will output if my cell pointers are set correctly. An alternative is to create a debug display in RM2K3 itself, but that takes more time.
I also note down to myself which pointer points to which variable and it's entire function, pointers in RPG Maker 2003 are confusing and you should have some planning before undertaking pointers if your a beginner.
Right on, I'll be patient and play around with this stuff in the mean time.

Thanks for the comments guys!
So I'm confused to this somewhat. Let me see if I understand this since I'm trying this very exact thing (but I'm using different variables obviously. For mine I'm using 949 for the Step Tmp, 950 for Step Ptr, and 950-955 for Steps 1-6.

Now, if you use the Variable Reference, what I'm understanding is that if say, Variable Reference points to 1, and Variable 1 has a value of 5, it'll instead point to the value of variable 5. And with Value Stored in Index, if the variable used for Value Stored in Index is 5, it'll instead point to the value of Variable 5?

I'm trying to come up with new puzzles and whatnot to use for my game as to spice it up, as there hasn't really been any puzzles as of yet in it. This whole concept has me confused though @_@
Yep, that's right. If you're referencing variable 0001, and that variable has the value 5, then you will point at the value of variable 0005 instead.

The best way to avoid confusion it to simply try out some simple stuff to make sure that you've understood it. Like, create an npc which does a few variable operations, and check the debug menu with F9 to check if the result is the same as you expect it to be (which is how I learned to use them).

You don't need pointers to make puzzles, but once you know how to use them, they can make complicated puzzles easier to code.
Pages: 1