Types.

Blitz3D Forums/Blitz3D Beginners Area/Types.

Aussie(Posted 2014) [#1]
A little confused at the moment, i have worked with types plenty of times but am having some problems & can't see how to get around it.
Basically i am parsing a .txt file & using the information to sort everything into types. I have no problems in loading everything into the scene.

So here is how i am organizing the types:
		                Case "Player"
					W\Mesh = CreateCamera()
					W\ID = ID$
					
					PositionEntity W\Mesh, W\L_X, W\L_Y, W\L_Z

				
				Case "Door"
					W\Mesh = LoadMesh("Door.b3d")
					W\ID = ID$
					W\State = State
					PositionEntity W\Mesh, W\L_X, W\L_Y, W\L_Z
					ScaleEntity W\Mesh, W\S_X, W\S_Y, W\S_Z
					RotateEntity W\Mesh, W\R_X, W\R_Y, W\R_Z
					
				Case "Corridor"
					W\Mesh = LoadMesh("Corridor_Baked1.b3d")
					
					PositionEntity W\Mesh, W\L_X, W\L_Y, W\L_Z
					ScaleEntity W\Mesh, W\S_X, W\S_Y, W\S_Z
					RotateEntity W\Mesh, W\R_X, W\R_Y, W\R_Z
							
				Case "Switch_Base"
					W\Mesh = LoadAnimMesh("Switch_1.b3d")
						W\Button = FindChild(W\Mesh,"Button")
						EntityColor W\Button,255,0,0
						EntityFX W\Button,1
					W\ID = ID$
					W\State = State
					PositionEntity W\Mesh, W\L_X, W\L_Y, W\L_Z
					ScaleEntity W\Mesh, W\S_X, W\S_Y, W\S_Z
					RotateEntity W\Mesh, W\R_X, W\R_Y, W\R_Z
					
				End Select


Now here is what i am struggling with. I am able to create a player (camera) & control it.
Function Update_World_Objects()
	For W.Obj = Each Obj
		If W\ID = "Player"
			Player = W\Mesh
			If KeyDown(205) TurnEntity W\Mesh,0,-.5,0
			If KeyDown(203) TurnEntity W\Mesh,0,.5,0	
			If KeyDown(200) MoveEntity W\Mesh,0,0,.025
			If KeyDown(208) MoveEntity W\Mesh,0,0,-.025
		End If 
	Next
End Function

But how am i able to interact with the different types. Eg, i am trying at the moment to have the player come within 1 unit of a switch to activate it. If all object meshes are W\Mesh then entitydistance (W\mesh,W\mesh) < 1???? How would i define witch mesh i am checking. Hope this makes sense.


Wings(Posted 2014) [#2]
you must use entitytype if you want a mesh to collision detection or use camera pick on mesh.

or just assign a new field to the object.. like

type stuff
field guid_id ;Random numnber making each object unique
field objecttype ; 0= non static no collission ; 1 static ; 2 door
field mesh ;
field x,y,z ;
end type


Matty(Posted 2014) [#3]
You will need to loop through all the relevant objects of type "Obj" in a nested for loop structure at the very least...

Something like:

player = w\mesh
for w2.Obj = each Obj
    ; compare w2/mesh to player (or w/mesh) and check for collision
next



Aussie(Posted 2014) [#4]
Thanks Wings & Matty
I don't think i have explained myself very clearly.

Wings i am setting up different fields for different objects under an ID$ field,
field ID; Door, switch, switch door. All listed on the .txt i am parsing.
I know how to set collisions & I understand what Matty is saying but what it is i am struggling with is,
I am using Blender to construct environments with & placing objects & use a script to spit out all the info to a .txt file. I am then Parsing the info & organizing it all into a type W.Obj with different fields for different objects. I am just testing my loading code at the moment but, lets say i have a heap of enemies, doors, pickups, switches, etc, etc.... All of the objects are loaded & they have the same field (as in the W\mesh) Matty going by what you have said would i then have to create a new w1.Obj, w2.obj, w3,obj etc, etc for each object? If doing it that way then would i have to change the Loading function i use for each level of game where the number of objects in the scene change?
It just seems a long way to do it especially if i have a heap of enemies & have to have them interact with each other as well.

Here is the loading function & an example of the .txt file i am using. If you can recommend anything or see if i am doing something that could be done better any suggestions would be greatly appreciated.

Load_World Function


.Txt file



Matty(Posted 2014) [#5]
Hang on looking at your code in the first post it looks very ... wrong.

You are not creating a new "Obj" instance each time you create a mesh so you are causing a memory leak by simply replacing W\Mesh with the current mesh each time...and losing your reference to the existing one.

I think you need to look at the examples in the reference guide for types.

Types form lists of objects.

No you don't need w1/w2/w3 etc...the "w" is merely a reference to the particular type instance. When you loop through a set of types using "w" as the variable name at each iteration the "w" instance holds a specific type instance in the list.


Aussie(Posted 2014) [#6]
 param$ = ReadLine$(filein)

	If Left$(param$, 6) = "Object"
		W.Obj = New Obj
	End If

I would have thought that this line was creating the new type every time it finds the word "Object" or am i missing something with the creation of each type in the loading function?


No you don't need w1/w2/w3 etc...the "w" is merely a reference to the particular type instance. When you loop through a set of types using "w" as the variable name at each iteration the "w" instance holds a specific type instance in the list.



That's the part i am getting confused with. If i have a heap of objects loaded all with the "w" i know i use a "For Next" loop to search through them.
But lets say i have a few instances.

Type Obj
	Field Mesh, ID$
	Field X#, Y#, Z#
End Type

W.Obj = New Obj
     W\Mesh = Loadanimmesh(player_mesh)
     W\ID = Player
     W\X = #
     W\Y = #
     W\Z = #

W.Obj = New Obj
     W\Mesh = Loadanimmesh(enemy_mesh)
     W\ID = Enemy
     W\X = #
     W\Y = #
     W\Z = #

W.Obj = New Obj
     W\Mesh = Loadanimmesh(enemy_mesh)
     W\ID = Enemy
     W\X = #
     W\Y = #
     W\Z = #


I know i can search through the instances to find the ID but if i need the 2 instances (in the future cold be 100+) with the W\ID = Enemy (or whatever else) to interact with each other like with entitydistance W\mesh,W\mesh how would you go about it?


Matty(Posted 2014) [#7]
You use two for loops nested inside each other with the inner one referencing say w2 and the outer one just w...on phone so cant type code.


Aussie(Posted 2014) [#8]
Thanks Matty,
It this what you mean?
For W.Obj = Each Obj
	If W\ID = "Enemy"
		For W2.Obj = Each Obj
			If W2\ID = "Enemy"
			If EntityDistance(W\Mesh,W2\Mesh) < 1
		     ;Do whatever
		End If
	End If
	Next
Next



Matty(Posted 2014) [#9]
That is a good starting point but you may find it is not the most efficient way....and you need to avoid checking an entity against itself. But theres a lot of tricks of the trade which you will soon learn.


Aussie(Posted 2014) [#10]
Yeah i thought that this loop would check the entity against it's self & that i could have some problems with it. Just need to understand how a loop like this works then i can figure a out a way to prevent it.

Would this loop check for W\ID = "Enemy" for one instance of the type and then loop through every instance in the inner loop for W2\ID = "Enemy" then look for the next W\ID = "Enemy" & repeat checking all W2\ID = "Enemy"?

If so i was thinking something like this could work.
For W.Obj = Each Obj
	If W\ID = "Enemy"
             W\Checked = true

		For W2.Obj = Each Obj
			If W2\ID = "Enemy"
                             If W2\Checked = true
                                   Next
                             end if
                        If EntityDistance(W\Mesh,W2\Mesh) < 1
		             ;Do whatever
		   End If
	      End If
	Next
      W\Checked = false
Next


Just double checking, is there a memory leak in the Load_World function. I did think that i was creating a new type when it read the word "Object"

Thanks again for the help Matty, it is very much appreciated.


Matty(Posted 2014) [#11]
The simplest way to know if the W and W2 are pointing at the same instance of an object is to use the undocumented handle command.

if handle(W)=handle(W2) then

endif 



Yasha(Posted 2014) [#12]
Why do you need the handle?

If W = W2 Then ...


...is strictly equivalent, and the normal way to do it.


Matty(Posted 2014) [#13]
You are right Yasha....I just forgot.


Aussie(Posted 2014) [#14]
Thanks heaps Matty & Yasha.