Author Topic: Use a variable in the Variable Name  (Read 4739 times)

DeeM77

  • Guest
Use a variable in the Variable Name
« on: December 04, 2017, 01:58:14 AM »
Hi,

Is it possible to set and use a variable in the Variable Name? I'd like to run the script for "personality 1" to "personality 12". So instead of copy pasting I would like to use a loop that stops after "personality 12".

Code: [Select]
Begin Boolean Compare : [personality 1 occupied] Equals True
    Begin Small Integer Compare : [personality 1] Equals [me]
        Set small int (condition) [me] value to 1
    End Condition
    Begin Small Integer Compare : [personality 1] Equals [you]
        Set small int (condition) [you] value to 1
    End Condition
    Begin Small Integer Compare : [personality 1] Equals [anyone]
        Set small int (condition) [anyone] value to 1
    End Condition
End Condition

Tnx

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Use a variable in the Variable Name
« Reply #1 on: December 04, 2017, 03:41:42 AM »
Any token can be used in a variable name, however there is a limitation: The only type of variable with a variable variable name that can be evaluated in a condition is "Text"(You'll notice the "Text" tab is the only one that accepts tokens as well as variable names).

So you can do
Code: [Select]
Set integer [personalityID] value to 1
Set integer [personality {INT:personalityID}] value to 123
Write '[Blue] {INT:personality 1}' to log
But to evaluate that value you must use tokens in a Text comparison:
Code: [Select]
Begin Text Compare : [{TXT:personality {INT:personalityID}}] Equals '{INT:me}'
End Condition

For more advanced conditions, look into the "{EXP:}" token, on page 127 of VoiceAttackHelp.pdf

EDIT: Fixed text compare. Always caveats with these things ::)
« Last Edit: December 04, 2017, 11:30:41 AM by Pfeil »

DeeM77

  • Guest
Re: Use a variable in the Variable Name
« Reply #2 on: December 04, 2017, 07:33:26 AM »
Thank you Pfeil,

you save me a lot of work!!

Code: [Select]
Begin Small Integer Compare : [loop] Has Not Been Set
    Set small int (condition) [loop] value to 0
End Condition
Begin Small Integer Compare : [loop] Equals 0
    Set small int (condition) [loop] value to 1
    Start Loop While : [loop] Equals 1
        Set integer [personalityID] to [personalityID] plus 1
        Set integer [personality] to [personality] plus [{INT:personalityID}]
        Write '[Blue] personality {INT:personalityID}' to log
        Begin Text Compare : [{INT:personalityID}] Equals '12'
            Set small int (condition) [loop] value to 0
        End Condition
    End Loop
    Set integer [personalityID] value to [Not Set]
End Condition

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: Use a variable in the Variable Name
« Reply #3 on: December 04, 2017, 07:48:39 AM »
Quote
The only type of variable with a variable variable name that can be evaluated in a condition is "Text"(You'll notice the "Text" tab is the only one that accepts tokens as well as variable names).

I think this is an oversight.  I'm actually working down in conditions on some new stuff so I'll take a look.

DeeM77

  • Guest
Re: Use a variable in the Variable Name
« Reply #4 on: December 04, 2017, 09:41:48 AM »
I still need a little help.

I wrote a script below. But in the text compare the INT:id isn't working. If I replace it with a "1" it works fine.
What am I missing here?

Code: [Select]
Set Text [set 1 module] to 'module1'
Set integer [id] value to 1
Write '[Blue] my {INT:id}' to log
Begin Text Compare : [set {INT:id} module] Equals 'module1'
    Set small int (condition) [myself] value to 1
    Write '[Blue] you have set module {INT:id}' to log
End Condition
Set integer [id] value to [Not Set]

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Use a variable in the Variable Name
« Reply #5 on: December 04, 2017, 11:29:06 AM »
Try
Code: [Select]
Set Text [set 1 module] to 'module1'
Set integer [id] value to 1
Write '[Blue] my {INT:id}' to log
Begin Text Compare : [{TXT:set {INT:id} module}] Equals 'module1'
    Set small int (condition) [myself] value to 1
    Write '[Blue] you have set module {INT:id}' to log
End Condition
Set integer [id] value to [Not Set]

My mistake; When you use tokens in the condition, the contents of the "Variable Name / Token" field are passed to the token parser, and not interpreted as a variable name.

DeeM77

  • Guest
Re: Use a variable in the Variable Name
« Reply #6 on: December 04, 2017, 01:21:48 PM »
Thank you Pfeil. That did the trick!

DeeM77

  • Guest
Re: Use a variable in the Variable Name
« Reply #7 on: December 05, 2017, 09:38:11 AM »
Just 1 quick question which would make my life a lot easier.  ;D

Is it possible to use a Boolean Compare with a variable in the variable name? Or does anyone know a nice workaround?

Code: [Select]
Begin Boolean Compare : [Check {INT:id} module set] Equals True

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Use a variable in the Variable Name
« Reply #8 on: December 05, 2017, 02:54:43 PM »
"Text" is the only type that currently allows variable variable names, but because tokens return text values, you can use it for boolean compares as well:
Code: [Select]
Begin Text Compare : [{BOOL:Check {INT:id} module set}] Equals 'True'

The "{BOOL:}" token will return either "True", "False", or "Not Set", so you can compare that output to a static string of text.

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: Use a variable in the Variable Name
« Reply #9 on: December 07, 2017, 10:27:16 PM »
Quote
I think this is an oversight.  I'm actually working down in conditions on some new stuff so I'll take a look.

Thinking this was not an oversight...  without spending a lot of time on it (or digging through this forum), I think I'm not going to pursue this one just yet as the UI matches the underlying code. 

In other news, new beta coming soon ;)

DeeM77

  • Guest
Re: Use a variable in the Variable Name
« Reply #10 on: December 08, 2017, 09:43:30 AM »
 ;D Looking forward  :P

Oss133

  • Newbie
  • *
  • Posts: 21
Re: Use a variable in the Variable Name
« Reply #11 on: March 11, 2018, 05:46:59 AM »
Hi,

I am tring to do something similar but cannot get it to work. I want to be able to save a value for the same module in different Elite Dangerous ships.

So I am testing the idea on the Cargo Hatch. I am setting up two ships, 0 and 1, and then setting up a CH variable for each one and showing that one is on and one is off.

This is only being used as test.

Code: [Select]
Set integer [SHIP] value to 0
Write '[Blue] SHIP {INT:SHIP}' to log
Set Text [CH{SHIP}] to 'ON'
Write '[Blue] CH {TXT:CH{SHIP}}' to log
Set integer [SHIP] value to 1
Write '[Blue] SHIP {INT:SHIP}' to log
Set Text [CH{SHIP}] to 'OFF'
Write '[Blue] CH {TXT:CH{SHIP}}' to log
Set integer [SHIP] value to 0
Write '[Blue] SHIP {INT:SHIP}' to log
Write '[Blue] CH {TXT:CH{SHIP}}' to log
Write '[Blue] CH 0 {TXT:CH0}' to log
Write '[Blue] CH 1 {TXT:CH1}' to log

When I run this code I get these results.

SHIP 0
CH ON
SHIP 1
CH OFF
SHIP 0
CH OFF
CH 0 Not set
CH 1 Not set

When I reset the SHIP to 0 I expect CH to show as ON but it is OFF.

When I show what I believe the concatenated variables to be, CH0 and CH1, they are not set. I want to be able to save and retrieve these between sessions but until I understand what I am doing wrong I cannot proceed.

Any help would be gratefully received.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Use a variable in the Variable Name
« Reply #12 on: March 11, 2018, 08:08:45 AM »
Code: [Select]
CH{SHIP}should be
Code: [Select]
CH{INT:SHIP}
The way you have it set up now means you're creating a text value literally named "CH{SHIP}", with a value of "ON".

If you use the "{INT:}" token, you'll get the value that the integer value "SHIP" contains, meaning the text value will be named "CH1" or "CH0".

Oss133

  • Newbie
  • *
  • Posts: 21
Re: Use a variable in the Variable Name
« Reply #13 on: March 11, 2018, 10:43:23 AM »
Thank you!

It's obvious once you look properly!