Nesting Arrays in Types

BlitzPlus Forums/BlitzPlus Beginners Area/Nesting Arrays in Types

covi2955(Posted 2012) [#1]
So I've tried looking everywhere for this and haven't found an answer.
Is it possible to make one of the fields in a new Type an array? And if so, how.
What I'm working on is an RPG and I want to do an inventory system. My items are types and I was hoping to do an array of types as the inventory, but I desire that all characters (PCs and NPCs) have their own inventory. It would make it a lot easier if I could just make it a listarray rather than having to make 10 fields named
field item1.item
field item2.item
etc.

Any solutions or help would be greatly appreciated


Midimaster(Posted 2012) [#2]
you can use types in array and arrays in type:

[bbcode]Type CardTyp
Field Color%
Field Points%
End Type

Dim Card.KartenTyp(32)

For i=1 To 32
Card(i) = New CardTyp
Next
[/bbcode]


and:

[bbcode]Type CardTyp
Field Color%
Field Points%
End Type

Type PlayerTyp
Field Value%[5]
Field Card.CardTyp[12]
End Type

Player.PlayerTyp = New PlayerTyp
Player\Value[0]=123
Player\Card[0]=New CardTyp
Player\Card[0]\Points=5
[/bbcode]


Bobysait(Posted 2012) [#3]
Fixed arrays can be used for what you're looking for

Type Item
 Field Name$
 Field Class%
 Field Icon%
End Type

Type Inventory
 Field Items.Item[100]
End Type

Function CreateItem.Item(name$, class%, icon%)
 Local it.Item = new Item
 itname=name
 itclass=class
 iticon=icon
 return it
End function

Local Inv.Inventory = new Inventory

Local MySword.Item = CreateItem("Sword", ITEM_WEAPON_ONEHAND, LoadImage("Icon_Sword.bmp"))
Local MyShield.Item = CreateItem("Sword", ITEM_WEAPON_SHIELD, LoadImage("Icon_Shield.bmp"))

InvItems[1] = MySword
InvItems[2] = MyShield


[edit]
Damned, 3 minutes ...

Last edited 2012


covi2955(Posted 2012) [#4]
Oh thank you guys so much. That's exactly what I'm looking for.
Just out of curiosity though, is it possible to do a multi dimensional array field?
For example (though I've tested this and it doesn't work)
something like

type map
field grid.tile[5,5]
end type

to do a 2 dimensional array of .tile types? I haven't figured out how to do it if its even possible.

Thanks again for the quick responses guys :)

Last edited 2012


Bobysait(Posted 2012) [#5]
Fixed arrays are only one dimension.

If you want two dimensions, you can use a Dim (but not in a "Field" type)
or a fake, using a one dimension fixed array



Last edited 2012


Midimaster(Posted 2012) [#6]
it is "possible":

Type TileTyp
	Field Image%, Value%
End Type

Type RowTyp
   Field Col.TileTyp[5]
End Type

Type PlayerTyp
    Field Row.RowTyp[5]
End Type

Player.PlayerTyp = New PlayerTyp

Player\Row[0]=New RowTyp
Player\Row[0]\Col[0]=New TileTyp
Player\Row[0]\Col[0]\Value=123


also something like that, where all field are defined imediately:

Const Dimension%=5
Type YTyp
	Field Image%, Value%
End Type

Type XTyp
   Field Y.YTyp[Dimension%]
End Type


Type PlayerTyp
    Field X.XTyp[Dimension]
End Type

Player.PlayerTyp = New PlayerTyp

; now define all:
For i=0 To Dimension
	Player\X[i]=New XTyp
	For j=0 To Dimension
		Player\X[i]\Y[j]=New YTyp
	Next
Next

; now use them imediately:
Player\X[3]\Y[2]\Value=123



misth(Posted 2013) [#7]
Umm... Is this for Blitz3D instead for Blitzplus????

I tried these examples but none of them worked. You guys are mixing up BMX with B+.....??


EDIT:
Oh nevermind... I tried to create 2D arrays. But still the examples wont work...? Maybe because '[' != '(' ?


Yasha(Posted 2013) [#8]
There are a couple of typos in Midimaster's second example (I assume it was typed straight into the forum window):

-- 'PlayerX' should be 'Player\X' (three places)
-- 'For Y = 0 to Dimension' should be 'For j = 0 to Dimension'

Other than that, both examples are perfectly legal BlitzPlus.

The PlayerX typo gives a syntax error so explains itself; the reason using Y instead of j causes a crash is more subtle: because j is used instead of Y in the loop body, but isn't being incremented in the loop header, all of the new objects are being stuck in \Y[0]. Not only is this a memory leak, but it also means that trying to dereference \Y[2] further down is actually trying to get a field out of Null, which is a runtime error (BlitzPlus doesn't seem to be quite as friendly as Blitz3D here: B3D would have given an unidentified memory access violation even in release mode, rather than going straight to the OS-level crash).


Midimaster(Posted 2013) [#9]
thanks for the bug report. The backslash did not work on the forum for some days. Each time, when I did edit a post it destroyed all backslashes.

Now it seems to work again. I will try to correct my posts in two hours. At the moment I'm in office...

The y has to be a j. Thanks for that!


misth(Posted 2013) [#10]
Okay, thanks Yasha! Now they seem to work just fine. :)
Thanks Midimaster for these examples! :)

So, it looks like B+ only handles 1D arrays in types? And I guess those can't be redimmed, right?