convert this

BlitzMax Forums/BlitzMax Programming/convert this

{cYan|de}(Posted 2004) [#1]
Type monkey
Field id
End Type

m.monkey = New monkey
m.id = 1212121

ter = Handle(m)
s.monkey = Object.monkey(ter)
print s.id

pointless program but still not sure how you would do the equiv in max, (the object handle stuff) the type stuff aint a problem, (this is b3d etc code btw;P)


Robert(Posted 2004) [#2]
You don't really need this in BlitzMAX any more, as the main usage of Object and Handle as I understood it was so a function could accept different (but related) types. With the Extends keyword in BlitzMAX this isn't really necessary any more.

For example you would declare an abstract Animal type, and then create Cat and Dog subtypes. The function would ask for an Animal, and hence would accept Cat and Dog subtypes.

Here is the code though if you need B3D functionality exactly:


Type Monkey
	Field id
End Type

m:Monkey = New Monkey
m.id=1212121

'Declare a Byte Ptr.  If you wanted to avoid all of the casting, you could declare this as 
'a Monkey Ptr, however if you want to replicate Blitz3D exactly you need a Byte Ptr.
'This can later be converted to any type of object pointer (not adviseable though) 

Local pointerToM: Byte Ptr

'Get the address of m (varptr) and cast it from a Monkey Ptr to a Byte Ptr

pointerToM = Byte Ptr(Varptr(m))

'The Monkey Ptr( ) bit casts the Byte Pointer to a Monkey Pointer.  The [0] bit at the
'end dereferences the pointer (ie. gets the value stored at that address)

q:Monkey=(Monkey Ptr(pointerToM))[0]

Print q.id



{cYan|de}(Posted 2004) [#3]
it dont quite work if you need to pass it into a function thou, surely you can? i need to pass a type instance into any number of functions and be able to access the vars inside that type instance.


Bot Builder(Posted 2004) [#4]
Erm, lol. You could pass type instances into functions in B3D as well.

Blitzmax it should be somehting like:

Function MyFunction(MyMonkey:Monkey)
 MyMonkey.OoohAhh(50,60)
 MyMonkey.Bananas=5
EndFunction



Dreamora(Posted 2004) [#5]
That isn't a problem.
You can just use the type to define the function input parameters or if you have different types that you want to use with this function, you can create a basetype and extend it from this one and access the basetype.

Don't know if this is what you are actually trying to achive ... but if so I wrote a little codesample.




An alternative would be the usage of VarPtr and the [0] access mentioned in the code above.