god more now! Re-Deimensioning Arrays...

Blitz3D Forums/Blitz3D Beginners Area/god more now! Re-Deimensioning Arrays...

ashmantle(Posted 2003) [#1]
Basicly he overwrites the previous array and uses it
for something completely different.. not sure if all the
variables inside the array gets deleted..

I use arrays all the time, and I have never redimmed an array..


Waz(Posted 2003) [#2]
weird and confusing.

THanks anywayz


Beaker(Posted 2003) [#3]
1. Do you need to swear? Children come here you know.

2. Re-using arrays like this is great for storing anything that changes size. For example: you might have a grid map in your game that is a different size in every level.

Level 1:
x = 10
y = 10
Dim map(x,y)

Level 2:
x = 20
y = 8
Dim map(x,y)

It is best to assume that the data gets deleted when you re-dim.


Tricky(Posted 2003) [#4]
But it's a way of programming I always recommend NOT to use, since it can easily turn a program into chaos if you're not carefull...


soja(Posted 2003) [#5]
It's also good to use if you don't know the size of array you'll need until runtime. (i.e. dynamic versus static)

Example:
A program that reads in some text file as data input. The text file could have any number of lines -- you don't know until you read it in.

You might declare a global array
dim mydata(0)
then use it later in a function, after you've determined how long the file is, and how much data you'll store.
dim mydata(thismuch)

This allows you to have your global variables up front, but still access them in functions.


Tricky(Posted 2003) [#6]
Personally I advise to dim them all to the max you intend to use and not to change it afterwards... On that method you are sure nothing can go wrong....


soja(Posted 2003) [#7]

Personally I advise to dim them all to the max you intend to use and not to change it afterwards...



This is generally a wise idea, as long as these apply:
1) You know the maximum number of elements possible BEFORE running the program
2) You keep track of how many elements are actually used (i.e. don't loop through all the elements of a 1000-element array when you're not using the last 100 of them)
3) You're not using a "large" array within a type, or loop, where many instances of it might be created, and much memory might be lost. (i.e. if you created 1000 arrays of 1000 elements each, but only used, on average, 500 elements, you're wasting memory)


Tricky(Posted 2003) [#8]
True.... I always keep track of that before I do anything... In fact, I sometimes keep things low, and start increasing the number of my original Dim, as I discover I need more... In the end that is the way to prevent a lot of bugs... Plus I'm used to dim everything always once, as an ex-pascal programmer... Pascal doesn't support redimming arrays, and since that really taught me to dim your arrays with utmost care, I continued to do that in Blitz, and I think that saved me a lot of trouble...


Ethan3850(Posted 2003) [#9]
Redimming arrays - didn't know u cud do that ... cudn't on ye olde Amiga Blitz... guess I shud of read the PC version's manual!

Do wat Tricrokra says, if u need 2 be constantly redimming arrays u shud really b using Lists - TYPE collections.

--
Win XPHE, AMD Athlon 1800XP, GeForce 4Ti 4800SE, DX9.0a
Processor running a little bit too hot!!!


Tricky(Posted 2003) [#10]
To be honest I never tried it in Blitz before, since I never redimmed anything in my whole life as a coder, because of the policy I live by stated above...

I know Visual Basic supports the "ReDim" command for that...

In Blitz you could just use Dim again, but I don't recommend it... I've tested the theory if an array looses it's data when redimming... It does...


Anthony Flack(Posted 2003) [#11]
It's actually quite good for blanking arrays. I know you shouldn't necessarily assume unassigned data to =0, but in this case, in blitz, it does.

I hope Waz is still around; haven't given up have you Waz?


Tricky(Posted 2003) [#12]
I wonder where Waz is myself...

I really hope things have not have gotten above his head...

I do think he has potentional as a coder, but I'm affraid he makes things more difficult than need to be...


Gabriel(Posted 2003) [#13]
I do think he has potentional as a coder, but I'm affraid he makes things more difficult than need to be...


I very much agree with that. From what I've seen he feels a need to understand things before knowing whether he needs to. I've always felt that you learn things better by doing at the same time as reading. That way you learn solutions to problems as they arise. Some things ( redimming arrays being a perfect example ) you may never need to know.

I've always considered it a bad practice ( for me ) and never bothered seeing what it did.

I learned blitz by setting myself tasks and completing them. They weren't anything fancy. I think my first program was a scroller with a few sin bobs. ( I'd done programming in other languages so I wasn't a total beginner. ) Waz will likely want to set himself simpler tasks to start with but I really thought he was on to something with his first game. Going on and making ever more complex games and learning how to achieve those tasks really seems like the way to go.


Tricky(Posted 2003) [#14]
I learned blitz by setting myself tasks and completing them.

I did write a tutorial on Turbo Pascal in the past, and my teaching method was exactly based on THAT principal... First explain what a feature does, giving a few example codes to make the point clearer, and then I set up a task you should easily be able to complete once you've understood what you've learned so far...

In my opinion THAT is the best way to learn programming in general...


Anthony Flack(Posted 2003) [#15]
There's still LOADS of commands I haven't even touched in blitz. I'll get to them when I need to. You still here, waz? I really hope waz is still sticking with it. It's kind of nice watching someone take their first steps, and hopefully being able to help out here and there.


Tricky(Posted 2003) [#16]
Yeah, ever seen an apprentice with so many masters? :D

He is quite motivated, so I really hope he'll resume his learning process...

If he can sort out to stick to important things and doesn't make things more difficult than need to be, I may become a good coder :)


SoggyP(Posted 2003) [#17]
Hi Folks,

@Tricrokra:
Yeah, ever seen an apprentice with so many masters? :D


And I happen to know for a fact that Anthony is two feet tall, green and has pointy ears...honest.

Later,

Jes


Tricky(Posted 2003) [#18]
Really? That's interesting? How did he get to look that that :)


Anthony Flack(Posted 2003) [#19]
Judge me by my size, do you?


Tricky(Posted 2003) [#20]
I don't judge you, but I must say that the discription SoggyP gives, is quite.... Interesting :)


WolRon(Posted 2003) [#21]
Waz: The force is strong in this one.


Tricky(Posted 2003) [#22]
Waz: The force is strong in this one.

Maybe... If he'd come back he'd know how willing we are to help him :)