smaller code

Blitz3D Forums/Blitz3D Programming/smaller code

Oiduts Studios(Posted 2008) [#1]
is there a way i can make this smaller

c1=CreateCube()
PositionEntity c1,-65,20,-10
ScaleMesh c1,2,2,2
EntityColor c1,100,100,100
EntityPickMode c1,1

c2=CreateCube()
PositionEntity c2,-65,15,-10
ScaleMesh c2,2,2,2
EntityColor c2,100,100,100

c3=CreateCube()
PositionEntity c3,-65,10,-10
ScaleMesh c3,2,2,2
EntityColor c3,100,100,100

c4=CreateCube()
PositionEntity c4,-65,5,-10
ScaleMesh c4,2,2,2
EntityColor c4,100,100,100

c5=CreateCube()
PositionEntity c5,-65,0,-10
ScaleMesh c5,2,2,2
EntityColor c5,100,100,100

c6=CreateCube()
PositionEntity c6,-65,-5,-10
ScaleMesh c6,2,2,2
EntityColor c6,100,100,100

c7=CreateCube()
PositionEntity c7,-65,-10,-10
ScaleMesh c7,2,2,2
EntityColor c7,100,100,100

c8=CreateCube()
PositionEntity c8,-65,-15,-10
ScaleMesh c8,2,2,2
EntityColor c8,100,100,100


smilertoo(Posted 2008) [#2]
Yes, but it wouldn't be as readable.
You could do it in a loop, while reading values from data statements.


boomboom(Posted 2008) [#3]
Why don't you make youself a function to create the exact cube you want? If you are doing repetative things, its often better to make a function for it. Personally, I code with a lot of functions. It helps keep your main loop really low in lines of code, and simple to read, and the nice thing is that once you have created your function and are happy with it, you can hide it away in an include file, or at the bottom of your page, so you don't have to look at it and get confused everytime you read the source. Here is a function to make the cube that you want:

Function CreateMyCube%(PosX#,PosY#,PosZ#,Scale#=2,ColorR%=100,ColorG%=100,ColorB%=100)
  ;Local Variables ----------
  Local Cube% ;Holds Cube Handle
  ;==========================

  ;Create And Transform the Cube
  Cube = CreateCube()
     PositionEntity(Cube,PosX#,PosY#,PosZ#)
     ScaleEntity(Cube,Scale,Scale,Scale)
     EntityColor(Cube,ColorR,ColorG,ColorB)
  
  ;Return The Cube ID
  Return Cube

End Function


Then you can make your cubes such as

;Standard Cubes
c1 = CreateMyCube(-65,20,-10)
c2 = CreateMyCube(-65,15,-10)

;Bigger Cube
c3 = CreateMyCube(-65,10,-10,3)

;Smaller Cube
c4 = CreateMyCube(-65,5,-10,0.5)

;Random Color
c5 = CreateMyCube(-65,0,-10,2,Rnd(1,255),Rnd(1,255),Rnd(1,255))


Etc.

I would personally do this with a type as well. But I don't want to confuse you too much :)


Edit:

So the automate the y position of it, using the function from above, and using an array for storage:

CubeNumber%=0
Dim Cubes(20)
For i = 20 to -15 Step 5
  CubeNumber = CubeNumber + 1
  Cubes(CubeNumber) = CreateMyCube(-65,i,-10)
Next

This is the bit where types are really nice. If your intrested I can post up an example using types for storage.


Ross C(Posted 2008) [#4]
I was just about to say that. The only thing changes is the Y pos, bar the entitypickmode on the first cube.

Oh, and also, shouldn't your step command be step -5?


boomboom(Posted 2008) [#5]
....shhh no one will notice :)


boomboom(Posted 2008) [#6]
Just wondering...

Is there any way, using my function example above, that I can set the color of the cubes, without having to define the size before (as the size has a value already)?

I have always found that I had to, but is there anyway to have it use the defualt size?


Moraldi(Posted 2008) [#7]
is there a way i can make this smaller

use a droplet program...


Ross C(Posted 2008) [#8]
boomboom, you would just put the colour first in the function.

Function create_cube(R,G,B,pos_x,pos_y,pos_z,scale_x=2,scale_y=2,scale_z=2)

Then you can override the default scales if you want, but you can't use the default function values if you are passing across values after that.


Stevie G(Posted 2008) [#9]
The default scale would be 1 , albeit with a mesh width, height and depth of 2.


chi(Posted 2008) [#10]
Graphics3D 800,600,0,2

cam=CreateCamera()
CameraZoom cam,1.6
PositionEntity cam,-65,2,-55

cubeY=20
Dim cube#(7)

For i=0 To 7
	cube#(i)=CreateCube()
	PositionEntity cube#(i),-65,cubeY-(5*i),-10
	ScaleEntity cube#(i),1.5,1.5,1.5
	EntityColor cube#(i),Rand(0,255),Rand(0,255),Rand(0,255)
	EntityPickMode cube#(i),2
	NameEntity cube#(i),"cube no."+(i+1)
Next

Repeat
	If MouseHit(1):MoveEntity cube#(1),1,0,0:MoveEntity cube#(7),-1,0,0:EndIf  ;LMB(move second and last box)
	CameraPick(cam,MouseX(),MouseY())
	If PickedEntity() > 0
		name$ = EntityName( PickedEntity() )
	Else
		name$=""
	EndIf
	RenderWorld
	Text 10,180,name$
	Flip
Until KeyHit(1)
End


edit: to speed things a little bit up you could also use copyentity instead of creating new cubes...


Oiduts Studios(Posted 2008) [#11]
thanks every1 (i like chi's code better)


boomboom(Posted 2008) [#12]
cool, whatever works for you :)

I would say to you that you should definatly bookmark this page and then come back to it in a couple of months.

Once you reach a certain amount of code, or a certain situation, such as if you wanted to create one of your cubes again somewhere else in the code, thats when you will find functions really can take you too the next level and be able to code bigger programs (in terms of complexity and lines of code).

Using your stuff as an example, if find later in your program that you need to rebuild your set of cubes in a different order, or location, or just want to rebuild one cube a different way, then you probably will have to do a gosub or copy/paste your code. This can lead to a very slippery slope of loosing how you understand your program, especially if you take a break for a couple of days/weeks and come back to it.


MaximilianPs(Posted 2008) [#13]
About PickedEntity....
i wonder if i could know some datas about.
i mean, i need to move the camera on a double on an entity

so i need to know piked entity's X, Y and Z.... who to ?


Ross C(Posted 2008) [#14]
PickedX() PickedY() PickedZ() PickedNX()... etc take your pick :o)

What exactly are you after?


MaximilianPs(Posted 2008) [#15]
oh mylol it was so easy ^^

i was using EntityX(entityPickata , True), but it looks cool :)
thnx alot