Object Problem

Blitz3D Forums/Blitz3D Programming/Object Problem

wizzlefish(Posted 2004) [#1]
I've been trying to use "Object" to sort things out, but it isn't working.

What I've done is I've made a function called "InitLevel." This functions sets up the level and has no errors. But when "InitLevel" calls the function "InitLevelGuns," I recive an error "Entity does not exist."

I know the problem, but I just don't know how to fix it because this is the first time I've ever needed to use "Object."

I named all my guns using "EntityName," but I don't know how to utilize this. Please help.

This is the "InitLevelGuns" function:
Function InitLevelGuns()

	;pistol (gun1)
	pistol.weapon = New weapon
	;lazer (gun2)
	lazer.weapon = New weapon
	;dartgun (gun3)
	dartgun.weapon = New weapon
	;XM1014 (gun4)
	rifle.weapon = New weapon
	;HK-MP5
	mp5.weapon = New weapon
	;SAW
	saw.weapon = New weapon
	
	pistol\category = "C90-KL"
	pistol\index = 1
	pistol\offsetx = .60
	pistol\offsety = -.60
	pistol\offsetz = .9
	pistol\entityhandle = LoadMesh("weapons\pistol1\pistol1.x",camera)
	pistol\gotit = False
	pistol\ammo = 16
	pistol\rounds = 10
	pistol\damage = 7
	NameEntity pistol\entityhandle, "pistol"
	
	lazer\category = "LAS-74"
	lazer\index = 2
	lazer\offsetx = .95
	lazer\offsety = -.63
	lazer\offsetz = 1.3
	lazer\entityhandle = LoadMesh("weapons\lazer\lazer.3ds", camera)
	lazer\gotit = False
	lazer\ammo = 100
	lazer\rounds = 50
	lazer\damage = 10
	NameEntity lazer\entityhandle, "lazer"
	
	dartgun\category = "D4-S"dartgun\index = 3
	dartgun\offsetx = .8
	dartgun\offsety = .64
	dartgun\offsetz = 1.8
	dartgun\entityhandle = LoadMesh("weapons\dartgun\dartgun.x", camera)
	dartgun\gotit = False
	dartgun\ammo = 6
	dartgun\rounds = 5
	dartgun\damage = 7
	NameEntity dartgun\entityhandle, "dartgun"
	
	rifle\category = "Benneli XM1014"
	rifle\index = 4
	rifle\offsetx = .60
	rifle\offsety = -.4
	rifle\offsetz = .8
	rifle\entityhandle = LoadMesh("weapons\xm\w_xm1014.3ds", camera)
	rifle\gotit = False
	rifle\ammo = 12
	rifle\rounds = 16
	rifle\damage = 13
	NameEntity rifle\entityhandle, "rifle"
	
	mp5\category = "HK-MP5"
	mp5\index = 5
	mp5\offsetx = .6
	mp5\offsety = -.5
	mp5\offsetz = .6
	mp5\entityhandle = LoadMesh("weapons\raven\w_mp5.3ds", camera)
	mp5\gotit = False
	mp5\ammo = 80
	mp5\rounds = 12
	mp5\damage = 10
	NameEntity mp5\entityhandle, "MP5"
		
	saw\category = "SAW M-2492
	saw\index = 6
	saw\offsetx = .95
	saw\offsety = -.84
	saw\offsetz = 1
	saw\entityhandle = LoadMesh("weapons\saw\saw.3ds", camera)
	saw\gotit = False	
	saw\ammo = 80
	saw\rounds = 24
	saw\damage = 15
	NameEntity saw\entityhandle, "SAW"
	
	;setup
	ScaleEntity pistol\entityhandle, 1, 1, 1
	ScaleEntity lazer\entityhandle, .08, .08, .08
	ScaleEntity rifle\entityhandle, .027, .027, .027
	ScaleEntity mp5\entityhandle, .05, .05, .05
	ScaleEntity saw\entityhandle, .1, .1, .1
	
	TranslateEntity pistol\entityhandle, pistol\offsetx, pistol\offsety, pistol\offsetz
	TranslateEntity lazer\entityhandle, lazer\offsetx, lazer\offsety, lazer\offsetz
	TranslateEntity dartgun\entityhandle, dartgun\offsetx, dartgun\offsety, dartgun\offsetz
	TranslateEntity rifle\entityhandle, rifle\offsetx, rifle\offsety, rifle\offsetz
	TranslateEntity mp5\entityhandle, mp5\offsetx, mp5\offsety, mp5\offsetz
	TranslateEntity saw\entityhandle, saw\offsetx, saw\offsety, saw\offsetz
	
	RotateEntity rifle\entityhandle, 90, 90, 0
	RotateEntity mp5\entityhandle, 90, 90, 0
	RotateEntity saw\entityhandle, 90, 90, 0

	HideEntity rifle\entityhandle
	HideEntity dartgun\entityhandle
	HideEntity lazer\entityhandle
	HideEntity mp5\entityhandle
	HideEntity saw\entityhandle
	
	;hide weapons that we don't have yet
	For w.weapon = Each weapon
		
		If w\gotit = False
			HideEntity w\entityhandle
		EndIf	
	
	Next
	
End Function


Here is "UpdateGun," where the problem is:
Function UpdateGun()
	
	If KeyHit(15)
		gun = gun + 1
		If gun >= 7
			gun = 1
		EndIf
	EndIf
	
	If gun = 1
	;	If pistol\gotit = True
			HideEntity pistol\entityhandle ;<--convert this to "Object"
			HideEntity rifle\entityhandle
			HideEntity mp5\entityhandle
			HideEntity dartgun\entityhandle
			HideEntity saw\entityhandle
			ShowEntity pistol\entityhandle
	;	EndIf
	ElseIf gun = 2
	;	If lazer\gotit = True
			HideEntity pistol\entityhandle
			HideEntity rifle\entityhandle
			HideEntity mp5\entityhandle
			HideEntity dartgun\entityhandle
			HideEntity saw\entityhandle
			ShowEntity lazer\entityhandle
	;	EndIf
	ElseIf gun = 3
	;	If dartgun\gotit = True
			HideEntity lazer\entityhandle
			HideEntity rifle\entityhandle
			HideEntity mp5\entityhandle
			HideEntity pistol\entityhandle
			HideEntity saw\entityhandle
			ShowEntity dartgun\entityhandle
	;	EndIf
	ElseIf gun = 4
	;	If rifle\gotit = True
			HideEntity lazer\entityhandle
			HideEntity pistol\entityhandle
			HideEntity mp5\entityhandle
			HideEntity dartgun\entityhandle
			HideEntity saw\entityhandle
			ShowEntity rifle\entityhandle
	;	EndIf
	ElseIf gun = 5
	;	If mp5\gotit = True 
			HideEntity lazer\entityhandle
			HideEntity rifle\entityhandle
			HideEntity pistol\entityhandle
			HideEntity dartgun\entityhandle
			HideEntity saw\entityhandle
			ShowEntity mp5\entityhandle
	;	EndIf
	ElseIf gun = 6
	;	If saw\gotit = True
			HideEntity lazer\entityhandle
			HideEntity rifle\entityhandle
			HideEntity pistol\entityhandle
			HideEntity dartgun\entityhandle
			HideEntity mp5\entityhandle
			ShowEntity saw\entityhandle
	;	EndIf
	EndIf
	
	;Shoot
	;-----
	If gun=1
	If ammo >= 1
	If MouseHit(1)
		PlaySound shoot
		ammo = ammo - 1
		picked=CameraPick(camera, 400, 300)
		p.hole = New hole
		p\entityhandle = CopyEntity(bullethole)
		p\alpha = 0.9
		p\typething = "pistol"
		PositionEntity p\entityhandle, PickedX(), PickedY(), PickedZ()
		CreateSomeParticles(PickedX(), PickedY(), PickedZ(), 100, 100, 100, 6)
	EndIf
	EndIf
	EndIf
	
	If gun=2
	If ammo >= 1
	If MouseHit(1)
		PlaySound shoot
		ammo = ammo - 1
		picked=CameraPick(camera, 400, 285)
		For i = 1 To 4
		l.laserhole = New laserhole
		l\entityhandle = CopyEntity(laserspark)
		l\alpha = 0.9
		l\typething = "laser"
		PositionEntity l\entityhandle, PickedX(), PickedY(), PickedZ()
		Next
	EndIf
	EndIf
	EndIf
	
	If gun=4
	If ammo >= 1
	If MouseHit(1)
		PlaySound shoot
		ammo = ammo - 1
		For i = 1 To 5
			picked=CameraPick(camera, Rnd(390,410), Rnd(275, 295))
			s.hole = New hole
			s\entityhandle = CopyEntity(bullethole)
			s\alpha = 0.9
			s\typething = "shotgun"
			PositionEntity s\entityhandle, PickedX(), PickedY(), PickedZ()
			CreateSomeParticles(PickedX(), PickedY(), PickedZ(), 100, 100, 100, 6)
		Next
	EndIf
	EndIf
	EndIf
	
	If gun=5
	If ammo >= 1
	If MouseDown(1)
	If MilliSecs() > mp5timer + 160
		PlaySound shoot
		ammo = ammo - 1
		picked=CameraPick(camera, Rnd(390,410), Rnd(275, 295))
		m.hole = New hole
		m\entityhandle = CopyEntity(bullethole)
		m\alpha = 0.9
		m\typething = "mp5"
		PositionEntity m\entityhandle, PickedX(), PickedY(), PickedZ()
		CreateSomeParticles(PickedX(), PickedY(), PickedZ(), 100, 100, 100, 6)
		mp5timer = MilliSecs()
	EndIf 
	EndIf
	EndIf
	EndIf
	
	If gun=6
	If ammo >= 1
	If MouseDown(1)
	If MilliSecs() > sawtimer + 84
		PlaySound shoot
		ammo = ammo - 1
		picked=CameraPick(camera, Rnd(390,410), Rnd(275, 295))
		w.hole = New hole
		w\entityhandle = CopyEntity(bullethole)
		w\alpha = 0.9
		w\typething = "saw"
		PositionEntity w\entityhandle, PickedX(), PickedY(), PickedZ()
		CreateSomeParticles(PickedX(), PickedY(), PickedZ(), 100, 100, 100, 6)
		mp5timer = MilliSecs()
	EndIf 
	EndIf
	EndIf
	EndIf
	
	For b.hole = Each hole
		EntityAlpha b\entityhandle, b\alpha
		b\alpha = b\alpha-0.02
		If b\alpha <= 0
		FreeEntity b\entityhandle
		Delete b
		EndIf
	Next
	
	For k.laserhole = Each laserhole
		EntityAlpha k\entityhandle, k\alpha
		k\alpha = k\alpha-.1
		If k\alpha <= 0
		FreeEntity k\entityhandle
		Delete k
		EndIf
	Next 
	

End Function 


The problem is where you switch guns - where all the "HideEntity"'s and "ShowEntity"'s are.

I need to convert those Hide and ShowEntities to Object, but I don't know how.

(Sorry this is such a long post - I hope I've made it thorough enough :P)


wizzlefish(Posted 2004) [#2]
I DARE you to try and figure it out.


wizzlefish(Posted 2004) [#3]
OK, people - I really need this answer.

I can't move along until I have the answer.

Does ANYONE know what OBJECT does?!?!?!?!


Ethan3850(Posted 2004) [#4]
Object? You use Object() to convert a Handle(), errh. The Handle() is an integer value that points to a Type structures' memory address and you use Object() to get the pointer back and access the Type structure - that probably doesn't make a lot of sense.

Your problem is that your loading a mesh that doesn't exist, or you've forget to Global one of the variables that points to your entity.


Ezbe(Posted 2004) [#5]
Get the handle of type:
TypeHandle% = Handle(Type.Type)


Get the type by handle:

Type.Type = Object.Type(TypeHandle%)


Someone gave a good tip using NameEntity with Handle(), when creating type which contains entity.

Simple example:


;Create an enemy and return the type
Function CreateEnemy.Enemy(Mesh)
  E.Enemy = New Enemy
  E\Entity = CopyEntity(Mesh)
  NameEntity E\Entity, Handle(E.Enemy)
  Return Enemy.Enemy


Now, instead of looping through the whole typelist searching for a certain entity (like one which got collisioned with), you just get the typehandle from the entityname and use Object to get the type:

;Find the enemy type with the entity
Function FindEnemy.Enemy(Ent%)
   Local TypeHandle% = EntityName(Ent%)
   Enemy.Enemy = Object.Enemy(TypeHandle%)
   If Enemy<>Null Return Enemy.Enemy   ;Or just Return Enemy.Enemy, remember to check for null-type
End Function



Ethan3850(Posted 2004) [#6]
You might find this discussion useful. Ezbe's pretty much said what's there now though.


Rambus(Posted 2004) [#7]
Just a few pointers,
Try using a select case structure rather then

If gun = 1
end if
elseif gun = 2
end if
elseif gun = 3
end if

vs

select gun
	case 1
		break;
	Case 2
		Break;
End select


Also; this might help make your code more readable.
Instead of...

If gun=4
If ammo >= 1
If MouseHit(1)

do...
If gun=4 and ammo >=1 and mousehit(1)


I doubt you wanted these pointers, but it's to late to take them back!


wizzlefish(Posted 2004) [#8]
Well, don't you name an instance of a type (like when I did "NameEntity pistol\entityhandle, "pistol") and then call upon it later using Object?


wizzlefish(Posted 2004) [#9]
And what about my original question - how would I modify my code to make it "work"


wizzlefish(Posted 2004) [#10]
Come on, guys, I really need you now. Please!

@G.0.D - why wouldn't I want the pointers?


WolRon(Posted 2004) [#11]
Sainfohi already stated what the problem is:

Your problem is that your loading a mesh that doesn't exist, or you've forget to Global one of the variables that pointers to yur entity.


You stated:
But when "InitLevel" calls the function "InitLevelGuns," I recive an error "Entity does not exist."

Well, if that's your error message then one of your entities is not being created. I don't see how this has anything to do with the Object command.

Do yourself a favor and use the Stop command (during Debug mode) and then step through your code line by line to see which entity doesn't exist.

Once you find out which one doesn't exist, then go back and figure out why.


WolRon(Posted 2004) [#12]
By the way, you are giving your weapons ammo
	rifle\ammo = 12

but you aren't even checking their respective ammo counters
	If ammo >= 1
	If MouseHit(1)
		PlaySound shoot
		ammo = ammo - 1

You need to check each ones individual counter
	If rifle\ammo >= 1
	If MouseHit(1)
		PlaySound shoot
		rifle\ammo = rifle\ammo - 1


unless your intent was to pass the rifle ammo count to the function in which case 'ammo' would have to be Global(I can't tell if it's global by the code you posted), or you would need a local 'ammo' variable like this:
Function UpdateGun(ammo)
  ;function code here
End Function
and call it like this:
UpdateGun(rifle\ammo)



Jeppe Nielsen(Posted 2004) [#13]
I think, you need to make your pointers to the weapon-type variables global, put this in the top of your code:

	Global pistol.weapon
	Global lazer.weapon
	Global dartgun.weapon
	Global rifle.weapon
	Global mp5.weapon
	Global saw.weapon



wizzlefish(Posted 2004) [#14]
Let's try that!


wizzlefish(Posted 2004) [#15]
Thanks, WolRon - didn't catch that.


wizzlefish(Posted 2004) [#16]
Jeppe - it worked! :)