1

I want to receive a string from an array using a variables' integer as the array index. But it is not working.

Attempt 1

; Suspended | 0 = No, 1 = Yes
global Suspended     := 0
global SuspendedMsg  := ["The script has been paused.","The script has been re-activated."]

Pause::
    Suspend
    if suspended = 0 ; If script is not suspended
    {
        TrayTip, Paused, SuspendedMsg[Suspended], 3
        Suspended++
    } else ; If it is suspended
    {
        TrayTip, Activated, SuspendedMsg[Suspended], 3
        Suspended--
    }
return

Attempt #1 will just display the string "SuspendedMsg[Suspended]" because I don't know where to set the variable indicator %. Even if I set it to SuspendedMsg[%Suspended%] it will either display [1] or [0].

Attempt 2

; Suspended | 0 = No, 1 = Yes
global Suspended      := 0
global SuspendedMsg   := ["The script has been paused.","The script has been re-activated."]
global SendSuspendMsg := SuspendedMsg[Suspended]

Pause::
    Suspend
    if suspended = 0 ; If script is not suspended
    {
        TrayTip, Paused, %SendSuspendMsg%, 3
        Suspended++
    } else ; If it is suspended
    {
        TrayTip, Activated, %SendSuspendMsg%, 3
        Suspended--
    }
return

Attempt #2 won't do as well, it doesn't even display any message. I tried fiddling arround with % inside the global SendSuspendMsg := SuspendedMsg[Suspended] variable but it won't do no good. Anyone care to help me out?

Dean
  • 301
  • 1
  • 3
  • 13

2 Answers2

0

Instead of TrayTip, Paused, SuspendedMsg[Suspended], 3 or TrayTip, Paused, SuspendedMsg[%Suspended%], 3, try

TrayTip, Paused, % SuspendedMsg[Suspended], 3

. TrayTip asks you for a

specify the message to display

which means as much as a String. So, variables names aren't handled as variables here, but as strings instead (as most of the times in commands). It would make sense to state TrayTip, Paused, %SuspendedMsg[%Suspended%]%, 3 , but you cannot nest variable's percent signs. So, we'll have to use the percent sign to force an expression:

Force an expression: An expression can be used in a parameter that does not directly support it (except OutputVar parameters) by preceding the expression with a percent sign and a space or tab. In [v1.1.21+], this prefix can be used in the InputVar parameters of all commands except the traditional IF commands (use If (expression) instead). This technique is often used to access arrays.

Concerning your second problem: I don't think Arrays can be declared like that, can they..? (but I'm not sure). Also see this short article. So I guess the problem lies within the 3rd line of your code, because the rest of it looks good to me

phil294
  • 10,038
  • 8
  • 65
  • 98
0

@Blauhim missed an important point, although his answer is mostly correct. First the Index in an Array when created like you did, always starts at 1, then proceeds to 2 etc, etc... So your code was flawed when you tried to use your Boolean variable to call to an index as a 0 Index does not exist (not to mention that you didn't force and Expression on that TrayTip Command).

; Set our variable to 1 why? Because we are going to use a Logical switch below.
Suspended     := 1
; This was correct format and I left it, although I removed Global's as they are not needed
SuspendedMsg  := ["The script has been paused.","The script has been re-activated."]

Pause::
    ; Suspend toggles each time it's called
    Suspend

    ; Here we are toggling the value of our variable using !
    ; We started with a 1 so that it would be correctly
    ;Changed to a 0 for the code below.
    suspended := !suspended

    ; Nothing changed here
    if suspended = 0 ; If script is not suspended
    {
        ; In order to pass an Array or Object or Expression to a Command you Force it
        ; using the a Percent Sign with a space on either side.
        ; Also note you were trying to use your Logical True/False 0 or 1 variable to         
        ; itterate. This didn't work because Array's always start with an Index of 1. 
        ; Below I've accounted for this by simply added a 1 to your suspended so it correctly 
        ; points to the Index in our Array.
        TrayTip, Paused, % SuspendedMsg[suspended + 1], 3

    } else ; If it is suspended
    {
        TrayTip, Activated, % SuspendedMsg[suspended + 1], 3        
    }
return
errorseven
  • 2,672
  • 2
  • 14
  • 20
  • Don't really get the part with "suspended := !suspended" but everything else worked. Also, I didn't know that every index starts with 1. So changing the "Suspended" value to 1 did the job for me here. Thank you both! :> – Dean Jul 19 '15 at 16:55
  • No worries, I'll be happy to expand on it. In AutoHotkey a Boolean value is 0 or 1 for False or True. Also the word True and False can be Assigned to a Variable and mean the same, they are interchangeable. The ! is a Logical-not which will Switch the value of the Variable from True to False or False to True, like a light switch. This shortens the code as before you needed two assignments using ++ or -- . There are other ways to write concise code, replace If statement with a Ternary. Check it out: http://ahkscript.org/docs/Variables.htm#Expression_Operators_in_descending_precedence_order – errorseven Jul 19 '15 at 18:03