Type Lists?
BlitzMax Forums/BlitzMax Beginners Area/Type Lists?
| ||
Why does it create same name twice instead of two different names in the lists?
Strict
'How many Players to create?
Global Number_of_Player = 2
Type Player
Field id:Int
Field name:String
Field skill_level:Int
End Type
'Create a list to store all Player
Global PlayerList:TList = CreateList()
Local Boss:Player
Boss = New Player
Boss.id = 1
Boss.name = "David R"
Boss.skill_level=77
Boss.id = 2
Boss.name = "Paul C"
Boss.skill_level=17
Graphics 800, 600
Drawtext "Num"+" Name"+" Skill Level",0,0
' Create a bunch of new Player
For Local N = 1 To number_of_Player ' Number_of_Player is a Global
Local NewBoss:Player
'Put the address of this new Boss into variable Player
NewBoss = New Player
DrawText Boss.ID,0,50+N*10
drawtext Boss.name,50,50+N*10
Drawtext Boss.skill_level,150,50+N*10
'Put this Player called NewBoss into our PlayerList
ListAddLast( PlayerList, NewBoss )
Next
While Not KeyDown(Key_Escape)
flip
Wend
|
| ||
| I have create little abit better code but what I want to see on screen is like this Num Name Skill Level 1 David R 77 2 Paul C 17
Strict
'How many Players to create?
Global Number_of_Player = 3
Global N
Type Player
Field id:Int
Field name:String
Field skill_level:Int
Method Players()
Local Boss:Player
Boss = New Player
Boss.id = 1
Boss.name = "David R"
Boss.skill_level=77
Boss.id = 2
Boss.name = "Paul C"
Boss.skill_level=17
DrawText Boss.ID,0,50+N*10
DrawText Boss.name,50,50+N*10
DrawText Boss.skill_level,150,50+N*10
End Method
End Type
'Create a list to store all Player
Global PlayerList:TList = CreateList()
Local test:player = New player
Graphics 800, 600
DrawText "Num"+" Name"+" Skill Level",0,0
' Create a bunch of new Player
For Local N = 1 To number_of_Player ' Number_of_Player is a Global
Local NewBoss:Player
'Put the address of this new Boss into variable Player
NewBoss = New Player
test.Players()
'Put this player called NewBoss into our PlayerList
ListAddLast( PlayerList, NewBoss )
Next
While Not KeyDown(Key_Escape)
Flip
Wend
Have I create my first Object Orient programming Basic? I just been learning from BlitzMax_OOP_Tutorial as I seem to get hang of it. |
| ||
-something wrong (IMHO)Boss = New Player Boss.id = 1 Boss.name = "David R" Boss.skill_level=77 Boss.id = 2 Boss.name = "Paul C" Boss.skill_level=17 here you're creating 1 object boss. then you assign a value to each field twice. it's the same doing a=2 a=4 -the player list can be embedded into the object here my solution (there's some code optimization to do but it works) Strict
'How many Players to create?
Global Number_of_Player = 2
Global N
global names:String[] = ["David R", "Paul C"]
global skill_levels:int[] = [77, 17]
Type Player
Field id:Int
Field name:String
Field skill_level:Int
global idGen:int = 0
method new()
idGen:+1
self.id=idGen
endmethod
function createPlayer:Player(name:String, skill:int)
Local Boss:Player
Boss = New Player
Boss.name = name
Boss.skill_level=skill
return Boss
Endfunction
End Type
'Create a list to store all Player
Global PlayerList:TList = CreateList()
Graphics 800, 600
DrawText "Num"+" Name"+" Skill Level",0,0
' Create a bunch of new Player
For Local N = 0 To number_of_Player-1 ' Number_of_Player is a Global
'Put this player into our PlayerList
local p:player = Player.createPlayer(names[N], skill_levels[N])
ListAddfirst( PlayerList, p)
Next
for local Boss:Player = eachin PlayerList
DrawText Boss.ID,0,50+Boss.id*10
DrawText Boss.name,50,50+Boss.id*10
DrawText Boss.skill_level,150,50+Boss.id*10
next
flip
waitkey
end |
| ||
| @HotShot2005, johnnyfreak is probably right but, unfortunately, there are so many oddities and errors in your code I am guessing you wrote it without really knowing why you were doing things. Is that right? If so, I would suggest you check out : Beginners Guide to Bmax and Learning Object-Oriented Programming in Blitzmax |
| ||
| I dont think I have fully learn Object-Oriented Programming in Blitzmax very well and I guess I have keep learning those both tutorial until I fully understand what is what. |
| ||
Local Boss:Player Boss = New Player Boss.id = 1 Boss.name = "David R" Boss.skill_level=77 Boss.id = 2 Boss.name = "Paul C" Boss.skill_level=17 This is like saying, Local apples:Int apples = 5 apples = 7 You do not have 2 seperate bosses, you have one boss. The last paragraph of code overwrites the original data. 'Put the address of this new Boss into variable Player NewBoss = New Player Don't know if this is a typo but you got Boss and Player the wrong way round in the description. |