Parameter object personality function

Blitz3D Forums/Blitz3D Programming/Parameter object personality function

Yue(Posted 2015) [#1]
Hello, how can I pass an object to a variable of a custom function?

When I talk about the subject I mean Rate.

The idea is to get any type, not just one specific.

Any suggestions?


Matty(Posted 2015) [#2]
type mytype

field x,y,z

end type

myobj.mytype = new mytype
myobj\x = 1
myobj\y = 2
myobj\z = 3

function myfunction(myvar.mytype)
;declare the function with an argument as above to pass a custom type to a function
;is that what you meant?
myvar\x = 4
myvar\y = 5
myvar\z = 6

end function 

myfunction(myobj)




Stevie G(Posted 2015) [#3]
If I'm understanding you correctly you need to make use of Object and Handle. It is a bit messy so you probably want to consider a better method of organising your game object.

This is just a quick and dirty example, note that you need to pass the handle of the type to the "DoSomethingWith" function.

[bbcode]
Type T1
Field x#,y#,z#
End Type

Type T2
Field a,b,c
End Type

Type T3
Field N$,x#,c
End Type

Global MT1.T1 = T1create( 1.5,2,105.66 )
Global MT2.T2 = T2create( 10,175,0 )
Global MT3.T3 = T3create( "Blah", 1.99, 5 )

Print Str( MT1 )
Print Str( MT2 )
Print Str( MT3 )

DoSomethingWith( Handle(MT1) )
DoSomethingWith( Handle(MT2) )
DoSomethingWith( Handle(MT3) )

Print Str( MT1 )
Print Str( MT2 )
Print Str( MT3 )

MouseWait
End

Function T1create.T1( x#, y#, z# )

Local this.T1 = New T1

this\x = x
this\y = y
this\z = z

Return this

End Function

Function T2create.T2( a, b, c )

Local this.T2 = New T2

this\a = a
this\b = b
this\c = c

Return this

End Function

Function T3create.T3( N$,x#, c )

Local this.T3 = New T3

this\N = N
this\x = x
this\c = c

Return this

End Function



Function DoSomethingWith( myHandle )

Local tmp1.T1
Local tmp2.T2
Local tmp3.T3

If Object.T1( myHandle) <> Null
;I'm a T1 type
tmp1 = Object.T1(myHandle)
tmp1\x = tmp1\x + 1
EndIf

If Object.T2( myHandle) <> Null
;I'm a T2 type
tmp2 = Object.T2(myHandle)
tmp2\a = tmp2\a + 10
EndIf

If Object.T3( myHandle) <> Null
;I'm a T3 type
tmp3 = Object.T3(myHandle)
tmp3\N = tmp3\N + " amended"
EndIf

End Function
[/bbcode]


Yue(Posted 2015) [#4]
Hi, I hoped to pass any object (type).