Type within a Type

Blitz3D Forums/Blitz3D Beginners Area/Type within a Type

Ross C(Posted 2006) [#1]
Ok, this is a pretty simple question, considering how long i've been using blitz.

I want to have a type collection like so:


Type Entity

  Field x,,z
  Field rotx,roty,rotz
  Field movement ;  if movement type = 1 then data is needed
  Field movement_type
  Field data_

End Type



Now, if the movement field = 1 then i want to have the data_ field as a type collection, containing the movement data. I have two questions though.

1. Is this possible?

and

2. Is the data contained within the data_field, exclusive to that particular type object? I mean, say i create new data in the type collection data_, would any other Type objects from the ENTITY collection be able to access data, that field did not create?


Ross C(Posted 2006) [#2]
I'll try and explain better. I've read that and i don't understand it :o)

I create 3 objects, using the fields above.

OBJ1

OBJ2

OBJ3

Now, OBJ3 has movement data, so i create a new object for the data_ field.

Can OBJ1 and OBJ2 access this data. Is this data accessable from OBJ1, OBJ2 and OBJ3, or does it belong to OBJ3?


Ross C(Posted 2006) [#3]
Ok, i knocked this up.

Type info
	Field st
End Type


Type entity
	Field x
	Field dat.info
End Type


e.entity = New entity
e\x = 7
Print "first entity x = "+ e\x

e.entity = New entity
e\x = 9
Print e\x
Print "second entity x = "+ e\x

e\dat.info = New info
e\dat.info\st = 8
Print e\dat.info\st

e\dat.info = New info
e\dat.info\st = 9
Print e\dat.info\st

Print 

e.entity = First entity

For e\dat.info = Each info

	Print e\dat.info\st
	
Next


Is this the correct way of doing this? If so, then it seems i can access the data created with the second ENTITY object, in the first entity. I realise why, because i'm looping through every instance of the type objects.

SO, is there anyway to create a field, within a field, so to speak, that i can create data in, only if it's needed?


octothorpe(Posted 2006) [#4]
I think you want to create objects which belong to other objects. This is not intrinsicly possible with Blitz, but there are many ways to accomplish it.

The simplest way to do this is to store a reference to the parent objects from within the child objects, like so:



The major drawback to this method is that you have to loop through all the children anytime you want to find children associated with a parent.

A container will overcome this drawback - Linked Lists are the most popular type of container. Here's how they work: the parent object contains a field pointing to the first child in its list, while each child has a field pointing to the next child in the list. By following the pointers, you can access all the children in the list. NULL signifies the end of the list.

To accomplish Linked Lists, you'll need to add List functions for every Type you want lists of (you can likely find these functions in the code archives.) Another approach is to use my Container Classes to manage Handle()s to your objects.


Ross C(Posted 2006) [#5]
hey thanks man. That sounds more like it :o) Speed isn't of the essence here, as it's an editor. The in game stuff won't work like this. Thanks!