# Thread: RPG Game Making a Play Space with Arrays - How?

1. ## RPG Game Making a Play Space with Arrays - How?

Hello everyone, I'm Mr. Lister, and I'm making an extremely small-scale RPG game where you make a little character and go around killing monsters and collect items. That's all it is so far, and this is just a private pet project of mine, so I don't plan to releasing it publicly, because this game isn't much to look at. It's pretty much just a little bit of subtraction/addition, if-then statements, and some extra fluff because I barely have high school knowledge of VB.NET, so beware if I sound confused when you start throwing technical terms at me.

My previous inquiry on VBForums involved how I could go about making Save Files. Now I need to ask you guys to help me solve a bigger and much more complicated problem (to me, at least).

I have just recently learned about 2D Arrays, where you can do things like
Code:
```Private awesomeArray(20,20) as String
awesomeArray(1,1) = "Hello"
awesomeArray(2,1) = "World"
....
awesomeArray(20,20) = "I love turtles"
End Sub```
..and use something like this to fill text boxes, right?

Well, in my little RPG game, I have a giant grid of 24 PictureBoxes by 24 PictureBoxes, each of those PictureBoxes being 24x24 pixels. Each of those PictureBoxes are named respectively of their position, like there is a PictureBox named x0y0, and another called x19y5, and another named x23y23. This grid is going to be like a 24x24 grid bird's eye view of a giant 999x999 tile map, and your character is always going to be in the center of the grid, on the PictureBox x11y11, so when you hit the Up key, your character stays in the center, but the entire map moves down to reveal more of the map. Also, before I forget to mention, things on the map are represented by BackColors, so your character is represented on x11y11 by x11y11.BackColor = Color.Maroon, or grass is x??y??.BackColor = LimeGreen, or a wolf is BackColor = Color.Gray, and a tree, which you can't walk through, is represented as x??y??.BackColor = Color.ForestGreen.

Now, can and how can I use a 2D Array to hold values like a wolf's level, health, and gold, and how can I randomize the elements of the Array on the Form's Load up action, so the map is different every time the game begins?

Please let me know if you have no clue what I'm talking about. I'll clarify and answer any questions you guys have!

2. ## Re: RPG Game Making a Play Space with Arrays - How?

Originally Posted by Mister Lister
Now, can and how can I use a 2D Array to hold values like a wolf's level, health, and gold, and how can I randomize the elements of the Array on the Form's Load up action, so the map is different every time the game begins?

Please let me know if you have no clue what I'm talking about. I'll clarify and answer any questions you guys have!
Why would you want to use a 2D array for holding properties ? What wolf ? Does the gold belong to the wolf ? Clarify your problem please.

3. ## Re: RPG Game Making a Play Space with Arrays - How?

Use a 3-D array. MyArray(x,y,z)
x,y is your standard grid as you have described. z can then hold properties for that grid position. It can hold what is present in that grid pos and any other stats you need (for example walls between this cell and the surrounding ones). I did the same thing many many years ago on a Sinclair Spectrum.
You can also create a 4-D array but it gets more and more difficult to visualise it.

4. ## Re: RPG Game Making a Play Space with Arrays - How?

Somehow I don't like that approach. Its too confusing and limited. I think its better to use a normal array of some kind of object to store information about elements on a map for a game. Among the information stored will be its x and y co-ordinates on the map.

5. ## Re: RPG Game Making a Play Space with Arrays - How?

You're thinking about this the wrong way. You don't need to store the 24x24 visible tiles, you need to store the 999x999 map. Each tile contains either nothing (is grass), an immovable tree, an enemy (I'm assuming you might have more than just wolves) or the player. It sounds like each tile can contain only contain a single thing.

As for your 24x24 visible tiles, you simply need to look at the current location of the player and the tiles around him and draw those, directly referencing the underlying map, not by copying things around a separate "view" array. For example, if your player is at location (345, 927), then x11y11 maps to that location, x0y0 maps to the tile at (334, 916) and x23y23 maps to tile at (357, 939). Although if it was me, I'd set up a way of referencing them such that the player's location was your view's (0,0).

You'll find a lot of conversions between "world coordinates" and "view coordinates" in your code. It will help you stay sane if you figure out a way of dealing wit that more elegantly. (Also, consider what you want to happen when the player stands at, for example, (999,999) world coords - is he still in the centre of the screen?)

6. ## Re: RPG Game Making a Play Space with Arrays - How?

Sorry, I got somewhat distracted in that last post. To continue from the first paragraph - if each tile can contain a 'thing', and that thing can be one of three different things (an immovable tree, an enenmy or the player), then that suggests either a Class called Thing that you then have three classes (Tree, Enemy and Player) Inherit from, or alternatively you have an Interface called IThing that the three classes Implement. The Tile can then contain a Thing/IThing, and not worry about exactly what sort of Thing it is. The Enemy class instance holds the level/health/gold of that enemy.

7. ## Re: RPG Game Making a Play Space with Arrays - How?

Originally Posted by espaņolito
Use a 3-D array. MyArray(x,y,z)
x,y is your standard grid as you have described. z can then hold properties for that grid position. It can hold what is present in that grid pos and any other stats you need (for example walls between this cell and the surrounding ones). I did the same thing many many years ago on a Sinclair Spectrum.
You can also create a 4-D array but it gets more and more difficult to visualise it.
Dude, that seems like it would make perfect sense. Like a giant 999x999x999 cube where the X and Y parts show what you see and what the object is, like a tree or grass, but in the back, in the Z dimension, all of the values for those grass and tree bits is revealed.

Originally Posted by Evil_Giraffe
You're thinking about this the wrong way. You don't need to store the 24x24 visible tiles, you need to store the 999x999 map. Each tile contains either nothing (is grass), an immovable tree, an enemy (I'm assuming you might have more than just wolves) or the player. It sounds like each tile can contain only contain a single thing.

As for your 24x24 visible tiles, you simply need to look at the current location of the player and the tiles around him and draw those, directly referencing the underlying map, not by copying things around a separate "view" array. For example, if your player is at location (345, 927), then x11y11 maps to that location, x0y0 maps to the tile at (334, 916) and x23y23 maps to tile at (357, 939). Although if it was me, I'd set up a way of referencing them such that the player's location was your view's (0,0).

You'll find a lot of conversions between "world coordinates" and "view coordinates" in your code. It will help you stay sane if you figure out a way of dealing wit that more elegantly. (Also, consider what you want to happen when the player stands at, for example, (999,999) world coords - is he still in the centre of the screen?)
Originally Posted by Evil_Giraffe
Sorry, I got somewhat distracted in that last post. To continue from the first paragraph - if each tile can contain a 'thing', and that thing can be one of three different things (an immovable tree, an enenmy or the player), then that suggests either a Class called Thing that you then have three classes (Tree, Enemy and Player) Inherit from, or alternatively you have an Interface called IThing that the three classes Implement. The Tile can then contain a Thing/IThing, and not worry about exactly what sort of Thing it is. The Enemy class instance holds the level/health/gold of that enemy.
Classes? Inheritance? Interfaces with Implementing? I'm really lost friend, could you maybe explain your paragraphs a little more clearer? It's not you, no! It's me, I've very little knowledge of VB... Well... More like scattered knowledge.. I know some things and other things, like If then Statements and Arrays.

8. ## Re: RPG Game Making a Play Space with Arrays - How?

Originally Posted by Mister Lister
Like a giant 999x999x999 cube
Would it really need to be 999 deep in the z direction? The dimensions do not all have to be the same you know, the array could be (999,999,2) or (999,105,21) or anything you need. Also 999x999x999=997,002,999 - that's a LOT of memory!

9. ## Re: RPG Game Making a Play Space with Arrays - How?

Originally Posted by espaņolito
that's a LOT of wasted memory!
Fix'd that for you. Most of the map would probably be generic(blank or default stuff on it that doesn't need to be recorded)

10. ## Re: RPG Game Making a Play Space with Arrays - How?

Originally Posted by Mister Lister
Classes? Inheritance? Interfaces with Implementing? I'm really lost friend, could you maybe explain your paragraphs a little more clearer? It's not you, no! It's me, I've very little knowledge of VB... Well... More like scattered knowledge.. I know some things and other things, like If then Statements and Arrays.
If you've not come across these yet, then it's a bit hard to explain in a single (or even a few) forum posts. Basically you would be defining your own types (things like Integers and Booleans etc, except yours are things like Tree and Enemy and Player). Then for each, for example, enemy that you place on your map, you create an object of type Enemy, and set that object's Gold, Health and Level properties.

However, although the Tree, Enemy and Player objects are all different (they have different properties and different behaviour), they all have something in common - they are things that can be placed on a tile in the map. Thus there is something common about them all and there is code (namely that which controls the map) that needs to look at them all as though they are the same thing. The mechanism by which this is done in VB.NET is through inheritance where you place the common code and properties in a base class from which all your classes inherit, or by using interfaces which define the properties and methods that are common, but each class that implements the interface supplies the implementation of those properties and methods.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•