Function pointers

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Hello.
Are function pointers a thing in CX?

Seems like its possible using reflection, just wondered if its built in
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Certainly not perfect and also the code doesnt work trying to call myClass using
Code:
        If MouseDown(MOUSE_LEFT) Then CallFunction(Self,"draw")
using relection...:
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:

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Fixed that one.. I wasnt reflecting mojo.app
main.cxs:
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
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
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
 

Rich

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
Ive created a module to allow Function calls using the string names of the Functions and Parameters

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

Comments welcome
 
Top Bottom