Arrays of entities

Blitz3D Forums/Blitz3D Beginners Area/Arrays of entities

Shifty Geezer(Posted 2004) [#1]
I've tried creating an array of entities within a function but it doesn't seem to work.

eg.
Dim actors(10)
For n=0 to 9
  test_func(actors(n))
Next

camera=createcamera()
pointentity camera,actors(0)

Function test_func(entity)
  entity=CreateSphere(8)
  PositionEntity entity,Rnd(-3,3),Rnd(-3,3),Rnd(-3,3)
End


The spheres are created and spread around but I'm told the entity actors(0) doesn't exist. Anyway I can create entities within an array in functions?


N(Posted 2004) [#2]
Try this:
Dim actors(10)
For n=0 to 9
  actors(n) = test_func()
Next

camera=createcamera()
pointentity camera,actors(0)

Function test_func(entity)
  entity=CreateSphere(8)
  PositionEntity entity,Rnd(-3,3),Rnd(-3,3),Rnd(-3,3)
  Return entity
End


You can't pass a pointer to a function so that the pointer gets set/written to (unless you're using a bank, in which case you'd have to poke the information into the bank). You're trying to pass a sort of pointer to the function test_func and then, in a sense, set the value of that pointer to the entity handle- basically, you can't do that specific thing in Blitz.


WolRon(Posted 2004) [#3]
That's because your entity variable is local to the function and lost once the function ends.

You need a return value from the function (or don't use the function at all).
Dim actors(10)
For n=0 to 9
 actors(n) = test_func()
Next

camera=createcamera()
pointentity camera,actors(0)

Function test_func()
  entity=CreateSphere(8)
  PositionEntity entity,Rnd(-3,3),Rnd(-3,3),Rnd(-3,3)
  Return entity
End


Edit: looks like Noel beat me to it.


N(Posted 2004) [#4]
Looks like our modified source code is the same, as well..

PLAGIARIZERZZZZ!!!!!!!!!1ONE!!

Sorry, it was just too tempting.


Shifty Geezer(Posted 2004) [#5]
Thanks!!

On a related note, is it possible to have an array of custom types so I can refer to each reference. eg.
Type cuboid
  Field x
  Field y
  Field z
End type

Dim actors(20).cuboid ; array of cuboid types, whatever the syntax would be

For n=0 to 19
  actors(n)\x=rnd(-10,10)
  actors(n)\y=rnd(-10,10)
  actors(n)\z=rnd(-10,10)
Next

p=actors(3)\x
q=actors(3)\y
r=actors(3)\z

If this worked, the x,y,z values of the fourth actor in the array are copied to p,q,r.

I'm guessing not and I'll need a workaround with each creation of a type needing a unique ID. Then I can trawl through the list from beginning to end until I find the one with the matching ID, but an array of types would be nicer!


N(Posted 2004) [#6]
Yes, but you need to define the array like this:
Dim actors.cuboid(20)


Just like how if you want an array of floats, you put the # after the name then the number of elements. You also have to create the objects before you can modify them.

For n=0 to 19
  actors(n) = new cuboid
  actors(n)\x=rnd(-10,10)
  actors(n)\y=rnd(-10,10)
  actors(n)\z=rnd(-10,10)
Next



WolRon(Posted 2004) [#7]
Looks like our modified source code is the same, as well..
Except that mine doesn't have a bug in it like yours.
PLAGIARIZERZZZZ!!!!!!!!!
How dare you!


Shifty Geezer(Posted 2004) [#8]
Wooho!


Shifty Geezer(Posted 2004) [#9]
Ack! Seems that these two things don't go together. Apparently I can;t have an array of type and use a function to populate it.
Type actor
  Field x
  Field y
  Field z
End Type

Dim actors.actor(10)

For n=0 to 9
  actors.actor(n)=test_func()
next

Function test_func()
  entity.actor=New actor
  entity\x = rnd(-10,10)
  entity\y = rnd(-10,10)
  entity\z = rnd(-10,10)
  Return entity
End Function

or alternatively
Type actor
  Field x
  Field y
  Field z
End Type

Dim actors.actor(10)

For n=0 to 9
  actors.actor(n)=New actor
next

For n=0 to 9
  actors.actor(n)=test_func()
next

Function test_func()
  entity.actor=New actor
  entity\x = rnd(-10,10)
  entity\y = rnd(-10,10)
  entity\z = rnd(-10,10)
  Return entity
End Function

and various variations. Is this a doomed mission or can you educated types bestow your wisdom upon me?


N(Posted 2004) [#10]
You're forgetting to tell the function to return an actor. By default, functions return an integer, so if you don't specify otherwise ($,#,%, or .YourTypeName) it'll return an integer.

Type actor
  Field x
  Field y
  Field z
End Type

Dim actors.actor(10)

For n=0 to 9
  actors.actor(n)=test_func()
next

Function test_func.actor()
  entity.actor=New actor
  entity\x = rnd(-10,10)
  entity\y = rnd(-10,10)
  entity\z = rnd(-10,10)
  Return entity
End Function



Shifty Geezer(Posted 2004) [#11]
Thanks. Blitz is more capable than it first seems when someone smart tells you about it!


gpete(Posted 2004) [#12]


Just in case someone stumbles on some of the
subtle errors above.... ;)