Question about types and pointers...

BlitzMax Forums/BlitzMax Beginners Area/Question about types and pointers...

LT(Posted 2007) [#1]
Let's say I want to create a type with a field of unknown type. Is this possible now without the variable pointers?

Type node
  Field obj
End Type

Where 'obj' could be a variable describing mesh data or anything else (this is for loading code). If I want to write a scene exporter for 3dsMax and node is the base type and it could contain data for a mesh, or a dummy, or anything else, what methodology would you recommend?


Dreamora(Posted 2007) [#2]
There is no unknown type.

It will always be extended from :Object

Problem is that you won't be able to get any more specific information from it if you really make it an :object so I suggest you write a base class for your stuff that saves some information on "what" it is so you know to what you have to cast it to retrieve the information again.


LT(Posted 2007) [#3]
But what if you don't know what it is until you load it? Let's say I have a type that describes mesh data and another that describes a light. When I actually load the object, I find out what type it is and go to the appropriate loading code, which fills in the obj field. It seems I can't do that without an unknown type or some kind of variable pointer.

I realize I could just create extended types with node as the base, but it would be nice if it could be more generic than that. I already have a nice system in 3dsMax that will easily write out any struct data with minimal effort on my part. I was hoping to be able to create something equivalent in Blitz Max.

Nuts!


Azathoth(Posted 2007) [#4]
Cast the reference to the type you're checking for.


LT(Posted 2007) [#5]
Gee, that's helpful. The reason I posted this in the beginner's forum is because I'm new to Blitz Max and the docs are rather lacking. The only example I've found involves using a variable pointer and that no longer works in the latest version. String and type pointers now bring up errors when they are dereferenced.


Azathoth(Posted 2007) [#6]
You haven't given much info as to how you're currently trying to do it and why you need pointers over references. References are safer.


Jake L.(Posted 2007) [#7]
Example of type-checking:
Type TMesh
   Method Morph();...;end method
End Type

Type TLight
   Method Shine();...;end method
End Type

global entity:TMesh=new TMesh

if TLight(entity) then TLight(entity).Shine() else TMesh(entity).Morph()


I see no usage for string-pointers in Bmax except you're talking to C/C++ code. So I can't imagine an example that makes sense. But maybe others can...

However, if I remember right you can create a pointer to a string using
global s$="a string"
global sptr:Byte ptr=s[0]


Jake


grable(Posted 2007) [#8]
Depending on your needs and preference, you could use SendMessage and not care what type it is.



Personally i tend to go for the Base class with an integer tag approach ;)


LT(Posted 2007) [#9]
@Jake: I see no usage for string-pointers either. That was just an example.

Thanks for the responses, but nothing has come close to what I am trying to do. Let me try to explain again...

Let's say I have three types...

Type node
  Field obj
End Type

Type mesh
  Field data
End Type

Type light
  Field data
End Type

I would like the obj field in a variable of type node to point to data of type mesh if the object being loaded is a mesh and to data of type light if the object being loaded is a light. Of course, there could be many other types of data. Those are just two examples. Because I have to declare obj in advance, I can't decide based on what is being loaded from a file. Because Byte Ptr no longer works with types, I can't have it point to an area in memory where the mesh or light type data exists.

As I said before, I could create extended types off the node, but that's not as generic as I would prefer. It also gets a bit sticky when I want to deal with tree nodes.

I have MaxScripts that can write out any kind of struct data in 3dsMax as long as there is a struct definition that describes it. This is possible because MaxScript doesn't care about types and does casting automatically and the structs are dynamic. I was just hoping to find something similar in BlitzMax.


tonyg(Posted 2007) [#10]
Can't you declare it as type Object (e.g. obj:object) and then cast it to the other objects as people are suggesting here?


LT(Posted 2007) [#11]
Hmm, maybe so. I'm not familiar with how it works in BlitzMax. Perhaps grable's example is useful, after all. The example doesn't show casting back to a type, but maybe that works. I'll give it a try tonight when I get home. :)


LT(Posted 2007) [#12]
Hey, it works! Of course, because I have to know the type, I'll have to store a string (or some kind of class id) to go along with each piece of data to identify it, but that's not so bad.

Thanks for the help and forgive me for the newbie question, but at least I posted in the right place. :)

EDIT: Aargh! Now I'm seeing a topic called "Getting types back from objects" that is exactly what I was looking for. I swear it didn't show up in my first search! Bah!