Returning the correct type

Blitz3D Forums/Blitz3D Programming/Returning the correct type

YellBellzDotCom(Posted 2007) [#1]
I was just wondering if there is better\ faster way to return a custom type then using a for loop.

for instance, if you have 100 player types with individual names and ids, but you wanted to find a player named Joe so you could smack him with a rubber duck, is the below code the only way to pick him out of a group of 100 players?


For P.player = Each player
	If P\name = "Joe" Then
		Smack_With_Rubber_Duck("Joe")
		Exit
	EndIf
Next


Thanks for any help here.

EDITED: Fixed the player name call, thanks KED


big10p(Posted 2007) [#2]
Well, that's one way if "Joe" is the only info you have to go on. However, in practice, isn't it more likely you can identify the player to smack in some other way, such as through picking or collisions etc., whereby you can retrieve the type instance directly. I mean, what are you doing in code to identify the player to be smacked as "Joe"?


Gabriel(Posted 2007) [#3]
It's not the only way, no. I'd say with a good hashing algorithm, a hash table is one of the best ways to do it.

http://en.wikipedia.org/wiki/Hash_table

There appears to be one in the code archives, but don't take that as a recommendation from me, as I no longer use B3D. It's by Octothorpe though, so it should be good.

http://www.blitzbasic.com/codearcs/codearcs.php?code=1439


Ked(Posted 2007) [#4]
You have a line of code messed up.

For P.player = Each player
     If P\name = "Joe" Then         ;CHANGED;
          Smack_With_Rubber_Duck("Joe")
          Exit
     EndIf
Next



Ross C(Posted 2007) [#5]
I personally wouldn't worry about it unless it's a massive list, ie a good couple of thousand of more. Hasing tables looks interesting though, i remember them from college. That wiki link sounds alot more complicated than i remember :D


Gabriel(Posted 2007) [#6]
You have a line of code messed up.

No he doesn't, his code is perfectly fine. The ".player" you have removed is not required but equally it is not invalid syntax either. It's just a technique some people use to make the code easier to read.


YellBellzDotCom(Posted 2007) [#7]
Thanks for that clear up Gabriel, I normally use the syntax that Ked recommended. I just did a quick mockup for this threads purpose, its not actual code Im using. I had no idea either way would work, though so thanks for the info.

Thanks for the other replies too, it will definitely give me some things to think about.

Searching through types is for the multiplayer portion of my game. Anytime a client makes a move the client will send its coordinates (maybe) to the server and the server will update the game. I was just wondering if there was a faster way then going through each player on a server to find the one that actually needs to move.

Thanks again.


YellBellzDotCom(Posted 2007) [#8]
How bout making a function in a seperate module that Im calling PlayerManager.bb like this...

Function FindPlayer(Name$)
	For P.player = Each player
		If P\name = Name Then
			Return player
			Exit
		EndIf
	Next
	
End Function


Im calling this function in the main client code like this...
TempNick$ = Input("Enter your Nickname: ")
CreatePlayer(TempNick)
	
P.player = FindPlayer(TempNick)



The problem with this is player isnt a valid returnable type. Any ideas?


YellBellzDotCom(Posted 2007) [#9]
Ok this works...

Function FindPlayer.player(Name$)
	For P.player = Each player
		If P\name = Name Then
			Return P.player
			Exit
		EndIf
	Next
	
End Function


This does what I need to find a player in my main client code using a function in a seperate bb.

Man, I love these forums...

Thanks again for the help!


PowerPC603(Posted 2007) [#10]
You can also give each player some sort of ID, which basically is just an integer number.
Inside each player's type-instance you create another field "ID".
When a player moves, you don't send it's name, but it's ID-number.
On the server-side you have an array of types where the ID's of the players refer to the index of that array.

Dim Players.player(100)

Type player
	Field Name$
	Field ID
End Type

For i = 0 To 99
	Players(i) = New player
	Players(i)\ID = i
Next

; Find a player with ID 5
P.player = Players(5)

Print P\Name


To find a player with the name "Joe", you'll still have to loop through the array to find the player you want/need.
But if you know the player's ID, then you've found him directly.