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.

DarkBASIC Professional Discussion / Arrays not reporting correct values

Author
Message
Einlander
19
Years of Service
User Offline
Joined: 28th Sep 2005
Location:
Posted: 5th Dec 2013 05:40
I am trying to make a 2d map generator. I store the map in a square 2d array. When I try to use ARRAY COUNT() I get completely wrong values.


Result: 3


Result 120

Reading the documentation the arrays should be counted from 1 to <array limit>, but the compiler seems to be counting from 0 and padding the results.

Is there a way to know the correct value, or is there a way to figure out by how much darkbasic pads an array? I cant move forward with my game until I can get reliable values within the limits I set.

I am running db pro 7.62

BOW TO YOUR OPRESSOR
luskos
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 5th Dec 2013 21:50
You can try to use UDT and array of that type to store all the data you need to put for every square of your map.Every position of the array could represent square on the map, you can use simple formula to convert the integer number of the array to map x and map y positions and vise versa.Look at this thread, Van B explain it really simple :http://forum.thegamecreators.com/?m=forum_view&t=170590&b=1

Coding is My Kung Fu!
And My Kung Fu is better than Yours!
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Dec 2013 23:12
The second snippet looks as if it should be



which should give the answer 120 - which would becorrect as is the other answer of 3.

From the Help File:

Quote: "This is usually the value used to create the array when the DIM command was used, however by modifying the dynamic array this value can change throughout the execution of the program. When an array is completely empty, it will return a count of -1. The count refers to the number of available indexes, and so will return a count of zero when the command DIM arr(0) is used, representing the fact a single subscript is available at index zero."


which is a very convoluted way of saying that it is the index of the final element in the array. Arrays are stored as a single dimension list indexed from 0 to array count(). For example



gives an array of 12 elements indexed from 0 to 11 - so array count(a()) will return 11 which is the highest valued index.

Your first example has 4 elements indexed from 0 to 3. The double subscript that you used is just a convenient way for you to refer to those elements, i.e. the index pairs (0,0), (1,0), (0,1) and (1,1) refer to the elements (0), (1), (2) and (3).

Your second example, assuming my correction above, will have 121 elements indexed from 0 to 120 (i.e. 121 = (10+1)x(10+1) ) which can be indexed by the pairs (0,0), (1,0), and so on up to (10,10).

Try this snippet:



You can index the elements in a variety of ways as long as they are consistent with the number of elements in the array.



Powered by Free Banners

Login to post a reply

Server time is: 2025-05-17 06:48:15
Your offset time is: 2025-05-17 06:48:15