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 Discussion / Help with multiple variables please!

Author
Message
SlayrUK
20
Years of Service
User Offline
Joined: 10th Apr 2005
Location:
Posted: 11th Apr 2005 00:10
Hey,

Im writing to write to save multiple lines of code - its to save positions of many objects within the game for my level editor.

I am doing this by writing a new variable for each object and then writing it to the file.



It looks something like that above - but I am not sure how to make it so that every variable will have the number X after it.
So it leaves me with:

Variable1 = Object 1 Position
Variable2 = Object 2 Position
Variable3 = Object 3 Position

and so on.

Any help would be appriciated. Can I just put X after the variable name?
Thanks guys.

Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 11th Apr 2005 05:57
first, you can only count in whole steps, so you don`t need a float for the count limit, use

for x=0 to ObjectTotal

instead, DB will not increment a for/next in fractional quantitys anyway, so it`s just a waste of time and memory

second, storeing numbers inside strings is slow and not needed, you can just open your save file (making sure it doesn`t already exist), and then write your variables directly to it: eg

open to write 1,"saved_positions.dat"
write float 1,variable#

third, what you need is a way of numbering your variables, for this you can use arrays and either just add numbers to the stack as you make em, or queue them or just make a large enough array and then save that with the save array command, an array is like a stack of boxes that can all hold a different number, they are all identical exept that the name varies by a number at the end, so to make an array that holds eleven numbers you do

dim number(10)

the 10 isn`t a typo, computers make arrays from element zero, so you now have eleven boxes named

number(0)
number(1)
number(2)
number(3)
number(4)
number(5)
number(6)
number(7)
number(8)
number(9)
number(10)

each box can hold one number, so for example

number(0)=55
number(1)=123
number(2)=1
number(3)=99999999
number(4)=365
number(5)=3
number(6)=12
number(7)=11
number(8)=1234567890
number(9)=666
number(10)=999

if you want the number 365 back then do

print number(4)

and the contents of box 4 (365) are printed, the number after the variable name in the brackets is called the index, and this is where arrays can get powerful, it is possible to use variables as the index, so to print out all eleven numbers you can do

for Index=0 to 10
print number(Index)
next Index

and the variable points to the numbers adress, you could think of it as row of houses on Number Street, house number 0 has one number living there, same for house number 1, 2, 3 etc, you can pick what number you call up either by just putting in a constant, or by using a variable.

fourth, the way you are trying to do it is possible, but once you have added a number to a string then you will still have the problem of trying to retrieve the number afterwards, easy enough if all the numbers have the same number of digits, a lot harder if they are fractional or range widely, it is better to stick to numbers, besides, converting numbers to strings and back again is slow and will markedly slow the code when you have a large amount of them to convert.

Mentor.

PC1:XP, P4 3ghz, 1gig mem, 3x160gig hd`s, Radeon 9800pro, 6 way sound.
PC2: Linux, AMD 2ghz, 512mb ram, Nvidia GeForce4mx, 16 bit SB.
PC3: XP, laptop, intel 2.6ghz celeron, ATI 9000igp, 256mb
SlayrUK
20
Years of Service
User Offline
Joined: 10th Apr 2005
Location:
Posted: 11th Apr 2005 08:29
Thanks a lot, great reply.

The thing is though, the numbers im storing in the strings aren't for retrieving. I want it to store the whole string with the number in it to; so it will store this in the text file

"Object position 10" - (by using the X variable to get the 10 from object X)

Therefore, im storing text and not just numbers.

In your fourth point, you said numbering variables was possible but wouldn't work for retrieving the numbers - AS I do not need to retrieve them, can you tell me how to number the variables with regards to the X?

Thanks for your help! Very much appriciated

HWT
20
Years of Service
User Offline
Joined: 1st Apr 2005
Location: Earth
Posted: 11th Apr 2005 08:44 Edited at: 11th Apr 2005 08:47
Hi

I have made a mini world builder program that utilizes arrays to store positions instead of variables. I hope that this code is of some use to you. Use the mouse (with LMB or RMB) to move the objects, Enterkey to change to the next object, "s" key to save the file and "l" key to load the file.

If you have any questions, please ask

Cheers!

HelloWorld Tommorrow
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 12th Apr 2005 02:54 Edited at: 12th Apr 2005 02:59
ok! easy enough!

x#=123.456
number=109876
something$="text>"+str$(x#)+"<more text>"+str$(number)+"<:bye!"
print something$
wait key

or for a group of numbers

dim something$(10)
for i=1 to 10
something$(i)="words:"+str$(rnd(20))+":and numbers"
next i
for i=1 to 10
print something$(i)
next i
wait key

hope thats what you meant, cheers.

Mentor.

PC1:XP, P4 3ghz, 1gig mem, 3x160gig hd`s, Radeon 9800pro, 6 way sound.
PC2: Linux, AMD 2ghz, 512mb ram, Nvidia GeForce4mx, 16 bit SB.
PC3: XP, laptop, intel 2.6ghz celeron, ATI 9000igp, 256mb

Login to post a reply

Server time is: 2025-05-23 08:27:26
Your offset time is: 2025-05-23 08:27:26