objects query

BlitzMax Forums/BlitzMax Beginners Area/objects query

Cruis.In(Posted 2006) [#1]
I have create an instance of an object from the same type, does the new instance still use the same x, y coords as the previous instance?

I have a ship based on tplayership. ship:tplayership
targetship:tplayership

the type tplayership has fields x and y

is targetship.x always going to be equal to ship.x?
in all my tests this is true, wondering if im doing something wrong.

How can I then create instances from the same type and have there coords differ? do i have to overwride there x and y field value?


REDi(Posted 2006) [#2]
No, each instance has its own set of values (unless you made them values global)




Cruis.In(Posted 2006) [#3]
unless I made the instance global? Or the value global?
I have a global of the instances in my globals file.

I have a type tplayership.

I have instanced ship:tplayer
and targetship:tplayership

i have a function to draw the targetship, which i call in the main loop.

'--------------------
'draw a ship anywhere
'--------------------
Function DrawShip( image:timage, x,  y )
	SetRotation(0)
	SetColor 255,255,255	
	SetScale(1.0 *newscale, 1.0*newscale)
	SetAlpha(1.0)
	SetBlend(ALPHABLEND)
	MidHandleImage (image)

	Local targetship:tplayership = New tplayership
		
	'draws the ship if the shiplist is empty. Shiplist gets a ship when I press T
	If ListIsEmpty(ship.shiplist) = False
		DrawImage(image, x , y )
		DrawText("target shieldpower= "+targetship.shieldpower,100,100)
	End If
	
	'adds object to shiplist, other code is just part of my testing
	If KeyHit(KEY_T)
		ListAddLast(ship.shiplist, targetship)
		targetship.x = x
		targetship.y = y
		targetship.shieldpower = 200
	End If
End Function


now i have my collision in the imagescollide function set to targetship.x targetship.y

but it isn't working. The X and Y value of the instance targetship is staying = to X and Y of the "ship" instance. the ship instance here is my ship by the way at the center of the screen. so needless to say my weapons explode as i fire them.

I cant get the targetship.x or y to change to its own values.... shouldnt each instance, no matter if its created from the same type, have its own x and y value.

the x and y are in fields in tplayership.

in my loop i call drawship(imageIwant, 400,400) to create the ship at x and y 400. but i have a drawtext of targetship.X and Y on the screen and it doesn't read 400,400 it reads and creates the target ship at my X and Y in the center of the screen. even though it draws the graphic of the ship in the correct place, isn't that weird? My function draws it correctly to 400,400
yet the targetship.x, and Y aren't correct.

or is that only correct because drawimage(,image,x,y) in the function works, and thats why at least the image is being drawn correct.


tonyg(Posted 2006) [#4]
Each 'new' instance creates 'new' variables to store 'X' and 'Y' values if you have defined them in your type definition.
If the same values are being used for each instance then you are doing something wrong.
How about a simple test where you define a type, create 2 instances and, using whatever method you prefer, set their
x/y values to be something different.
If you still have a problem then post that simple test code so we know what you're doing.

If you are using RND then don't forget to seedrnd.


Cruis.In(Posted 2006) [#5]
is it bad to make instances of your types global? Like my player ship and the target ship? Could that cause confusion between the variables belonging to each instance? or no?


Cruis.In(Posted 2006) [#6]
I think I found the problem. my tplayership type has two fields, x and y. field x:float, field y:float

when i instance ship:tplayership
and targetship:tplayership

they both instance at those default x and y because they would be 0, since i havent specified a number in their fields. i tested this by putting the x and y fields to 512, 384... and the drawtext coords are in my debug function and it read the same coords for both playershipX and targetshipX. and the Y values as well.

i thought that my function which has image,x,y in it when i called it would make the difference when i called it by changing targetship.x = x and the same for Y.

where x and Y in this function, are the values i give to the function parameters when i call it. the drawship function above. I moved the targetship.x = x out of the If
Keyhit T statement and put it so it sets those values when the function is called. so now that works.

edit
I am doing this to teach myself and understand better the use of lists. Collision detection tests. so that i can put combat in the game. so i was trying to make an enemyship which initiazlized with its own shield and hull, so thats the way i did, to test. I'm sure once ihave the full grasp I can add combat properly

Plus I thought I had types down. I dont want to create a type for every ship in the game...

this function works now with the collision function
Function DrawShip( image:timage, x,  y )
	SetRotation(0)
	SetColor 255,255,255	
	SetScale(1.0 *newscale, 1.0*newscale)
	SetAlpha(1.0)
	SetBlend(ALPHABLEND)
	MidHandleImage (image)

	targetship.x = x
	targetship.y = y
					
	'draws the ship if the shiplist is empty. Shiplist gets a ship when I press T
	If ListIsEmpty(targetship.shiplist) = False
		DrawImage(image, x , y )
		DrawText("target shieldpower= "+targetship.shieldpower,ship.x-512,ship.y+150)
		DrawText("target Hull= "+targetship.hull_strength,ship.x-512,ship.y+170)
	End If
	
	'adds object to shiplist, other code is just part of my testing
	If KeyHit(KEY_T)
		targetship.shiplink = ListAddLast(targetship.shiplist, targetship)
		targetship.shieldpower = 200
	End If
End Function



HERE IS THE COLLISION FUNCTION

Function Collision()
	Local torpedo:ttorpedo = New ttorpedo
	For torpedo = EachIn torpedo.torplist
	
	'if the list is empty, the torpedo will pass through where targetship.x and and targetship.y
	'will be. Because it is just a "bot" and won't move...Otherwise my torps would explode
	'even when the ship graphic is gone"
	
	'if the list is empty, then the torpedoes just go on happily.
	If ListIsEmpty(targetship.shiplist) = False
		If ImagesCollide(torpedo.photonimage, torpedo.x, torpedo.y,torpedo.frame,targetship.shipimage,targetship.x,targetship.y,0)
			If targetship.shields = False
				targetship.hull_strength:-5
			Else 
				targetship.shieldpower :- 5
			End If 
			
			'handle shields
			If targetship.shieldpower < 5
				targetship.shields = False
				targetship.shieldpower = 0
			End If
			
			'basic destroy code
			If targetship.hull_strength < 5
				RemoveLink targetship.shiplink
			End If 
			
			'stupid box i draw so i can see when it flashes when a collision occurs.
			'it flashes on and off screen too fast, with just text.
			DrawText "COLLISION!",50,50
			DrawRect 50,50,50,50
			PlaySound(torpedoexplosionFx)
			RemoveLink torpedo.link
	End If
	End If  
	Next 
End Function


I guess you guys understand why im doing this weird code... just trying to understand things before i go off coding without understanding how everything works with regards to these things.