another types question

BlitzPlus Forums/BlitzPlus Programming/another types question

rdodson41(Posted 2004) [#1]
Is there a way to have nested types? Such as

Type player
Field w.weapon
End Type

After making a bunch of players each with multiple weapons, how would i go about accessing the weapons of just a certain player. I cant seem to find a way to find just p\w.weapon instead of every w.weapon. Can somebody help?


Regular K(Posted 2004) [#2]
p\w\weapon ?


WolRon(Posted 2004) [#3]
Type weapon
  Field weapontype
  Field ownerID
End Type

Type player
  Field ID
  Field w.weapon
End Type

Dim p.player(8) ;8 players

Const pistol = 1
Const rifle = 2

For ID = 1 To 8
	p(ID) = New player
	p(ID)\ID = ID
	p(ID)\w = New weapon
	p(ID)\w\weapontype = pistol
	p(ID)\w\ownerID = ID
Next



;select only player 3's weapons
For thisweapon.weapon = Each weapon
  If thisweapon\ownerID = 3 Then Print "Player 3's weapon"
Next


WaitKey()
End



rdodson41(Posted 2004) [#4]
Is it not possible to do something like this without keeping an extra variable in the Type, ownerID? I want to just be able to do something like

For player3\w.weapon=Each of player3’s weapons
	dostuff(p\w\weapontype)
Next



morduun(Posted 2004) [#5]
Nope. You can manually link type instances just like you'd do a linked list in C, you can store references of their handles in a bank and you can store groups of them in arrays, but you can't store separate native lists the way you're describing.


skn3(Posted 2004) [#6]
Try this
Type players
	Field name$
	Field first_weapon.weapons
	Field last_weapon.weapons
End Type

Type weapons
	Field name$
End Type

Function NewWeapon.weapons(player.players)
	Local weapon.weapons

	If player <> Null
		weapon = New weapons
		If player\last_weapon <> Null
			Insert weapon After player\last_weapon
		Else
			player\first_weapon = weapon
		End If
		player\last_weapon = weapon
		Return weapon
	End If
	Return Null
End Function

player.players = New players
player\name$ = "player1"
weapon.weapons = NewWeapon(player)
weapon\name$ = "gun"
weapon.weapons = NewWeapon(player)
weapon\name$ = "hammer"
weapon.weapons = NewWeapon(player)
weapon\name$ = "sword"

player.players = New players
player\name$ = "player2"
weapon.weapons = NewWeapon(player)
weapon\name$ = "flamethrower"
weapon.weapons = NewWeapon(player)
weapon\name$ = "gun"
weapon.weapons = NewWeapon(player)
weapon\name$ = "grenade"

For player.players = Each players
	Print player\name$
	
	;how to loop through a players weapons
	weapon.weapons = player\first_weapon
	While weapon <> After player\last_weapon And weapon <> Null
		Print " - "+weapon\name$
		weapon = After weapon
	Wend
Next
stop


Blitzmax has the ability to do this properly. Not that is any help for any of us pc users at the moment.


rdodson41(Posted 2004) [#7]
Thanks skn3[ac], since it seems that what i want can't be done, your example looks like a good way to do it. I might also experiment with WolRon's way.