• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Function pointers

Rich

Well-known member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
3rd Party Tool Dev
Joined
Sep 9, 2017
Messages
451
Hello.
Are function pointers a thing in CX?

Seems like its possible using reflection, just wondered if its built in
 
Certainly not perfect and also the code doesnt work trying to call myClass using
Code:
        If MouseDown(MOUSE_LEFT) Then CallFunction(Self,"draw")

Code:
Strict
Import reflection
Import mojo2

#REFLECTION_FILTER="main"

Class test
    Function draw:Void()
        Print "hello from test"
    End

End

Class myClass Extends App
    Field canvas:Canvas
  Field t:test

    Method OnCreate:Int()
        SetUpdateRate(60)              
        canvas = New Canvas
        t = New test
       
        Return 0
    End

    Method CallFunction:Void(obj:Object,function_name:String,objs:Object[]=[])
        Local c:ClassInfo = GetClass(obj)
        Local funcs:FunctionInfo[] = c.GetFunctions(True)
        Local i:Int=0
        While i<funcs.Length()
            If funcs[i].Name=function_name
                funcs[i].Invoke(objs)
                i=funcs.Length()
            End
            i+=1
        Wend
    End
   
    Method OnUpdate:Int()
        If MouseDown(MOUSE_LEFT) Then CallFunction(t,"draw")

        Return 0
    End
   
    Method OnRender:Int()
        Return 0
    End
   
    Function draw:Void()
        Print "hello from myClass"
    End
   
End


Function Main:Int()
    ' Create an instance of your class that you have defined above.
    New myClass      
    Return 0
End
 
Last edited:
Fixed that one.. I wasnt reflecting mojo.app
Code:
Strict
Import reflection
Import mojo2

#REFLECTION_FILTER="main;mojo.app"

Class test
    Function hello:Void()
        Print Millisecs()+" hello from test"
    End

End

Class myClass Extends App
    Field canvas:Canvas
  Field t:test
     
    Function hello:Void()
        Print  Millisecs()+" hello from myClass"
    End

    Method OnCreate:Int()
        SetUpdateRate(60)              
        canvas = New Canvas
        t = New test
        Return 0
    End
   
    Method OnUpdate:Int()
        If MouseDown(MOUSE_LEFT) Then CallFunction(t,"hello")
        If MouseDown(MOUSE_RIGHT) Then CallFunction(Self,"hello")

        Return 0
    End
   
    Method OnRender:Int()
        Return 0
    End
   
    Method CallFunction:Void(obj:Object,function_name:String,objs:Object[]=[])
        Local c:ClassInfo = GetClass(obj)
        Local funcs:FunctionInfo[] = c.GetFunctions(True)
        Local i:Int=0
        While i<funcs.Length()
            If funcs[i].Name=function_name
                funcs[i].Invoke(objs)
                i=funcs.Length()
            End
            i+=1
        Wend
    End

End


Function Main:Int()
    ' Create an instance of your class that you have defined above.
    New myClass      
    Return 0
End

Now I need to add the ability to add parameters to the function call.
I feel another module coming on here
 
Got that working now.... its always hit and miss with reflection so code isnt amazing. Gonna sleep now and will post up tidy code tomorrow
 
Ive created a module to allow Function calls using the string names of the Functions and Parameters

Cerberus:
CallFunction(Self,"draw",["c"])
CallFunction(t,"draw",Self,["c"])
See the repo for a running example

Comments welcome
 
Back
Top Bottom