How to reference a Tlist that's part of a Type?
BlitzMax Forums/BlitzMax Beginners Area/How to reference a Tlist that's part of a Type?
| ||
Hi. I've used global lists before with great success. But I'm having trouble when I use a list as a field inside an object. I'm not sure how to reference it or use Eachin to loop through the list. I'm used to doing this: Global weaponlist:TList weaponlist = CreateList() [...] weaponlist.AddLast(myWeapon:weapon) [...] For Local myWeapon:weapon = EachIn weaponlist If randomweaponid = myWeapon.number then [...] Next That works great. But how do I do this? Type player Field number:Int 'player number (which player) Field weaponlist:TList [...] Local weaponlist:TList weaponlist = CreateList() [...] End Type For Local myPlayer:player = EachIn playerlist myPlayer.weaponlist.Last() = globalweaponmasterlist.First() 'this doesn't work. globalweaponlist.RemoveFirst() Next I'm sure it's simple. I'm just lost. :) Last edited 2011 Last edited 2011 |
| ||
(Player list (if you want a player list) should be global inside the type) otherwise for weaponlist "eachin player.Weaponlist" That is there isnt a list of Player (there was in B3d), there is only the member field of player.Weaponlist, to get playlist you want to add it as global field Last edited 2011 |
| ||
Type player Field number:Int 'player number (which player) Field weaponlist:TList [...] Local weaponlist:TList '<---------delete this line weaponlist = CreateList() [...] End Type For Local myPlayer:player = EachIn playerlist myPlayer.weaponlist.AddLast(globalweaponmasterlist.RemoveFirst()) Next Last edited 2011 |
| ||
You need to create the list object before you can add to it and the code has to be in a method. the new() method is a good place as thats called when the player object is created.method new() self.weaponlist =new tlist end method this has taken bleedin ages to type on my android phone. |
| ||
he didweaponlist = new Tlist is the same: weaponlist = CreateLIst() |
| ||
Double post Last edited 2011 |
| ||
Read his post again, isnt it that he is having problems accessing PLAYERLIST? AS in the list of type player. I HATE giving code, cos 1) I dont think ppl learn when you just give them the code 2) If I make a mistake I look an arse. 3) If I keep my answers vague I cannot by wrong (Which I still agree I might be) BUT Type player Field number:Int 'player number (which player) Field weaponlist:TList Global Playerlist:TList= CreateList() Method New() Self.Playerlist.AddLast(Self) Self.weaponlist = New TList End Method End Type For Local myPlayer:player = EachIn player.playerlist myPlayer.weaponlist.AddLast(globalweaponmasterlist.First()) 'this doesn't work. globalweaponlist.RemoveFirst() Next The line that doesnt work STILL wont work cos I didnt define globalweaponmasterlist. Also "Playerlist" would by most be called "List" and accsesed as (T)Player.list or self.list As I said, the type player is automaticly put on a list in B3d, but in Bmax you need to create the list, add the instance to it (and delete it) your self Last edited 2011 Last edited 2011 Last edited 2011 |
| ||
@H&K you are right I just ass-u-me that playerList was defined as global outside of the player type. I failed to see that he used player.playerList in the for loop(or maybe he forgot to show it as a type global?). any way one of the problems was that he created a local variable for the weaponList after he had defined it as a field in the type. which would maintain a null value for the weaponsList in the field and quite possibly the reason for his error. also he only mentioned an error in his field list and that is all I was working with(the weaponList) also this: myPlayer.weaponlist.AddLast(globalweaponmasterlist.First()) 'this doesn't work. globalweaponlist.RemoveFirst() ' <---- should be globalweaponmasterlist.RemoveFirst() is the same as this: myPlayer.weaponlist.AddLast(globalweaponmasterlist.RemoveFirst()) just in case you didn't know. :) I do believe others learn from posting a solution to their problem, specially when they have been pondering and can't figure out the solution. I know that's how I learn. and I don't mind posting code cause if it's wrong I would have learned something from it as well and is human to make a mistake, at least that's what I think. I also don't go out writing code just because they want it unless I see that they have been trying with out success. Last edited 2011 Last edited 2011 |
| ||
I am confused by these posts. To convert Global weaponlist:TList = New TList inside a type, put Type TPlayer Global weaponlist:TList = New TList End Type To access this list, you need to put TPlayer.weaponlist Instead of just plain weaponlist. Inside any function or method of TPlayer, as a shortcut, you can omit the TPlayer part and just call it weaponlist. It will automatically assume you are talking about the list already in the type. In other types or functions, you will need the TPlayer part. |
| ||
Yes. (And no) Because Global weaponlist:TList = New TList Type TPlayer Global weaponlist:TList = New TList End Type Whilst what you say is correct in general, in this example Weaponlist is still a field and there is ANOTHER list, PlayerList Type TPlayer Field weaponlist:TList = New TList Global Playerlist:Tlist = New TList End Type TPlayer.playerlist.weaponlist I use Blide, so its easier for me to always use full names (Type name. or Self.) If you read above I said it was better to rename playerlist as list, I said this cos then it would be Tplayer.list.weaponlist Now in blide this would be typing Tpl.l.we.last() (Depending on other type/field names). Blide just makes using types so much easier and faster. And with well written types help tips popup for each stage. I probably could do without all the typenames and selfs. Last edited 2011 Last edited 2011 |
| ||
You mean he wants two lists of the same name, one in full global and the other in type global??? Call them different names and avoid all this mess. |
| ||
I believe he as a pool of weapons as a global and that is what he is calling a globalWeaponList and he has a WeaponList for the weapons a Player can carry and that is in the type player field. he mentioned that he knows how to use global Lists but he doesn't know how to use list as fields and that is what he wanted to know how to use. I think. At least that is what I understood or maybe I am wrong? Last edited 2011 Last edited 2011 |
| ||
You mean he wants two lists of the same name, one in full global and the other in type global??? Nearly. What I understood is three list, (same as jesse I think) he has an ordinary type-list of Players, then a Global selection list of available weapons, and then a list of the weapons each player has. (But it might be that I keep adding the playerlist ;)So FULL Global weaponlist, In Type (ie one instance Global) Playerlist, One each instance (ie field), another Weaponlist Call them different names and avoid all this mess. I agree, however this is Another situation where full name is helpful.Last edited 2011 |
| ||
He has not made any replies. I think we need him to clarify what he needs. |
| ||
Yep. Some people are so rude. Not even a thank you. |
| ||
Sorry guys. I spent alot of time over last weekend working on the code and putting these principles to use. I even typed up a reply but didn't post it cuz I was still working stuff out. I didn't mean to be rude but I can see that I was just by letting too much time go by. Anyway, the early assumptions you guys were making were correct. playerlist was just a list of the player objects. I use it to loop through and look at each player. I made a global list and it works fine. I never considered putting playerlist into the player type. That seems odd to me -- like putting the bucket into the definition of the things that you'd put into the bucket. But I now see why it makes sense. I still left it as a global though. :) weaponlist was a list of weapon objects that each player has. That's why I thought I would try to put it into the player type instead of making it a global list. And also, then I could use EachIn to loop through the players and then, the weapons for each player. That's where I got stuck. Here's a bit of the code I wrote. This is about cards and not weapons, but it's the same idea: [...] For Local myPlayer:player = EachIn playerlist [...] 'set x and y location depending on the player DrawText (myPlayer.name,x-20,y-20) For Local myCard:card = EachIn myPlayer.handlist DrawImage (myCard.image, x, y) x = x + 30 Next Next So each player object is in a playerlist, each player has a bunch of cards objects in a list (handlist) defined within the player type, and each card object has fields (value, suit, image, etc). This works like a champ thanks to you guys. I get exactly what I needed which is the ability nest my EachIns and get access to player.name and card.image at any given moment. I am creating the handlist in a Create Function (not the New Method) inside the player type the way you guys suggested: [...] Field handlist:TList 'list that holds the player's hands [...] myPlayer.handlist = CreateList() ' create this player's hand (which is a list of cards) playerlist.AddLast(myPlayer:player) ' add new player to playerlist Couple other things: H&K: I do learn from code snippets so I appreciate you posting. I code for fun. That's what I like about Blitzmax. It's not frustrating and you can get stuff working pretty easily. But sometimes I get stuck. Having you guys here is very appreciated. |