News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

On Arrays...

Started by Makeii Runoru, August 08, 2008, 06:39:19 PM

Previous topic - Next topic

Makeii Runoru

I have made a sample list and Dim'ed about (20) of them. Is there an easier way to assign values to the array without having to do this?:

Sample(1).type1 = ___
Sample(1).type2 = ___

Sample(2).type1 = ___
Sample(2).type2 = ___

Sample(3).type1 = ___
Sample(3).type2 = ___

etc.
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

Big C.

It's easier for all if you offer some sample code so we can see what are you trying to program...

But as an answer: Is a For Next Loop in conjunction with a data section a solution for your problem?

Makeii Runoru

I might need to see an example of what your concept calls for. The code I have is a project for an ORPG. The code I have a problem for is here:

Type type_equip_armor
name$
icon

core
classes
plus1
plus2
plus3
EndType

Dim Armor(30) as type_equip_armor


Now, because I just made 30 different objects of Armor, I need an easier way to edit the information easier than the way I explained above.
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

kevin


  If you're going to initialize characters/objects in your game then using DATA statements  is a pretty easy way of doing it.


   Type tPerson
    Name$
    Height
    Money$
   Endtype

   Dim People as tPerson list

Dim Fields$(100)

s$=readdata$()
While s$<>"EndOFDetails"

People= new tPerson
Count=SplitToArray(s$,",",fields$(),1)

People.name$ =Fields$(1) ; this field is a string
People.Height =val(Fields$(2))
People.Money$ =Fields$(3)
s$=readdata$()
endwhile


For Each People()
print People.name$
print People.Height
print People.Money$
next


Sync
waitkey



// Name, Height, Money$
Data "Bill, 186 , $400"
Data "John, 200 , $45433"
Data "Sally, 178, $999999"
Data "EndOFDetails"

Makeii Runoru

This looks pretty intense. I'll have to study how everything in this code works. Thanks!
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

kevin


  Here's an array version, with a few quick comments..




Dim Fields$(100)


  Type tPerson
    Name$
    Height
    Money$
   Endtype


   Dim People(100) as tPerson


// ---------------------------------------
//  Readf the people data from the DATA statements in our People() array
// ---------------------------------------

// REad the first line from the data statements
s$=readdata$()

// While the S$ isn't equal to "EndOfDetails",  we keep read from the data statements bellow.
While s$<>"EndOFDetails"
// bump the number of people we've found
NumberOfPeople=NumberOfPeople+1

// Tell Pb to break the string S$ down into indivual parts..
Count=SplitToArray(s$,",",fields$(),1)

// assign them parts as thi persons details
People(NumberOfPeople).name$ =Fields$(1) ; this field is a string
People(NumberOfPeople).Height =val(Fields$(2))
People(NumberOfPeople).Money$ =Fields$(3)

// read the nexct line of date
s$=readdata$()
endwhile


// ---------------------------------------
// Display the people from the array..
// ---------------------------------------
For lp=1 to NumberOfPeople
print People(lp).name$
print People(lp).Height
print People(lp).Money$
next


Sync
waitkey



// Name, Height, Money$
Data "Bill, 186 , $400"
Data "John, 200 , $45433"
Data "Sally, 178, $999999"
Data "EndOFDetails"





Makeii Runoru

I found that memory banks seem to organize what I need rather than raw data on the bottom of the code. I have a type called type_weapon and has over 10 listings.

Also, is it possible to contain this data in a txt file rather than inside the IDE?
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

Big C.

Yes of course...

you can make a config file ot whatever to populate your types or arrys...

see attachment...


Makeii Runoru

Oh cool. I think this will be a little easier for me to organize in my projects. I can set each object to a different txt file, or put objects or lists of similarity into the same txt file.

But I was trying out this stuff, and Play Basic only seems to read values on a certain position in the file, and doesn't regard a file's lines (with carriage return) when reading, or from what I know. Is there a way to set a value on a line of the txt file in stead of separating them with commas?
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

kevin


  I can't really follow that.  So mind give us a look at what code your trying and how you want to the data file set up ?