Arrays of Objects

BlitzMax Forums/BlitzMax Beginners Area/Arrays of Objects

Armorproof(Posted 2011) [#1]
What I have tried to create is a Field of an Array of Objects.
So far, I believe I have got the basics of it working, but I am having lots of troubles with accessing a field of one of the objects in the array.
What I have for trying to access it is as follows:

DrawText level.levelArray[1,1].value,cntx*14,cnty*14

I am taking a wild guess and saying that this is the problem. Is there a way to bring up the object, and then access one of its values, instead of doing it in one line? Or am I looking at it from the wrong angle all together.

Would greatly appreciate someones help, thank you.


Jesse(Posted 2011) [#2]
That line of code doesn't really explain much about your problem.
are you storing different type of objects in the array?
one way or the other, you have to cast it back to the object class it was originally before you can access the object fields.
if you are just putting one kind of object in the array than create the array as the object type you want to refer to, that way you can do as your sample code above.

if you are storing multiple objects types in the array, the best way to handle it is trough polymorphying by creating the array type as the base type.

Don't create the array as type object, that will complicate things quite a bit.

Last edited 2011


Midimaster(Posted 2011) [#3]
but this line would work!

Did you care about CREATING the objects with the NEW commands?

SuperStrict

Type TLevel
	Field Value%	
End Type

Type TypB
	Field C:TLevel[9,9]
End Type

Global A:TLevel[9,9]

' example one:
A[0,0]=New TLevel
A[0,0].Value=123
Print A[0,0].Value

'Example two:
Global B:TypB = New TypB
B.C[1,1]=New TLevel
B.C[1,1].Value=456
Print B.C[1,1].Value



TomToad(Posted 2011) [#4]
What kind of error are you getting? Try putting Strict or Superstrict at the top of your code and see if you get better info on what the problem is.


Czar Flavius(Posted 2011) [#5]
We need to see the definition of the type with the array.


ziggy(Posted 2011) [#6]
Is it a casting issue maybe?
Are you converting the objects to their types before trying to access the fields?


TomToad(Posted 2011) [#7]
I can't know for sure since we don't have the code here, but I'm guessing it is a Field/Global problem.

I'm betting he has a field in type TLevel intended to hold a list of TLevels. He creates one TLevel and adds it to the array. Then he creates another TLevel and adds that to the array. Problem is, if you have the array as a field, then each instance of the TLevel will have it's own individual array. My guess is he is doing something like this

Type TLevel
	Field LevelArray:TLevel[10,10]
	Field value:Int
End Type

Local Level:TLevel

For Local x:Int = 0 To 9
	For Local y:Int = 0 To 9
		Level = New TLevel
		Level.Value = 10
		Level.LevelArray[x,y] = Level
	Next
Next

Print Level.LevelArray[1,1].value


Problem is that this would not work since the LevelArray within the current Level has only one element set. What I think he wants to accomplish is this
Type TLevel
	Global LevelArray:TLevel[10,10]
	Field value:Int
End Type

Local Level:TLevel

For Local x:Int = 0 To 9
	For Local y:Int = 0 To 9
		Level = New TLevel
		Level.Value = 10
		Level.LevelArray[x,y] = Level
	Next
Next

Print TLevel.LevelArray[1,1].value

Now all instances share the same global array and can be accessed with TLevel.Levelarray[n,n]

A second possible problem would be something like
[code]
Global LevelArray:TLevel[9,9]
LevelArray[9,9] = new TLevel

Which won't work since the array would be indexed with [0,0]-[8,8]


Czar Flavius(Posted 2011) [#8]
Now it's getting ridiculous. How can you guess all that from one vague description?


Armorproof(Posted 2011) [#9]
Even with my vague description though, you guys helped me get closer to the solution of my problem :D
Thanks, even if you guys didn't know what you were helping with lol