Types? TList? Globals?

BlitzMax Forums/BlitzMax Beginners Area/Types? TList? Globals?

Apollonius(Posted 2007) [#1]
I'm reading through types, I though I knew about types but I'm having quite some problems when I actually try my hands at it:

I wana know, do type globals stay in types? and do I need to do mytype.my_global_var to access it? or:
local instance:mytype.my_global_var?

How do I access list within types?

Type Troom
	Field w:Int
	Field h:Int
	
	' Room List
	Global RoomList:TList=New TList
	' On Creation
	Method New()
		RoomList.AddLast(Self)
	End Method
End Type


Then I try to access it using:

intro:Troom=New Troom
level1:Troom=New Troom
level2:Troom=New Troom

For Local s:String=EachIn RoomList
    Print s
Next

It's not working for some reason... I dont understand TList too well. I also tryed to do RoomList.Count() but didnt work.. says count's not an identifier.. or something..

oh and if I understand correctly
method new() is every time an instance(object) is created it is runed for that instance..?

I'm a Type, TList.. confused individual..


H&K(Posted 2007) [#2]
Troom.RoomList.count()

For Local s:String=EachIn TRoom.RoomList
    Print s
Next


You dont need the "TRoom." in any member function or Method of TRoom, however I nearly always use the full Type name.


Apollonius(Posted 2007) [#3]
All fixed, no more error poping but why doesnt it print the objects?

It only prints 3(for the count())

Include "types.bmx"

intro:Troom=New Troom
level1:Troom=New Troom
level2:Troom=New Troom


For s:String = EachIn Troom.RoomList
    Print s
Next

Print Troom.RoomList.count()


Not printing a list of the objects?


Brucey(Posted 2007) [#4]
Your TList contains a list of Trooms. When you do
For s:String = Eachin....

You telling BlitzMax - "For each String in the list...."

But you don't have strings in the list ;-)

How about.
For r:Troom = Eachin Troom.RoomList
    Print r.toString()
Next

...and add a
Method toString:String()
    Print w + ", " + h
End Method

to your Troom Type...


Czar Flavius(Posted 2007) [#5]
Personally I think your rooms would be better off stored as an array if this is for your RPG game :P

An array allows you to access any particular room instantly. A list means you have to search through the entire list until you find the room you're looking for. So if a door leads to room #30, you're going to have to look at room #1 #2 #3... #29 #30 in the for loop until you find the one you are looking for. An array lets you access any room directly.

Lists are more useful when you don't know how many things are going to be in the list, and they are going to be added or removed "at will". Assuming your level doesn't change as you go through (new rooms being made or destroyed) lists are less useful.


Apollonius(Posted 2007) [#6]
so objects aren't string. That's why it spitting out numbers now? :o

What would I do if I wanted to access the 2rd object in the list?


H&K(Posted 2007) [#7]
So if a door leads to room #30, you're going to have to look at room #1 #2 #3... #29 #30 in the for loop until you find the one you are looking for
Unless each door keeps two fields with the links to the rooms on either side of it. But yes, and this point an array would seem to be better.

What would I do if I wanted to access the 2rd object in the list
If you wanted the Nth object in a list, (And if you are doing that rather than always the NEXT object, the Czar sudgestion about arrays is better), you ask for the First object, then the next object then the next object n-1 times.

If you still think that you always want a List, then make a Global array inside TRoom, and make each element the link to the room in the list. ie Element 0 is the link to the first Troom and Element n-1 is the link to the Nth Room. So as you New each Troom you also resize the array (Which is slowwwwwww), and add that new link to the array


Czar Flavius(Posted 2007) [#8]
A list is like a pile of paper. Say you want the paper with a green triangle (or something). You can't tell where it is just by looking at the pile. The first sheet has a blue square, so you take that one off. The second sheet isn't it either. The only way you can find it is to, one-by-one, remove sheets of paper from the pile until you find that one sheet you are looking for. This is slow, of course.

Arrays are like a book, where each page has a page number, and a contents/index at the front. If you want a specific page, you can instantly find what you are looking for in the index, and go directly to that page. You don't need to look at every page before it in order to find it.

So what is the point of lists? If you had more paper to add, you simply put it onto the pile. To add an extra page into the book, however, is going to be hard work. If you put it in the middle, for example, all pages afterwards will need to be updated in the index.

So in short, lists are good where you can be adding or removing from it quickly (like bullets from a gun - one second there are 10, the next they have all gone) but they are not good when you need to access a specific item in the list. Since persumably there is nothing unique about each bullet, and you will need to go through them all one by one to update them anyway, they are perfect for storage in lists.

Arrays are good when you need to access a specific item quickly because the items are unique in some way (like a room), rather than just clones of some generic thing that always acts the same (like a bullet). Arrays are bad for when you need to add or remove elements, as this process is slow, very slow if in the middle of the array.

Since you'll be needed to access specific rooms, to find out which is the next one, and it's unlikely you will be adding or removing rooms (have a variable that says the number of rooms on the level, and initialize the array to that number) I would say arrays would be best for your rooms.


Apollonius(Posted 2007) [#9]
This is getting confusing... between my questions and my code being re-written at the same time, I get lost, because I ask question then re-write stuff and then those questions don't apply anymore and I have no clue what I'm talking about or what your talking about lol.

I re-wrote my concept added in a total of 3 Types to controle Screens, Worlds and Rooms

Type TScreen, TWorld, TRoom
TScreen=
Intro, Menu(Singleplayer->Management, Credits, ect..), World

TWorld=
Think of a 2 Dimensional Tile Map. Let's say 4 by 4 and then in all those "worlds" there are:

TRoom=
Rooms attached togheter

Now I have to find a way to write down my concept and get untangled in everything.

Thanks for the help guys, very appreciated, I'm just very antangled right now lol

Nothing Finalised yet though.


Jesse(Posted 2007) [#10]
Apollonius go through this want to be "tutorial" and see if it helps you. it don't run on its own but I hope it can
help you. you can copy paste the code below to your bmax EDI to read it easier(maybe):

Include "types.bmx"
 

level1:Troom=New Troom ' created an object of type Troom with the name level1.

'lets initialize the global RoomList to store all of our objects one of two ways.

level1.roomlist = CreateList() ' global variable roomlist accessed through level1

'or

Troom.roomlist = CreateList() ' global variable roomlilst access through Troom. note:
'.............................' there is no need to have an instance of troom to access globals with in types
'.............................' but can only be accessed through the corresponding type or an instance(object) of.

level1.roomlist.addlast(level1)' this (adds/stores) the address of level1(object) to global roomlist

' now here comes the fun part.

level2:Troom=New Troom ' creates an object of type Troom with name level2 

level1.roomlist.addlast(level2) '(adds/stores) the address of level2 (object) to roomlist
level2.roomlist.addlast(level2) ' the previous line, this line, and the next line
Troom.roomlist.addlast(level2)  ' all do the same thing. they store the address 
'.............................. ' of the same Object three times in the Global
'.............................. ' roomlist. You can use either of the three examples.
'.............................. ' they store level2 address to roomlist. of course if you 
'.............................. ' use all three you will have the addres of the same
'.............................. ' object stored in roomlist three times.

' and add another object

level3:troom = New troom 

troom.roomlist.addlast(level3) ' this one will add level3 to global roomlist                                        

'at this point there are there addresses stored in roomlist: leve1, level2 level3 
'you can access items in each object directly this way:
level1.w = 640
level1.h = 480
level2.w = 640
level2.h = 480
level3.w = 640
level3.h = 480

Print level1.h
Print level1.w
Print level2.h
Print level2.w
Print level3.h
Print level3.w


' or you can access all of your objects sequentially in the order they were stored in roomlist
' like this:
For Local cells:Troom = EachIn Troom.roomlist ' you can also use level1.roomlist...
	cells.w = 640 ' this values will be stored in level1, level2 and level3.
	cells.h = 480
Next  

For Local s:Troom = EachIn level2.RoomList ' this will display all three objects level1, level2 and level3
    Print s.h
    Print s.w
Next

'this displays the count of object adresses stored in roomlist 

Print Troom.RoomList.count() 'this will all display the number 3
' or
Print level1.RoomList.count()
' or 
Print level2.RoomList.count()
' or
Print level3.RoomList.count()

'You don't have to create names for each object but
' you will not have direct access to them either. They can be 
'stored directly to RoomList:
Troom.roomlist = CreateList()
Troom.RoomList.addlast(New Troom)'this creates the first troom or level1
Troom.RoomList.addlast(New Troom)'this creates the second troom or level2
Troom.roomlist.addlast(New Troom)'this creates the third troom or level3

'To access them just use the For statements with eachin as above


note: this may not be the best way of doing things and is just one way of doing it. I really hope this helps you.