Access part of a type that's in an array.

BlitzMax Forums/BlitzMax Beginners Area/Access part of a type that's in an array.

Matt Vinyl(Posted 2009) [#1]
Hi all,

Type typ_lamp
	Global x:Int=0 'lamp x position on the screen
	Global y:Int=0 'lamp y position on the screen
	Global lamp:Int=0 'lamp item identifier
	Global state:Byte=0 'lamp on/off state
	Global alpha:Byte=0 'lamp alpha level
	Global alphadir:Byte=0 'lamp alpha direction (fade in / fade out)
End Type


arr_lamps[1].x=232
arr_lamps[1].y=602
arr_lamps[1].lamp=37
arr_lamps[2].x=601
arr_lamps[2].y=1154
arr_lamps[2].lamp=16


	For Local a:Int=1 To 2
	DrawImage gfx_lamps,arr_lamps[a].x,arr_lamps[a].y,arr_lamps[a].lamp
	Next


Why won't this correctly draw my 2 seperate images at the desired co-ordinates on screen? (I've cut out the peripheral code which doesn't affect this, of course).

I have over 150 items of this type that will eventually be in this array, but want to get it right before I arduously populate the objects. ;)


Brucey(Posted 2009) [#2]
that looks fine. What's the problem exactly?


Matt Vinyl(Posted 2009) [#3]
Bizarrely, it seems to only draw one of the lamps (alpha'd png images') and not the second. Then, if I change some of the values in my array assignment code (the second box in my OP), it 'hangs on' to the previously stated images? Here's the rest of my code so far for completeness - in a very 'test' state at the moment. (Thanks for looking!) :)

'---Heaven & Hell (Main)
'---28 January 2009
'---Version 0.01

Strict

Graphics 1024,768',32,60 remove the hyphen to enable fullscreen mode

SetBlend ALPHABLEND

Global gfx_screen:TImage=LoadImage("main.png") 'main machine glass
SetImageHandle gfx_screen,0,0

Global gfx_reela:TImage=LoadImage("reela.png") 'reel a image
SetImageHandle gfx_reela,0,0

Global gfx_reelb:TImage=LoadImage("reelb.png") 'reel b image
SetImageHandle gfx_reelb,0,0

Global gfx_reelc:TImage=LoadImage("reelc.png") 'reel c image
SetImageHandle gfx_reelc,0,0

Global gfx_lamps:TImage=LoadAnimImage("lamps.png",200,128,0,200) 'lamp images
SetImageHandle gfx_lamps,100,64

Global var_state:Int=0
'var_state values
'0=initialise
'1=introduction screen
'2=attract mode active
'3=play mode active

Global var_mainx:Int=0 'main x offset origin
Global var_mainy:Int=0 'main y offset origin

Type typ_lamp
	Global x:Int=0 'lamp x position on the screen
	Global y:Int=0 'lamp y position on the screen
	Global lamp:Int=0 'lamp item identifier
	Global state:Byte=0 'lamp on/off state
	Global alpha:Byte=0 'lamp alpha level
	Global alphadir:Byte=0 'lamp alpha direction (fade in / fade out)
End Type

Global arr_lamps:typ_lamp[2]

'populate lamp array with lamp information
arr_lamps[1].x=232
arr_lamps[1].y=602
arr_lamps[1].lamp=37
arr_lamps[2].x=601
arr_lamps[2].y=1154
arr_lamps[2].lamp=16


'--------------------------------------------------

Repeat
	Cls
	SetAlpha 1
	fnc_reels()
	SetViewport 0,0,1024,768
	fnc_screen()
	Flip
Until AppTerminate() Or KeyHit(KEY_ESCAPE)

'--------------------------------------------------

Function fnc_reels()
	SetViewport 342,1237+var_mainy,97,250
	TileImage gfx_reela,342,1237
	SetViewport 463,1237+var_mainy,97,250
	TileImage gfx_reelb,463,1237
	SetViewport 587,1237+var_mainy,97,250
	TileImage gfx_reelc,587,1237
End Function

Function fnc_screen()	
	If KeyDown(KEY_DOWN) And var_mainy>-768 Then var_mainy:-6
	If KeyDown(KEY_UP) And var_mainy<0 Then var_mainy:+6
	SetOrigin var_mainx,var_mainy
	DrawImage gfx_screen,0,0
	For Local a:Int=1 To 2
	DrawImage gfx_lamps,arr_lamps[a].x,arr_lamps[a].y,arr_lamps[a].lamp
	Next
End Function


Edit: It still only draws the 'latest' item in the array if I change the drawing routine to...

	DrawImage gfx_lamps,arr_lamps[1].x,arr_lamps[1].y,arr_lamps[1].lamp
	DrawImage gfx_lamps,arr_lamps[2].x,arr_lamps[2].y,arr_lamps[2].lamp



Matt Vinyl(Posted 2009) [#4]
Think I've got it - I've not used 'New' to make the actual types. Let's see if that works... ;)


BladeRunner(Posted 2009) [#5]
All fields of your type are global, so you can only have one instance ;)


Jesse(Posted 2009) [#6]
yes change the globals in the type to fields


Matt Vinyl(Posted 2009) [#7]
Legends! How could I make such a basic error...? Although I do get a "Unhandled Memory Exception Error" now, probably not numbered something correctly.

Oh well, excellent stuff! :)


Zeke(Posted 2009) [#8]
remember arrays first element is 0 not 1

global array:mytype[2]

then use

array[0] and array[1]


Matt Vinyl(Posted 2009) [#9]
Thanks for that - it was indeed the problem, along with me not doing a loop to create all the 'New' objects. Working splendidly now. Cheers for everyones' help! :)