return type from function

Blitz3D Forums/Blitz3D Beginners Area/return type from function

ryan scott(Posted 2005) [#1]
can you return a type from a function?
function makeabullet()
b.bullet_type = new bullet_type
return b.bullet_type ?
end function

b2.bullet_type=makeabullet()

(this doesn't work)

i can't figure out how to do it, if its possible.


degac(Posted 2005) [#2]
try this one, it should work:

function makeabullet.bullet_type()

you must declare what type of 'RETURN' you want.


WolRon(Posted 2005) [#3]
It's not as complex as it sounds.

The type identifier for the function is always declared between the function name and the parenthesis ().

Examples:
funcname()             ;returns an integer (lack of an identifier defaults to integer)
funcname%()            ;returns an integer
funcname$()            ;returns a string
funcname#()            ;returns a float
funcname.mytype()      ;returns a mytype type
funcname.bullet_type() ;returns a bullet_type type
funcname.anytype()     ;returns an anytype type



markcw(Posted 2006) [#4]
thanks, i couldn't find anything in the help on this, but i knew it would be in these forums somewhere.

also, here is a full example of how to return a type from a function.

;return a type example

test.testtype=example()

Print "x="+test\x
Print "y="+test\y

WaitKey
End

Type testtype
 Field x,y
End Type

Function example.testtype()

 Local test.testtype
 test.testtype=New testtype
 test\x=42
 test\y=24
 Return test.testtype

End Function



Sledge(Posted 2006) [#5]
I take it that local instance gets deleted automagically on program exit? I noticed in Mark's old skybox creation function that [EDIT: IGNORE ME, I'M AN IDIOT WHO REMEMBERS THINGS BACKWISE, BUT ANYWAY] I've always wondered if this is the "official" (ie a safe) way to manage local entities and type instances.


markcw(Posted 2006) [#6]
i don't know what skybox function you mean, but i assumed that is the correct way to do it.

afaik, when you declare a local in a function, that function will be able to use that variable even if there is a global variable of the same name. if you don't specify a local then a global of the same name will override it. so you may end up with a bug in your code if you don't use locals in functions.


Sledge(Posted 2006) [#7]
I've just been avoiding creating things in functions, for no other reason than not being sure what happens (to the local object(s)) when you return either an entity or type. I guess there's no such thing as a local type instance and, when returning entities, you pass back the reference to the object's location and can free it by referring to whatever you called the function with (ie mySkyBox.skyBox=createSkyBox()).

I'm just being daft and worrying about nothing, but hoping somebody else would say "you're just being daft and worrying about nothing". :D


markcw(Posted 2006) [#8]
yes, i think you're worry about nothing. :)

when you do "my.mytype=New mytype"
that's your instance of a new type.

by doing "function myfunction.mytype()"
you can then return a type created inside a function.

if you can do that then there is no reason why you can't delete the type.


(tu) sinu(Posted 2006) [#9]
When you create a local type within a variable, the handle of the type is not global but all types are automatically global. You can access the newly created type, just not by using the local handle but by either assigning a global handle or using for t.ThisType = each ThisType