Deleting procedurally generated cubes

Community Forums/General Help/Deleting procedurally generated cubes

Blitzplotter(Posted 2016) [#1]
here's some code I'm using to generate cubes and attempt to name using nameentity, the problem is that I can't seem to Delete and Entity using freeentity wuth the name I am assigning to it:


;create the named entity suffix

dsuffix$="D"+Str(x)+Str(y)+Str(z)  ;typically can create a named entity of B899
						
Color 255,255,255
						
cubelotsf=CreateCube()
						
NameEntity cubelotsf,(dsuffix$)
						
ScaleEntity cubelotsf,0.4,0.4,0.4
						
PositionEntity cubelotsf,x,y,z
						
EntityColor cubelotsf,0,0,0



Then I'm trying to 'remove a cube dependent on some logic using the following - I'm making sure the string associated with a cube using NameEntity is constructed as per my attempt at 'removing' it from the world:


FreeEntity("B899")



Is there something I need to do to remove procedurally generated cube entities?

This was another attempt that failed....:


Color 255,255,255
						
;cubelotsf=CreateCube()
dsuffix$=CreateCube()
						
NameEntity dsuffix$,(dsuffix$)
						
ScaleEntity dsuffix$,0.4,0.4,0.4
PositionEntity dsuffix$,x,y,z
						
EntityColor dsuffix$,0,0,0
						


I'm suspecting the handle of nameEntity "B899" is failing as the Entity cannot be moved prior to free-ing it using the "B899" string without a MAV occurring.

Although using:

  

MoveEntity dsuffix$,10,1,1



Does work, so I'm suspecting an array of cubes might be the way forward.... dsuffix$ is possibly like a pointer to where the entity is stored, and just because NameEntity has assigned a string to the entity does not mean the Entity can be manipulated by using the NameEntity command.


RemiD(Posted 2016) [#2]
You confuse the name of the entity and the reference of the entity.


Blitzplotter(Posted 2016) [#3]
I suspect the reference to the entity is akin to a pointer to the entity which expires with the next iteration of my poorly designed loop.


Kryzon(Posted 2016) [#4]
It's a number that identifies that entity.
I think it should give you more control to keep a type handle in the entity name.
Type SpecialCube
	Field entity%
	Field bla$

	;... Any properties you want.
End Type


myCube.SpecialCube = New SpecialCube
myCube\entity = CreateCube()
NameEntity( myCube\entity, Handle( myCube ) )

;When it's time to delete it...

thisCube.SpecialCube = Object( Int( EntityName( thisCube\entity ) ) )
FreeEntity( thisCube\entity )
Delete thisCube
Using the Object() and Handle() functions to convert user types references to user types to Int values and back.


Stevie G(Posted 2016) [#5]
You could parent all the cubes to a GlobalPivot and name them as you have already.

Then, to delete use: FreeEntity FindChild( GlobalPivot, "B899")

May be slowish if you have thousands of children. If you're planning to make some sort of Minecraft clone then an array of cubes would be a better approach.


Blitzplotter(Posted 2016) [#6]
Look forward to giving this a whirl tonight, thanks for the feedback, I am attempting a Conway life thing in 3D.


RemiD(Posted 2016) [#7]
@Stevie G>>good idea ! Thanks !


Blitzplotter(Posted 2016) [#8]
Thanks for the suggestions all of you, the code example below demonstrates how numerous cubes (of the same name) can be generated:


Graphics3D 800,600, 32, 3

Local CamPivot = CreatePivot()

Local Cam = CreateCamera(CamPivot)PositionEntity(Cam , 0 , 30 , -70)


globalpivot = CreatePivot()

PositionEntity globalpivot,1,1,1

PointEntity(Cam , globalpivot)

Global greencubecount=0



;1st cube

x=1: y=1: z=1

bsuffix$="B899"

cubelotsb=CreateCube(globalpivot)

NameEntity cubelotsb,(bsuffix$)

ScaleEntity cubelotsb,0.4,0.4,0.4

PositionEntity cubelotsb,x,y,z

EntityColor cubelotsb,0,255,0

;2nd cube

x=1: y=4: z=2

bsuffix$="C999"

cubelotsb=CreateCube(globalpivot)

NameEntity cubelotsb,(bsuffix$)

ScaleEntity cubelotsb,0.4,0.4,0.4

PositionEntity cubelotsb,x,y,z

EntityColor cubelotsb,255,0,0

;########################################################################

While 1
	
	UpdateWorld()
	RenderWorld()
	
	If KeyDown(16)
		
		Text 20,20,"Q Pressed, attempting to 'delete' the green cube
		
		FreeEntity FindChild( GlobalPivot,"B899")
		
		If greencubecount>=1 
			
			greencubecount=greencubecount-1
			
		EndIf
		
		
	EndIf
	
	If KeyDown(17)
		
		Text 20,20,"W Pressed, attempting to 'generate' the green cube
		
		x=1: y=1: z=1
		
		bsuffix$="B899"
		
		cubelotsb=CreateCube(globalpivot)
		
		NameEntity cubelotsb,(bsuffix$)
		
		ScaleEntity cubelotsb,0.4,0.4,0.4
		
		PositionEntity cubelotsb,x+(greencubecount*1.5),y,z
		
		EntityColor cubelotsb,0,255,0
		
		greencubecount=greencubecount+1
		
	EndIf
	
	If KeyDown(200)
		Text 90,90,"in"
		
		MoveEntity Cam,0,0,10
		
	EndIf
	
	If KeyDown(208)
		Text 90,90,"out"
		
		MoveEntity Cam,0,0,-10
	EndIf
	
	VWait 4
	
	Text 20,40,"Amount of green cubes idented with B999: "+greencubecount
	
	Text 20,62,"press W to generate green cubes, press Q to decrement green cubes..."
	
	Flip
	
	Cls
	
Wend

	
		



Bobysait(Posted 2016) [#9]
If you don't have too many rows, columns, heights, and a single cube per cell, you can just use an array to store the cubes. It will be faster than any "FindChild" method

for eg : a 64*32*64 voxel (-> 64 cells x, 32 cells y, 64 cells z)
Dim cubes(64,32,64)

cubes(8,9,9) = CreateCube()
[...]


;And to remove a cube, use the 3D coordinates :
FreeEntity cubes(8,9,9)
; and don't forget to reset the cell too ^^
cubes(x,y,z) = 0



Blitzplotter(Posted 2016) [#10]
@Bobysait, thanks for the suggestion. I was considering writing a DLL in 'c' to speed it up, however my 'c' is a little rusty. I (might) be able to ramp up the render area to 96 by 96..... (currently 32 x 32)


The first iteration of my life 'thing' whilst it works (pleasingly well) using the FindChild method (thanks StevieG), the rendering of checking through the status of a 32 by 32 grid takes a little while. Interesting results nonetheless:--

7 generations creates first red cell:



and a few generations later:



and a few generations later, bit like a Borg cube....