forums

TNT Basic Forums > Programming Q&A
How to resize arrays?
< Last Thread     Next Thread >
Author
Thread        Post A Reply

10-01-2006 12:28

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

i.e, increase the size of the array by 1?, or reduce size of array by 1.

10-01-2006 14:52

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

this is not built in TNT, so you'll have to do it yourself.

basically, you create a second array in which you copy the data from the first. then, you re-create the first one with the size you want and you can then copy the contents from the backup one to yours.

Like this:


int old_size=5
int new_size=7
int n

'create array
int var[old_size]

for n=0 to old_size-1
var[n]=random(0,100)
next

' print contents
print "Before: "
for n=0 to old_size-1
print var[n]
next

' resize
int backup[5]
for n=0 to old_size-1
backup[n]=var[n]
next


int var[new_size]

for n=0 to old_size-1
var[n]=backup[n]
next

for n=old_size to new_size-1
var[n]=random(0,100)
next

' print new contents
print "------------------"
print "After: "
for n=0 to new_size-1
print var[n]
next



hope it helps =)

10-02-2006 03:28

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

...
would it just be quicker to not resize, and have all the arrays constantly with large number of values?


eg.

array[1000]

and not have to resize it.

Would this be faster for the game, or would resizing the variables be more optimal?

in the rts i am making, I am currently doing this:
So.. to have to resize each and every one of them every tume I want to make a unit.... you get the point.

'list of properties of each unit in game.
'shipfacing[1] is the facing of the first unit created, shipfacing[2] is 2nd unit... etc
'unit limit of 1000 in game. (actual number maybe less)
global int shipfacing[1000]
global int sprite[1000]
global int type[1000]
global int currentx[1000]
global int currenty[1000]
global int targetx[1000]
global int targety[1000]
global int totalshield[1000]
global int currentshield[1000]
global int totalhp[1000]
global int currenthp[1000]
global int totalarmour[1000]
global int currentarmour[1000]
global int weapon[1000]
global int topspeed[1000]
global int rotationspeed[1000]
global int prodspeed[1000]
global int currentprod[1000]
global int planettype[1000]
global int selected[1000]
global int rotate[1000]
global int xdist[1000]
global int ydist[1000]
global int image[1000]
global int team[1000]
global in exists[1000]

10-02-2006 07:03

Posted by:
Orteil

Location:
Somewhere in France

Find more posts by Orteil

I don't know whether making big arrays at the start would speed or slow down TNTbasic, but I have a tip I use often :
you have lots of arrays each sized with 1000. Whether than changing every 1000 if you want to increase/decrease the limit for units, you can just declare a var at the beginning (like "number_units" or "nu") and set it to 1000 (or 150, or 42) and then make the arrays look like this : tunasteak[nu],llama[nu],spongebob[nu] and so on.
Then you just have to change the nu value to have the whole set of arrays fit.
Hope it helps (you say this stuff in english, I guess ?).

10-02-2006 12:11

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

Doh!

Even with that many 1000 value arrays, and two dozen sprites, the game still managed 30 fps!

But as soon as I introduced the 10000 value arrays-particle-system to record every single particle on screen.... It got reduced to 0.66, with only 2-3 particles....

I could either limit the number of particles to 1000 (which'd make the game suck), or take up resizing arrays... If only at least for the bulets.

10-02-2006 13:08

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

You cannot define new global variables in procedures... how else can I do it?

10-02-2006 23:25

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Using huge arrays should not slow down much, only take more ram. Though, for heaven's sake, don't use 10 000 particles! You're never going to use that many at the same time. In fact, having around 100 particles is probably enough, just write a smart system that reuses the same variables at different times, i.e. each unit does not need to have its own particles, they can share the same ones because they're not all using particles at the same time.


>>
You cannot define new global variables in procedures... how else can I do it?
>>

This is a big problem in TNT. Defining globals in a procedure wouldN,t make much sense, but it would be awesome to be able to set the dimension of a previously declared array from within a procedure. TNT procedures are very limited.

For now the only work-around is to use 'goto'. And i really hate to say that because goto should ideally never be used.

10-03-2006 00:11

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

Lol... Since everyone says "GOTO" is very dangerous at making spaghetti code, I made my game in such a way that it can't be used at all. (or very hard to place in. i.e, a complete structure rewrite.)

Right now My code is:

Globals
G
G
G
G

Procedures
P
P
P
P

first procedure callled.

10-03-2006 00:12

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

I read something about "shared variables" but couldn't find it any more.

10-03-2006 00:43

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

yes!

http://www.tntbasic.com/learn/help/guides/sharedvariables.htm

this might indeed be useful for this issue

10-03-2006 00:59

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

after some fiddling... I've found out how to use globals AND resize them in a procedure!!
int i
int number of values=10
Global int IAMAGLOBAL[numberofvalues]

for i=0 to numberofvalues-1
IAMAGLOBAL=i
next i

resize()

Procedure resize()
int i
int IAMAGLOBAL[11]
for i=0 to 10
IAMAGLOBAL=i
print IAMAGLOBAL
next

CONFIRM()
end proc

procedure confirm()
int i
for i=0 to 10
print IAMAGLOBAL
next i
end proc



voila!!! you just increased global array size by 1!

10-03-2006 01:00

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

when I defined the "[ i ]" thing for the array, it turned into italics... but you get the point

10-03-2006 01:21

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

wow. that's very weird behavior from TNT, but if it works...

Procedure resize()
int i
int IAMAGLOBAL[11]

the IAMAGLOBAL in resize should logically on be a local variable to the procedure hiding the global one. Amazing that you can create a second variable with the same name and that it actually ends up being the same variable.

10-03-2006 01:24

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

Thats what I thought too...

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.