variable for type's fieldname?

Blitz3D Forums/Blitz3D Beginners Area/variable for type's fieldname?

ryan scott(Posted 2004) [#1]
is there some way of accessing a field in a type by using a variable name

instead of:

t.trigger=new trigger
t\xposition = 1

what if i had 'xposition' in a variable?

t\{fieldname} = value

is there any way to do this?

it would make data loading so much easier, because you could write nice files like this:

[newobject]=triggerarea
triggerid=4
x=10
z=10
width=5
depth=5

and be able to add new fields without having to add lines to your loading routine. even with a nice file structure like this, currently my load routine has to have at least 1 or 2 lines to handle each and every field. it's really clumsy. (where are perl hashes when you need them most??)

i don't want to get away from using types, but i guess if i had to i could use the 'soups' routines in the code archives. it's just that i anticipate a performance hit from the string manipulation going on during runtime.

anyone have any ideas?


soja(Posted 2004) [#2]
I've read through this 3 times and I still don't understand the issue...


Perturbatio(Posted 2004) [#3]
I think he wants some sort of eval function for use with type fields.
(Not that I can see the point of it).


PowerPC603(Posted 2004) [#4]
I've been looking for this too.

My game will use some sort of INI-files, where there are blocks of data, while each block contains several lines of data (or parameters) like this:

[Sector]
Name              = Sol system
Description       = This sector is the best know sector
PositionX         = 250
PositionY         = 250
Race              = Terrans

[Sector]
Name              = Barnards star
Description       = This sector is an agricultural one
PositionX         = 200
PositionY         = 180
Race              = Aliens


When I'm reading this file, I check for a data-block (= "[Sector]").
When such a line is found, I read all following lines until there's an empty line.

But the issue is reading each line (I can do it already, but in a clumsy way).

For every line I read from the file, I split the word before the equal sign and the one after the sign into two variables: the first one is "ParameterName" and the second is "ParameterValue".
Then I try to find a match for the parametername (with a select case statement), where I put the value in the correct field of a type.

But it would be easier if I could use the content of the variable "ParameterName" as the fieldname.

Now I do this (NOTE: I use arrays of types to store my data):
        Select ParameterName$
            Case "Name"
                ArraySectors(SectorNumber)\Name$ = ParameterValue$
            Case "Description"
                ArraySectors(SectorNumber)\Description$ = ParameterValue$
            Case "PositionX"
                ArraySectors(SectorNumber)\PositionX% = ParameterValue$
            Case "PositionY"
                ArraySectors(SectorNumber)\PositionY% = ParameterValue$
            Case "Race"
                ArraySectors(SectorNumber)\Race$ = ParameterValue$
        End Select


If I just could use something like this:
ArraySectors(SectorNumber)\ParameterName$ = ParameterValue$


then it would be simpler (of course the type must have the field where the ParameterName would point to).

The above line would use the content of the variable "ParameterName" (example: "Name") as the field-pointer (or how would you call it) and put the value (content) of "ParameterValue" in the Name-field of the type.


WolRon(Posted 2004) [#5]
Can't you just use an array within a type?
ArraySectors(SectorNumber)\ParameterName[pm] = ParameterValue

Notice the square brackets (not parenthesis)...
Type atype
	Field normalfield
	Field anarray[50]
End Type

typeinstance.atype = New atype
typeinstance\normalfield = 5
typeinstance\anarray[4] = 27

Print typeinstance\normalfield
For iter = 1 To 10
	Print typeinstance\anarray[iter]
Next

WaitKey()



TomToad(Posted 2004) [#6]
I'm not sure if that would be possible. When the compiler compiles your program, it replaces variable names with memory pointers. The compiler checkes to make sure the fields exists at compile time. I guess you could make a system where such a thing would be possible, but that would greatly slow down the use of types. Better to have the slowdown at the beginning of your program parsing the parameters than during the program where your types are being accessed n times per second.


_PJ_(Posted 2004) [#7]
Yeah, Tom's right. I had a similar idea whereby a value was read, then another value was loaded based on the read variable. Because it was unknown at Compilation time, I got an error.


PowerPC603(Posted 2004) [#8]

Can't you just use an array within a type?



Actually no, I can't do that.
Each index of the array represents a sector (solar system) within my game, and I use the types in those arrays to hold all data about a sector.

Doing the reverse is even more complex (for my game anyways).
Besides, when I'm reading station- and planet-data, I'm using 2-D arrays (and for commodities being sold there even 3-D arrays), so that's not an option.

@ TomToad: Thanks for clearing that up.
Now I see why it cannot be done (in Blitz anyway).


jondecker76(Posted 2004) [#9]
I wouldn't try to do this with types. IMO, it would be a better choice to use XML to store the data - thus allowing you to add/remove things easily without having to mess with loading and saving routines every time you want to try something different.


_PJ_(Posted 2004) [#10]
galactic Allegiance uses a complex Type structure, read from a file when a new 'sector' (solar system) As an example, here's an extract from the sector builder: