Another 'Type' problem

Blitz3D Forums/Blitz3D Beginners Area/Another 'Type' problem

ClaudeClaus(Posted 2003) [#1]
Hello,

I would like to use types for handling baddies in my game but if I create my new types in a function it's not possible to declare them as 'global' and then I get an error message at compile time when I try to access them from within another function.

Am I missing something or should I create them as global at the beginning of my program (but then it's not very dynamic as I cannot create the amount I need for each level nor delete them when they're 'dead'). Or should I use subroutines instead of function ?

Thanks for any help :)
Claude


MikeT(Posted 2003) [#2]
Hi Claude

All you need to do is put a g;obal declaration of the object/type after the type is declared e.g.

;declare the type...(in this example an alien)
;alien
Type alien
Field x
Field y
Field reloading
End Type

then do a global declaration like this....

Global alien.alien ;alien object

This allows you to create as many aliens as you need with the following line..

alien.alien = New alien

this can be put inside a loop or function and creates a new object of the alien type (an alien). You can create as many aliens as you want with only the 1 global declaration.

Setting it as global allows the alien objects to be accessed anywhere in your code.

Hope this helps

MikeT


Ken Lynch(Posted 2003) [#3]
[quote]Am I missing something or should I create them as global at the beginning of my program[quote]

Yes, you are misunderstanding types. Types are always global, so your functions can always access each type.

type myType
  field field1
  field field2
end type

for n = 1 to 10
  ;m is local here

  m.myType = new myType
  m\field1 = n
  m\field2 = n * 2
next

PrintMyTypes

waitkey

function PrintMyTypes()
  ;t is local and goes through each myField type

  for t.myType = each myType
    print t\field1 + " " + t\field2
  next
end function


As you can see I have no global variables and can still access the types. Also I don't need to create a new variable for each type I create, I can reuse variables because of the way types work.

I can use the following:

t = first myType ;t points to the first type
t = last myType ;t points to the last type
t = after t ;t now points to the next type
t = before t ;t now points to the previous type



ClaudeClaus(Posted 2003) [#4]
Waow, that were quick answers. Thanks very much guys! Problem solved :))
Claude


Boiled Sweets(Posted 2003) [#5]
you say...

t = first myType ;t points to the first type
t = last myType ;t points to the last type
t = after t ;t now points to the next type
t = before t ;t now points to the previous type

This is intesting, but how if I have a 3d array do I refer to object t(3,4,5) for example if I had...

type myType
field x_pos
field y_pos
field z_pos
end type

for x = 1 to 10
for y = 1 to 10
for z = 1 to 10
m.myType = new myType
m\x_pos = x
m\y_pos = y
m\z_pos = z
next
next
next

How do I now refer to the item at element 3,4,5?

Can it be done, it certainly can in c.


soja(Posted 2003) [#6]
But you don't have a 3-dimensional array... you've got a list of 1000 myTypes. The first one has x_pos=1, y_pos=1, z_pos=1, the second one has x_pos=1, y_pos=1, z_pos=2, the eleventh one has x_pos=1, y_pos=2, z_pos=1, etc, all the way to #1000, which has x_pos=10, y_pos=10, z_pos=10.

Also, you've got one declared myType pointer, "m", which is currently pointing to the last myType you created (the same as "Last myType").

You would have to iterate through the list to get the one you wanted, since you didn't save any pointers.
m.myType = First myType
for i = 1 to <whatever> : m = After m : Next


What you could do is create an array for myType pointers, so you could have directo access to any of them.
Type myType
	Field x_pos
	Field y_pos
	Field z_pos
End Type

Dim t.MyType(10,10,10)
For x = 1 To 10
	For y = 1 To 10
		For z = 1 To 10
			t(x,y,z) = New myType
			t(x,y,z)\x_pos = x
			t(x,y,z)\y_pos = y
			t(x,y,z)\z_pos = z
		Next
	Next
Next


Now, you still have the list of 1000 types, but instead of just one type pointer ("m") pointing to the last one you created, you've got 1000 type pointers in the "t" 3-dimensional array that you can access.


Boiled Sweets(Posted 2003) [#7]
Soja,

thanks, you figured it out too! I managed to do it also just like you did before reading this.

Thanks for your help though :-)

Best regards,

Rupert.