Author Topic: vaProxy Objects and Custom Subroutines  (Read 4057 times)

Mentat_Hawwat

  • Guest
vaProxy Objects and Custom Subroutines
« on: December 18, 2017, 09:39:52 PM »
Good evening!

I'm having some trouble with adding subroutines to a Voice Attack plugin.
I'm having trouble using the vaProxy object inside another subroutine.
The object is not set, of course, when VoiceAttack hits the entry point.
The gist of what I'm after is here:

Public Shared Sub VA_Invoke1(vaProxy As Object)
        vaProxy.SetDecimal("NumKittenFarts", 123.4)

        Call KittenSub()

    End Sub

    Public Shared Sub KittenSub()
        vaProxy.SetText("KittenName", "Jimmy")
    End Sub

Would anyone be able to help?  If so, I'd be quite gracious.

Antaniserse

  • Global Moderator
  • Jr. Member
  • *****
  • Posts: 87
    • My VA plugins
Re: vaProxy Objects and Custom Subroutines
« Reply #1 on: December 22, 2017, 07:12:28 AM »
The proxy object is passed by VA to the plugin Invoke() function as a standard parameter, so its scope is just local to that function.

If you want the variable to be available elsewhere, you need to bounce it forward as a parameter again, or store a reference somewhere else with a broader scope.

Personally, I made a base class to acts as a handler for all the other features in the plugin; the Invoke() code simply initialize the correct instance, and passes a reference to vaProxy that is stored in a protected property, so it's now available to all the inherited classes

A small excerpt taken from VAExtentions:
Code: [Select]
Public Shared Sub VA_Invoke1(vaProxy As Object)

        'The factory class gives back the correct instance of an inherited class, based on the 'vaProxy.Context' value, and store the full vaProxy object as a property
        Dim contextHandler As ContextHandlerBase = ContextFactory.Create(vaProxy)
        If contextHandler Is Nothing Then
            vaProxy.SetSmallInt(App.KEY_ERROR, ContextHandlerBase.ERR_CONTEXT)
            vaProxy.SetText(App.KEY_RESULT, String.Format("Unsupported or empty context: '{0}'.", vaProxy.Context))
        Else
            'This is where the actual code is performed
            contextHandler.Execute()
        End If
End Sub

where ContextHandlerBase is something like this:
Code: [Select]
Public MustInherit Class ContextHandlerBase
    Protected Context As ContextFactory.Contexts
    Protected VAProxy As Object

    Public Sub New(ByVal context As ContextFactory.Contexts, vaProxy As Object)
        Me.Context = context
        Me.VAProxy = vaProxy 'this will then be accessible by all inherited classes
    End Sub

    Public MustOverride Function Execute() As Boolean
End Class

You don't necessarly need to go into this complexities if you don't like it; simply add a (vaProxy As Object) parameter to all your subroutines and you are good to go.
« Last Edit: December 22, 2017, 07:26:33 AM by Antaniserse »
"I am not perfect and neither was my career. In the end tennis is like life, messy."
Marat Safin