Search code examples
arraysif-statementautohotkeytrayicon

AutoHotKey - Receive content from an array using a variables' integer as the index


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?


Solution

  • @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