Author Topic: Using variables across instances?  (Read 4449 times)

Castleberg

  • Newbie
  • *
  • Posts: 37
Using variables across instances?
« on: January 01, 2022, 09:51:38 AM »
I seem to be running into a wall here in using variables across Inline Function instances.

A simple test:

Command 1 : calls Inline function :

Code: [Select]

public class VAInline
{
public void main()
{

int x = 4321;
}
}

Command 2 calls a different inline function :

Code: [Select]

public class VAInline
{
public void main()
{

VA.SetInt("TestVar321", x);

}
}

Ends with error that "x" does not exist in the current context.

Do I need to write my code as a separate Assembly, and reference it?  I have tested with the "retain instance" checked, but still run into this error. I have no double I am overlooking something, but can't seem to find the answer in the forums.

As a side (or potentially related) issues. I am a little confused on how I can be running 2 inline functions, both with a public void main() signature, without causing a error of multiple entry points. I assume the "main()" is not really main, but just instantiates a new method under the "VAInline" Class. Or more specifically executes it's code within that class.

Trying to create a new method under the VAINLINE { Main() { that I can call from another instance is also unsuccessful.

Where am I going wrong?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Using variables across instances?
« Reply #1 on: January 01, 2022, 11:17:38 AM »
If you want to use variables across instances, you'll want to save their values as VoiceAttack variables using the relevant "Set" method, and retrieve them using the matching "Get" method.

For retention of complex datatypes in memory, you'll currently want to use a plugin, which has access to its own instance of the SessionState Dictionary


One instance of an inline function cannot just access the variables declared in another, least of all those scoped to a specific method.
You'll want to have a look at the C# documentation from Microsoft for more information on such non VoiceAttack-specific topics

Castleberg

  • Newbie
  • *
  • Posts: 37
Re: Using variables across instances?
« Reply #2 on: January 01, 2022, 12:21:02 PM »
Much thanks. I finally noted the line in the documentation that states, "NOTE:  This property should only be used with plugins, as the values are not maintained between calls using inline functions. " 

Hmm. I think plugins may be beyond my capability at the moment, but something to strive for....

What would be the most efficient way to store a List<string> or List<obj> ?  Given VA lacks arrays, and I don't think it has lists, I am hesistant to run a for loop and array it into a series of variables, although I suppose that is my only alternative...

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Using variables across instances?
« Reply #3 on: January 01, 2022, 12:49:17 PM »
You can split a list into separate variables as you mentioned, or you could serialize it as a text format like JSON or XML, so it can be stored into a single string variable. The latter would also be much more attractive when using more complex datatypes.

Serialization and de-serialization of a few variables in memory shouldn't take very long, in human perception terms.