Array question

Blitz3D Forums/Blitz3D Beginners Area/Array question

PowerPC603(Posted 2004) [#1]
I'm trying to create a space sim.

I'm using arrays of types to store all data about the spacestations and other stuff.

When I have a sector that has only 4 stations, I will use an arrays with indexes 1-4.

I want the ability to let the player build his own stations later on.

But then I should need to increase the array's indexes, so it can hold the existing data and add one more.

How would I do that?

Should I create a second (temporary) array and copy all data to this new array, then redim the original array (with one more index) and copy all data back to it?
As redimming an array clears all data that's in it.

I could also preset a certain maximum to it, but then the player is limited to these settings.


Hujiklo(Posted 2004) [#2]
Why not dim a larger array in the first place, and then set a maximum in your game for how many stations the player can build... an array of 100 or so lying around not doing much won't break the bank!


Perturbatio(Posted 2004) [#3]
An alternative would be to use a bank to store the handles of the data, you can resize them without data loss.


PowerPC603(Posted 2004) [#4]

Why not dim a larger array in the first place, and then set a maximum in your game for how many stations the player can build... an array of 100 or so lying around not doing much won't break the bank!



I was considering that, but it would use too much memory upfront, if each index also points to an unused (but existing) type-instance, with lots of fields in it.

I want the game to be expandable as much as I can (like X2).
In this game, the player can build an unlimited number of stations.

But since I will be making arrays that only hold a specific type of station (power plant, food factory, ...), the indexes will be low in the first place (a maximum of 10 per sector) and I want to have around 100 sectors.

Then it is easy (and quick) to copy the array to another first, then redim it and copy the data back to it.

I didn't considered a bank before, as I've not yet experienced with them.
I will try to understand those too and see if they are easier to use.

But can you use a bank for storing pointers to type-instances and access it like arrays of types (Array(index)\Fieldname = xxx)?


Hujiklo(Posted 2004) [#5]
Don't use arrays then use types instead - that way you can add and delete as you please without the constraints of arrays.


PowerPC603(Posted 2004) [#6]
That's also true, but when I have to edit some data in one field of a certain type-instance, I would have to loop through them all, just to edit one field.

Using an array, I can "group" all stations in a sector together (using the first index as the sector-number, the second as the station-number).

That way, I only have to loop through the second index of the array (if you have 100 sectors and 10 stations per sector), then you would have to loop through 1000 type-instances, instead of 10.

I'm trying all kinds of different approaches, but can't find the perfect solution.
And as long as I cannot find the perfect one, I cannot start with the game.

They should be:
- grouped together (so looping through them doesn't take too long)
- easy to use
- expandable, without re-compiling the entire EXE (I use textfiles to store all data)
- memory-efficient


Hujiklo(Posted 2004) [#7]
How often are you going to cycle through these arrays...looping through 1000 types isn't going to be that slow. But anyhow why don't you keep count of your type list- have global variables to represent which is the start of a sector population count and the second can represent the population of that type of object in that sector number...so if you wanted to make 100 zones then make your first generic type only one hundred big - you would need 200 global variable though...so perhaps use an array to hold those values is a way better idea. Basically if you then add 1 new station into a sector (let's say sector 10) you find it's sector number in your array - the first number is your start position in your type - the second number will hold the population count of that sector which to start will be 0 ( this is also the end count)...you then make your type by copying one of your global meshes and add 1 to the second number - then all sector numbers after sector 10 must be moved up 1, so add 1 to their value in your sector numbers array - as well as well (as adding one to the population end number) anything between start and end values is the poulation count ...this way you can keep track of all your sectors and their populations.
You use the array population start and end counts as the start and end numbers for your timer inside your type - you can use before and after as well as insert commands as well.
I just thought this out quickly so there may be some flaws here - but best bet is to just make a start on your system as it becomes more and more obvious what you must do to achieve your exact goal.

I hope you get it anyhow!


tonyg(Posted 2004) [#8]
Would this help...
http://www.blitzcoder.com/cgi-bin/articles/show_article.pl?f=cyberseth11252002124838.html
or how long would it take to copy to new array and copy back?


Hujiklo(Posted 2004) [#9]
That's pretty much the idea there Tonyg...a nice slick tutorial to really show the power of the array/type relationship!! Cool!


tonyg(Posted 2004) [#10]
If anybody is unaware of the articles section at Blitzcoder then it's well worth having a browse.


PowerPC603(Posted 2004) [#11]
Thanks Tonyg,

I can really use that, because I am using arrays of types.

I have found a solution for my game.
Since the player can only be in one sector at a time,
it has no use to load all data about every station in the universe, especially for sectors were you're not present (the other 99).

I will first be trying to create a game in the style of Freelancer (having lots of sectors and only one ship at a time and no other player-owned stuff).

This way I can create arrays with only 1 dimension.
When you enter a jumpgate (to go to another sector), all current data (and type-instances) are destroyed, the arrays will be redimmed, new type-instances are created (as much as needed) and data is loaded into them.

No need to copy/restore arrays and have too much data loaded at a given time.

Guess the station-building thingie will be for the next version of the game, when this one is working perfectly.
I will first try to get this game working, before I attempt to let the user build anything else, as this would be my first game.

If I would use 2-d arrays, and when one sector contains 15 stations, the first index of the array would represent the sectornumber (100), the second the stationnumber (15).

If another sector only had 1 station, this array would have 14 indexes (and also 14 type-instances) to many.

I was concearned (or how do you write this, as I'm Dutch) that my game would use too much unused, but allocated memory.

Looping through all these types (1000) is not slow, I know, but if you would have to loop through them, say 10 times per loop, at 50 fps (= 50 loops per second), then Blitz hasn't any time left to render the scene.
Then the framerate would go down to a crawl.

But thanks for the help, guys (and girls).


WolRon(Posted 2004) [#12]
I doubt that iterating through 10,000 types per frame will slow any modern computer down.