Graphics

 View Only
  • 1.  Event Handling Inside XPression

    Posted 08-25-2018 06:50

    Hey guys,

    Just messing around with XPression Global scripts and ran into an issue; i'm try to get a event trigger from an output using xpOutputFrameBuffer.OnSceneState, so far I've had no luck.

    Here is the code I have so far; just to make sure I wasn't doing anything terribly wrong I implemented the OnCIITrigger from xpEngine which works fine.

    I was getting a BC31029 error when using OnSceneState from xpOutputFrameBuffer so I switched to using OnClear as it doesn't have any parameters, still no luck...

    I thought maybe the reason it wasn't working as I haven't actually grabbed an Output yet, only defined the object itself, so I wrote a function that gets the output and returns it, still no luck..

    I'm at a loss as to why its not working, any feedback would be helpful! (Keep in mind this my first time doing event handles in VB)

    'Define New Engine Object with event handling
    Dim WithEvents Engine as new xpEngine

    Dim WithEvents fb as xpOutputFrameBuffer = GetFB(Engine)

    Function GetFB(Engine as xpEngine) As xpOutputFrameBuffer
    Dim fb1 as xpOutputFrameBuffer

    Engine.GetOutputFrameBuffer(0,fb1)

    Engine.DebugMessage(fb1.BoardType.tostring,0)

    return fb1

    End Function

    'Sub that will handle the OnCIITrigger event
    Private Sub OnCII(ByVal Trigger As String) Handles Engine.OnCIITrigger
    If Trigger = "TEST" Then
    Engine.debugmessage("Works!",0)
    End If
    End Sub

    Private sub Cleared() Handles fb.OnClear

    Engine.DebugMessage("Cleared Test",0)

    End Sub



  • 2.  RE: Event Handling Inside XPression

    Posted 08-25-2018 16:43
    Hello bderry ,

    good work for a first time with events !
    Thank you for your sample code and the subject it brings.

    Compilation error code BC31029 indicates a mismatch between the signature of the event and the function to handle this event.
    For OnSceneState event , this happens if you use long type for State parameter : it can be fixed if you change it to a integer.
    For OnClear event, the error shouldn't be raised , that's strange.

    A script like this one should compile without errors (it's ok within Xpression Designer 6.7 and 8.0 ) :
    Dim WithEvents fb as xpOutputFrameBuffer

    Public sub OnSceneState(ByVal Scene As xpScene, ByVal State As integer ) Handles fb.OnSceneState
    end sub

    Private sub Cleared() Handles fb.OnClear
    End Sub


    Using "WithEvents" and "Handles" clause is the standard way of associating an event with an event handler and it's done at compile time.
    [FONT=arial]In your sample code fb [/FONT]object is retrieved after compilation : that may explain why it's not working.


    To avoid this , you can use dynamic event handling instead with AddHandler clause :
    Dim myFb as xpOutputFrameBuffer = Nothing

    public sub MyHandlerCreation(byval Engine as xpEngine)
    Dim fb1 as xpOutputFrameBuffer = Nothing

    ' be sure to add handler only once
    if myFb is Nothing and Engine.GetOutputFrameBuffer(0,fb1) then
    myFb = fb1
    AddHandler myFb.OnClear , AddressOf Cleared
    end if

    end sub

    Public sub Cleared()
    ' Do something here
    ' Engine.DebugMessage("Cleared Test",0)
    End Sub


    a call to MyHandlerCreation function will create the handler .



    Hope it helps.



    #XPression


  • 3.  RE: Event Handling Inside XPression

    Posted 08-26-2018 06:59
    Awesome so I seem to have it working now; I have attached the working project for anyone future reference [ATTACH]n16934[/ATTACH] . thanks for the help @amignon!

    Code:
    Dim WithEvents Engine as new xpEngine

    Dim WithEvents myFb as xpOutputFrameBuffer = Nothing

    public sub EngineState(ByVal state as EngineState) handles Engine.OnState
    Dim Engine as new xpEngine
    Engine.Debugmessage("FB Events loading",0)
    OutputCreation(Engine,0)
    Engine.Debugmessage("FB Events loaded",0)
    end sub


    public sub OutputCreation(ByVal Engine as xpEngine, ByVal FbIndex as Long)

    try
    if Engine.GetOutputFrameBuffer(FbIndex,myFB) then

    Engine.Debugmessage("Successfully got FrameBuffer: " + FbIndex.toString + " From Brand: " + myFB.Brand,0)

    end if


    Catch ex as System.Exception
    Engine.Debugmessage(ex.message,2)

    end try

    end sub

    Public sub Cleared() handles myFb.OnClear

    Engine.DebugMessage("Cleared",0)

    End Sub

    #XPression


  • 4.  RE: Event Handling Inside XPression

    Posted 08-26-2018 15:50
    I recommend you do not do this. You are going to run into reliability issues, as nothing will hold onto the reference of Engine as soon as your script finishes executing. Engine will be eligible to be garbage collected by the .NET garbage collector, and mysteriously your event will suddenly stop being called.

    The xpEngine class is only designed to be instantiated from outside applications of XPression, where the external application can hold a reference to it. Scripts are meant to be short lived pieces of code that terminate as soon as the script has completed, at which time all objects within it may be free'd.

    If you are trying to do something like this, you should probably just write an external C# or VB application.

    #XPression