Addressing another type within same type

Blitz3D Forums/Blitz3D Programming/Addressing another type within same type

Swifty(Posted 2006) [#1]
I have a type called .player . As players log in it creates a new.player .

I have a main loop that checks each players moves/actions in turn. My problem is , if i want to address a different player than the one the loop is on, say to change a field value , how would i access that player and then continue with the main loop?

my only solution i have come up with so far, is to save players stats to the HD, and then i can access them randomly by player\name. the down side is this takes time accessing the HD. The upside is i have random access to any player field.


gosse(Posted 2006) [#2]
The what-what?
You can have more than one variable of the type defined. Or am I not understanding you right?

Example:
SeedRnd 143

Type player
	Field name$
End Type

players.player = New player
players\name = "Luke"
players.player = New player
players\name = "Joe"
players.player = New player
players\name = "William"
players.player = New player
players\name = "Jack"
players.player = New player
players\name = "Averell"

For players.player = Each player
	For otherplayers.player = Each player
		If Rand(1, 4) = 1 Then
			Exit
		End If
	Next
	Print players\name + " vs " + otherplayers\name
Next

While Not KeyDown(1)
Wend



Swifty(Posted 2006) [#3]
What i wanted was a way to access directly, another player, and not have to search through the type to find it. time needs to be kept to a minimum, as there could be many players in the loop


Who was John Galt?(Posted 2006) [#4]
Hard disc operations are going to be slower than searching a fairly substantial list.

How are you getting a situation where you know the name of the player you want but you don't have their type handle? We need an example.


Swifty(Posted 2006) [#5]
I get the player name from his EntityName

Lets say there are 1000 players
the loop is on player 56 , who targets player 990
I read player 990`s name from his Entity Name.
would it be faster to loop through ALL players upto 990 to get his data , or just read from HD. remember this would be a process I have to do every loop for many of the players logged in


Swifty(Posted 2006) [#6]
OK i done some testing

running a type with 10,000 players and searching for player 9999 takes from 4 to 9 millisecs to complete a search for a Int ,and 19 to 37 millisecs for a string search ,on my machine (not compiled).

As this is way more players then the game will ever hold, I agree that a search through a type is the best way to go.
I just wish there was a way to directly access a type without searching . for example: TypePointer = (player\name$="john") - pointer stops at first matching type :)


Tom(Posted 2006) [#7]
the loop is on player 56 , who targets player 990

You should have a 'target.Player' field that points to the current target type instance

Type Player
 Field x#,y#
 Field Target.Player
 Field Health
 ..
End Type

..

For thisPlayer.Player = each Player
 If thisPlayer.Target\health < 5 ...whatever
Next

did I understand you correctly?


Stevie G(Posted 2006) [#8]
Not 100% sure what your trying to do but you could probably make use of the old favourite - store the handle to the type instance in the entity name like so ...

type Mytype
  field Name$
  field Entity
  field Health
  field Etc..
end type

m.mytype = new mytype
m\Name = "Stevie G"
m\Entity = createsphere()
m\Health = 100
Nameentity m\Entity, handle( m )



To access directly based on the Target Entity ..

Test.mytype = object.mytype( entityname( TargetEntity ) )
Test\Health = Test\Health - 1


Another alternative would be to define an array of types ..


type MyType
  field name$
end type

dim MyTypes.MyType( 100 )

MyType(0 ) = new Mytype
MyType(0)\Name = "Joe Schmo"



So to access player 990 it would be ...

HisName$ = MyType( 990 )\Name



Stevie


Swifty(Posted 2006) [#9]
if someone leaves the game, then player 990 could become player 989 in the type list. I think i will have to search every loop

also the target and/or player stats can change from loop to loop, so i do need to read them every loop.


Jams(Posted 2006) [#10]
"Not 100% sure what your trying to do but you could probably make use of the old favourite - store the handle to the type instance in the entity name like so ...

Bingo! There's your answer right there, pay attention to this man :)


AdsAdamJ(Posted 2006) [#11]
I think I know what you mean, here's my take on it:

type npc_dat
field x, y, z
field target.npc_dat
end type

; Create player NPC
player.npc_dat = new npc_dat

; Create 10 NPC's
for n=0 to 9
npc.npc_dat = new npc_dat
next

; Makes the field 'target' point to the last 'NPC'
player\target.npc_dat = last npc_dat

; Delete the current player
delete player

; Point player.npc_dat to the last npc_dat
player.npc_dat = last npc_dat


You have to remember that when you declare a type, all you are doing is starting a list of 'npc_dat's. When you create a 'new npc_dat', you're just simply adding to that list. Imagine you're drawing a list out on paper and you have your red pen (your 'player.npc_dat' variable) lined up against the first item. The black pen is lined up against the first item as well - this represents the 'npc.npc_dat' variable. The pens can point to any one of the items in the 'npc_dat' list by either looping through them, or declaring another variable (aka using another pen to point with). All you are doing when you declare a 'randomvariablename.npc_dat' is creating a pointer to that list item. You can create pointers within types to other items in the list as well.