Problem with types
Blitz3D Forums/Blitz3D Beginners Area/Problem with types
| ||
Hello. I have written the following code which creates two players using a type and a function to get a player's health value. The PlayerHealth(2) is supposed to return player two's health (in this case 60) but it is returning player one's health (100). Please help me correct it. ;Create first player p.player=New player p\prop.properties=New properties p\prop\health=100 ;Create second player p.player=New player p\prop.properties=New properties p\prop\health=60 ; Print PlayerHealth(2) WaitKey() ; Type player Field prop.properties End Type Type properties Field health End Type ; Function PlayerHealth(player_number) For p.player=Each player count=count+1 If count=player_number Then For p\prop.properties=Each properties Return(p\prop\health) ;return health Next EndIf Next End Function |
| ||
If you're gonna hardcode the players like that anyway, why not just make an array of the players? |
| ||
The variable count will start at 0 so you will need to use PlayerHealth(1) to get player 2's data. |
| ||
"If you're gonna hardcode the players like that anyway, why not just make an array of the players?" This isn't my real program, i just wrote it to show an example of my problem. In my real program I won't know how many type objects that will be created. "The variable count will start at 0 so you will need to use PlayerHealth(1) to get player 2's data." It doesn't work. PlayerHealth(1) returns 100 (which is player 1's health, as the function is supposed to return) But I can't get player 2's health. |
| ||
You don't need the "For p\prop.properties=Each properties" part:;Create first player p.player=New player p\prop.properties=New properties p\prop\health=100 ;Create second player p.player=New player p\prop.properties=New properties p\prop\health=60 Print PlayerHealth(2) WaitKey() Type player Field prop.properties End Type Type properties Field health End Type Function PlayerHealth(player_number) For p.player=Each player count=count+1 If count=player_number Then ;For p\prop.properties=Each properties Return(p\prop\health) ;return health ;Next EndIf Next End Function |