Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / multidimensional array data

Author
Message
Bago
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 6th Nov 2013 13:57
I am wondering how to fill/read/search all data in a multidimensional array in one go.

Using a simple tile based map of 10x10 size with also using a type to hold more than one piece of data per tile to explain what I am meaning.

So array creation and type:


Since I wish to know of a better way to fill the arrays data this is how I currently fill it:


Now the other problem is searching through every row/column to find where the player is and to test blocks directly near player to test if movement is possible in a specific direction.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 6th Nov 2013 19:43
You are using TYPE variables to access the values. A multidimensional array is typically something like
DIM Maplayout(10,10), or even DIM Maplayout(10,10,5). Not to get technical, but there is a difference.

Anyway, why not use nested loops to store the info? Something like this:



I added an example of how you can use "Dirt" as your default Blocktype, and "Grass" if the Y position is 2. Unless you are going to need more precision than an integer provides, I don't see why you would need to make your X and Y type variables as floats. Based on your code, you could use integers instead.

You need individual object numbers to assign, otherwise it will error. Some code was included to give you an example of how it could be done.

Also, why not consider tracking the player's position separately, instead of tracking their place in the map data?

So many games to code.....so little time.
Bago
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 7th Nov 2013 01:43 Edited at: 7th Nov 2013 02:17
Thanks for the filling array data in one go that makes it much easier.

For me using floats in x/y it was just an example of data that has a gridded positioning. Like you said I would use integer's if no decimal places are needed.

As for player position in the array it was a variable to be searched as it could be anything from inventory multi-array and need to find the first empty slot to place item into inventory or checking if player has a specific item in inventory to perform an interaction like a key for a door. But seeing your code I think I understand how to search the entire array for a single specific entry data.



As for seeing if I can move in a specific direction (from players location) can be used for other things like an inventory where an item takes up more than 1 slot in the array like a 2x2 size item, need to find an area in the array that has that shape empty to place the item into inventory. This one I'm still not sure on.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 7th Nov 2013 04:11
I'm sure there are several different ways you could handle the player's location. If it were me, I would use type variables for the player, much like you did with the map.



You could then easily determine a random starting point for the player:



Quote: "As for seeing if I can move in a specific direction (from players location) can be used for other things like an inventory where an item takes up more than 1 slot in the array like a 2x2 size item, need to find an area in the array that has that shape empty to place the item into inventory. This one I'm still not sure on. "


Need a little clarity here. Is the player moving from square to square or are they roaming around within a square? If it is square to square, all you need to do is check the square next to the player based upon their input. So, if they press the right arrow key, you check to see if the square that is at Maplayout(player.x + 1,player.y).BlockType to see if it = "Dirt" (or whatever else you want it to be).

If the player is able to roam around, you could determine if they collided with items, powerups, NPC, etc. by using collision checking (I suggest using Sparky's if you are not already familiar with it - link is below). Placing items might be a little more involved and perhaps you want to put doors on rooms to allow access.

I am uncertain of what you want to do, so post again and I will try and help.

Sparky's DLL (get the newest DBP one):
http://forum.thegamecreators.com/?m=forum_view&t=31051&b=5

LB

So many games to code.....so little time.
Bago
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 7th Nov 2013 07:21
I am not coding any specific game, just learning to use multi-arrays and the data in them that won't cause out of array bounds crashes.

Lets just say the character is the size of a tile and teleports from tile to tile. You are right that with finding the players location I can test what's around it (I just didn't see it till you pointed it out) except for when the player is at the edge of the map and it tries to test numbers beyond the array.

I'm assuming a lot of IF commands to make sure its not going to try and test below 1 or above 10 for this example. In 4 codes for each direction up, down, left, right. To test that the player location is not already on edge in that direction and prevent movement, another variable could be added into the type of allowedtomovehere and test that also.



Thanks for your help.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 7th Nov 2013 19:26
I like to use bitwise operations to help with setting a player's direction. I knocked together some code to show how you can do this:



This creates cubes that represent the map values. The player is a red sphere, the "Dirt" cubes are brown and the "Grass" ones are green. Note that the Y works from the bottom up and not top down.

There are other ways of doing it, but this is what I typically will use. If bitwise operation isn't for you, let me know and I can show you another way.

So many games to code.....so little time.
Bago
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 8th Nov 2013 01:49 Edited at: 8th Nov 2013 01:50
I also noticed that I need to change Y to Z for object positioning as my map would be vertically placed out.

Now I've learned something new bitwise operations, that can be handy to know, Thanks. I would usually use 4 separate Bloolean's for that type of thing.

As for the gosub's I usually just call functions as the usage seems the same to me.
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 8th Nov 2013 02:54 Edited at: 8th Nov 2013 02:55
Quote: "I'm assuming a lot of IF commands to make sure its not going to try and test below 1 or above 10 for this example. In 4 codes for each direction up, down, left, right. To test that the player location is not already on edge in that direction and prevent movement, another variable could be added into the type of allowedtomovehere and test that also."


That sounds a bit more complicated than it needs to be, I've done something very similar in the past with an object recycling/LOD system. Checking adjacent tiles without going out of the arrays bounds can be as simple as:




edit: fixed a typo
Bago
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 8th Nov 2013 02:55
Thanks that does make it less complicated.

Login to post a reply

Server time is: 2024-11-23 15:35:22
Your offset time is: 2024-11-23 15:35:22