EachIn

BlitzMax Forums/BlitzMax Beginners Area/EachIn

Banshee(Posted 2005) [#1]
I've been trying to find a tutorial on this because I thought it must have been asked a lot, I got to this stage within a few minutes of downloading the Max demo - and several hours later i'm still at this stage...

The problem is the new lists feature, I understand the concept: Several lists of the same data type. I just cannot figure out the syntax and reading the manual and searching for other users problems hasn't cleared this up for me.

In this code I have a data type called unit, and I have a list called shipList, but these are not linked in any way.

I know 10 of the unit data type are created, but I cannot iterate through them in the mainLoop with eachIn because that is indexed via the TList and not the TType.

I'm baffled as to the syntax required to resolve this, help please :)

Strict

Graphics 640,480,0
Global AppTitle$="AAAAAAARRRRRRRRGGGGGGHHHHHHHHH WTF Lists..."

Global backdrop=LoadImage ("Backdrop\backdrop1.jpg")


Type unit
	Field o,ai
	Field x#,z#,f#
End Type
Global shipList:TList=CreateList()




createPlayers(10)
'function MainLoop
	Local ship:unit

	While Not KeyDown(KEY_ESCAPE)
		For ship=EachIn shipList
			DebugLog("SHIP LOOP ACTIVE")
			Select ship.ai
				Case 0
					control(ship)
					camera(ship)
				Case 1
					ai(ship)
				Default
			End Select
		Next
		For ship=EachIn shipList
			moveShip(ship)
		Next	
	
		Flip
		Cls
	Wend
	End
'end function



Function LIVE_SHIPS_____________________________________________________________________________________________()
End Function
Function control(ship:unit)
End Function
Function ai(ship:unit)
End Function
Function moveShip(ship:unit)
End Function



Function LIVE_GENERAL_PURPOSE__________________________________________________________________________________()
End Function
Function camera(ship:unit)
	'Centres display on Ship
	SetScale 5,5
	DrawImage backdrop,-(ship.x + (GraphicsWidth()/2)) , -(ship.z + (GraphicsHeight()/2))
	SetScale 1,1
End Function




Function SETUP_________________________________________________________________________________________________()
End Function
Function createPlayers(qty=10)
	For Local n=1 To 10
		Local ship:unit=New unit
		ship.o=LoadImage("Ship\7.png")
		If n>1 Then ship.ai=1 'Make First Ship the Human Player

		ship.x=Rnd(0)
		ship.z=Rnd(0)
	Next
End Function



Banshee(Posted 2005) [#2]
I found it in an unrelated code snippet :) YAY

Function createPlayers(qty=10)
	For Local n=1 To 10
		Local ship:unit=New unit
		ship.o=LoadImage("Ship\7.png")
		If n>1 Then ship.ai=1 'Make First Ship the Human Player

		ship.x=Rnd(0)
		ship.z=Rnd(0)
		ListAddLast shipList,ship
	Next
End Function


I'd tried ListAddLast before, but failed to understand how to use it properly (after creating the variable locally with New).


Rimmsy(Posted 2005) [#3]
Several lists of the same data type.
I'm not sure you know but I thought I'd point out that a list can contain any object not just objects of the same type. so:
local g:Tlist=createlist()

local o:myObject=new myObject
local o1:another = new another

listaddlast(g,"hello")
listaddlast(g,o)
listaddlast(g,o1)

for local i:object=eachin g
   if string(i)<>null
      print "it's a string "+string(i)
  elseif myObject(i)<>null
      print "it's a myObject"
  elseif another(i)<>null
      print "it's another."
  endif
next

Hope you get the hang of them, they're handy.


Rimmsy(Posted 2005) [#4]
Also, using inheritence makes it a lot cleaner to update what i think you're trying to update:

Global shipList:Tlist=CreateList()

Type baseUnit
	Field name$
	Field x#,y#,f#
	
	' whenever you make a new object, this method will get called
	' add to the shipList so we don't have to later on.
	'Method New() 
	'	ListAddLast(shipList,Self) 
	'End Method
	
	Method update()
		f:+1 
	End Method
End Type

Type playerUnit Extends baseUnit
	Method update()
		super.update() ' will perform what baseUnit.update() does
		' update keypresses, etc for player
	End Method
End Type

Type AIUnit Extends baseUnit
	Method update()
		super.update() ' will perform what baseUnit.update() does
		
		'do IA code here
	End Method
End Type

Function updateShips()
	For Local i:baseUnit=EachIn shipList
		i.update()
	Next
End Function


' --------------------------------
Local i:playerUnit=New playerUnit
	i.name="player"
	ListAddLast(shipList,i)
	
Local j:AIunit=New AIUnit
	j.name="AI 1"
	ListAddLast(shipList,j)	


While KeyHit(KEY_ESCAPE)=0
Cls

	updateShips()

Flip
Wend
End



Banshee(Posted 2005) [#5]
Thank you. I understand the OO stuff to the "Get the theory, never done it in practice" level - so I figured i'd write this in a very B3D way first to ease into BMax slowly. My snippet is very much the way I structure my B3D stuff.

I'll start using methods pretty soon I think (i've been dying too be able to do that in ages), I already do inheritence of a sort in B3D (type within a type) but dont find it is generally that much use in the simplistic stuff I write.


Rimmsy(Posted 2005) [#6]
I thought you might already understand it and only replied on the off chance you didn't. I love the inheritence part of bmx. After a while you'll find you won't be able to go back to b3d. Not that you can't, but you just won't be able to bring yourself to be constrained without the things you have in bmx. Not that b3d is bad, I love it, but after using bmx exclusively for so long I can't imagine using anything else. [/bmx loving]


Banshee(Posted 2005) [#7]
I'm writting one game, I don't want to abandon my 3D project. It's just that I start a new job on Monday and they gave me a Mac today to brush up. I'm going to go in on Monday morning with a simple arcade game all finished & polished off...

I just wont tell them I wrote it on the PC :)


Rimmsy(Posted 2005) [#8]
Sweet! Good luck with that Becks. Be sure to share the outcome with us. I'm curious like an orange.


Banshee(Posted 2005) [#9]
You do realise when I say polished, more than anything else I mean simple...

It's meant to impress a bunch of Mac-philles who don't know what a joystick is - if you want it to impress a bunch of coders ... that'll take longer :)




Rimmsy(Posted 2005) [#10]
Mmmmmm, space-battle-esque laser fire. What are you doing to those poor jelly babies anyway?


Banshee(Posted 2005) [#11]
Setting fire to them it seems


fps note: 250 ships in arena


xlsior(Posted 2005) [#12]
Looking nice!


Banshee(Posted 2005) [#13]
It's looking nearly finished now, I just need to put a front end on it and some kind of HUD to show the battle status, but as i've only got the BMax demo until after xmas I doubt i'll be able to release it until i've had a pay packet or two.


Banshee(Posted 2005) [#14]



Rimmsy(Posted 2005) [#15]
I'd be interested to see the source because I'm doing something similar but I *hate* my AI combat code. it's just crud. Looks like you've done a lot in a short space of time. Looks good.


Banshee(Posted 2005) [#16]
B3D version Source: http://www.blitzbasic.com/codearcs/codearcs.php?code=1034
Game: http://spacecorps.bansheestudios.com/

Pretty much the same thing