a straight answer to the type thing

BlitzMax Forums/BlitzMax Programming/a straight answer to the type thing

{cYan|de}(Posted 2004) [#1]
ok not being a git, and tnx for the help so far, but most of it really hasnt helped.

i need to do the following, i just need a simple straight answer and a lil example maybe.


this is b3d etcish code, and does nothing really but shows exactly what i need.

type pie
field nothinghereforthisexample
end type

global mypie

function initprog()

p.pie = new pie
mypie = handle(p)

end function


function updatetheprog()

updatepie(mypie)

end function


function updatepie(pt)

p.pie = object.pie(pt)
;in the real version id be using any vars i had inside the type instance here for positioning etc etc

end function


well thats pretty much what i need, only ways ive been told are really not working. and in this case the docs are not my friend;) btw the actual object type stuff aint the problem, the new way of doing the object handle stuff.


Robert(Posted 2004) [#2]
Sorry to ask a silly question, but why don't you just define mypie as an instance of pie? The above example doesn't show *why* you need to use object and handle in first place. If you can explain why you need object and handle, then perhaps we can help.


{cYan|de}(Posted 2004) [#3]
its meant to show what i need, not why. however, if the above would work could you post a snip of it? i need to pass it into functions etc


Robert(Posted 2004) [#4]
Why is important though. It might well be the case that there is a completely different (but better) way of doing things in BMX. If this is about one function handling different types of object then that is totally redundant given BlitzMAX's OO capabilities.

Here is a direct translation of your source:

Type pie
Field nothinghereforthisexample
End Type

Global mypie

Function initprog()

p:pie = New pie
mypie = p

End Function


Function updatetheprog()

updatepie(mypie)

End Function


Function updatepie(pt)

p:Pie = (Pie Ptr(pt))[0]

End Function  


The (Pie Ptr(pt))[0] bit converts the integer pt to a pointer to a Pie object, which can then be dereferenced (the [0] part) to give the Pie object itself.


Tibit(Posted 2004) [#5]


I would recommend you to do this in a new OOP way. Here is the exact same thing but with OOP.



Note there is no Lists involed in this so you can't loop thru them with a For..inEach loop

If you haven't checked out these BMax-Tutorials do so ;)
http://www.blitzbasic.com/Community/posts.php?topic=42002
This one is mainly about types:
http://www.blitzbasic.com/Community/posts.php?topic=42003


{cYan|de}(Posted 2004) [#6]
ok say i made a few "pies"

basically

p.pie = new pie
mypie = handle(p)

a few times with the handle mypie being different each time (kinda like when you load a image or whatever you give a handle ie image = loadimage)

i need to be able to pass that into functions (not in the pie object) and edit the values inside the type instance that handle refers to.


skidracer(Posted 2004) [#7]
This illustrates the autocasting of objects to ints and back that BlitzMax performs for function calls:
Function Handle:Object(o:object)
return o
End Function

Function Obj:Object(o:object)
return o
End Function

type mytype
end type

t:mytype=new mytype
h=Handle(t)
b:mytype=mytype(Obj(h)) 'same as Object.mytype(h) in legacy blitz



Tibit(Posted 2004) [#8]
When you pass the object you pass the adress to it ie the handle? I guess I don't understand what you are looking for?


Robert(Posted 2004) [#9]
Neither Blitz nor BlitzMAX pass the actual object into a function, they pass a reference to it, so editing the parameter edits the actual object itself:

Type pie
Field a
End Type

q:Pie = New Pie

updatePie(q)

Print q.a

Function updatePie(pieParam:Pie)
	pieParam.a=42
End Function



Floyd(Posted 2004) [#10]
Why is important though.


That's really the key point.

I've seen a lot Object/Handle code on these forums and in the Code Arhives. Most of it is truly dreadful.

Typically Object and Handle are used to solve some perceived problem that doesn't really exist.


FlameDuck(Posted 2004) [#11]
Typically Object and Handle are used to solve some perceived problem that doesn't really exist.
And should they have been used to solve an actual problem, this can now be done much more elegantly, using polymorphism.

i need to be able to pass that into functions (not in the pie object) and edit the values inside the type instance that handle refers to.
Why? Types are already being passed by reference! Handle is in this case superfluous as far as I can tell.


{cYan|de}(Posted 2004) [#12]
spot on, tnx all. more of this stuff should be doc`ed nearer to final release on win and linux,