Jump to content

Help with variable scope problem


Recommended Posts

Hi forum i`m working in a script that use boxes indexed into an 2d array that manage different properties. There are two consolewrite output that shows you a value stored in the [n][1] column of the array, one inside the function the other after the function, i don`t know why the function dont change that value. by default the value is 0, i use a function to change it to 1, i know that i can do that manualy typing $array[0][1] = 1, but i want to use a function because it have practical applications for the script

I left a snippet here.

Global $index
$hGui = GUICreate("DrumsLoops", 700, 300, -1, -1)
$Boxes = _CreateBoxes(100, 10, 590, 210, 8, 4, 40, 2)
GUISetState()

While True
    $Msg = GUIGetMsg()
    Switch $Msg
        Case -3
            Exit
        Case $Boxes[0][0] To $Boxes[UBound($Boxes) - 1][0]
            $index = _GetBoxIndexByMsg($Boxes, $Msg)
            _SetBoxStateByIndex($Boxes, $index, 1)
            ConsoleWrite($Boxes[$index][1] & @CRLF)
    EndSwitch
WEnd

; #FUNCTION# ====================================================================================================================
; Name ..........: _CreateBoxes
; Description ...:
; Syntax ........: _CreateBoxes($ix, $iy, $iWidth, $iHeight, $iItemsPerRow, $iRows, $iBoxSizes, $iPropertiesAmount)
; Parameters ....: $ix                  - X position of the controls in the GUI.
;                  $iy                  - Y position of the controls in the GUI.
;                  $iWidth              - Width.
;                  $iHeight             - Height.
;                  $iItemsPerRow        - Amount of items per row.
;                  $iRows               - Amount of Rows.
;                  $iBoxSizes           - Size of the boxes.
;                  $iPropertiesAmount   - Additional properties slots.
; Return values .: An Array
;                   [n][0] = ID of the box.
;                   [n][1] = State of the Box(Selected = 1).
;                   [n][2] = Position of the item in a row.
;                   [n][3] = Number of row of the n box.
;                   [n][4] = Color of unselected box(default color = 0x555555)
;                   [n][5] = Color of selected box(default color = 0x005500)
;                   [n][n] = Aditional properties slot you want
; Author ........: monoscout999
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _CreateBoxes($ix, $iy, $iWidth, $iHeight, $iItemsPerRow, $iRows, $iBoxSizes, $iPropertiesAmount)
    Dim $aLines[$iItemsPerRow * $iRows][6 + $iPropertiesAmount]
    If $iBoxSizes * $iItemsPerRow < $iWidth Then SetError(1, 0, "The iBoxSizes parameter is too big for the total width")
    If $iBoxSizes * $iRows < $iHeight Then SetError(2, 0, "The iBoxSizes parameter is too big for the total height")
    Local $ixSpace = (($iBoxSizes * $iItemsPerRow - $iWidth) / $iItemsPerRow + 1) * - 1
    Local $iySpace = (($iBoxSizes * $iRows - $iHeight) / $iRows + 1) * - 1
    Local $iAuxiliaryGuide = 0
    Local $iAuxiliaryySpace = $iySpace
    Local $iAuxiliaryGuideY = 1
    For $i = 0 To UBound($aLines) - 1
        $aLines[$i][0] = GUICtrlCreateGraphic($ix + ($ixSpace * ($iAuxiliaryGuide + 1)) + ($iBoxSizes * $iAuxiliaryGuide), $iy + $iySpace, $iBoxSizes, $iBoxSizes)
        GUICtrlSetBkColor(-1, 0x555555)
        $iAuxiliaryGuide += 1
        If $iAuxiliaryGuide = $iItemsPerRow Then
            $iySpace += $iAuxiliaryySpace + $iBoxSizes
            $iAuxiliaryGuide = 0
            $iAuxiliaryGuideY += 1
        EndIf
        $aLines[$i][1] = 0
        $aLines[$i][2] = $iAuxiliaryGuide + 1
        $aLines[$i][3] = $iAuxiliaryGuideY
        $aLines[$i][4] = 0x555555
        $aLines[$i][5] = 0x005500
    Next
    Return $aLines
EndFunc   ;==>_CreateBoxes
Func _GetBoxIndexByMsg($aBoxes, $iMsg)
    If Not IsArray($aBoxes) Then SetError(1, 0, "The aBoxes Parameter is not an array")
    For $i = 0 To UBound($aBoxes) - 1
        If $aBoxes[$i][0] = $iMsg Then Return $i
    Next
    SetError(2, 0, False)
EndFunc   ;==>_GetBoxIndexByMsg
Func _SetBoxStateByIndex($aBoxes, $iIndex, $iState)
    If Not IsArray($aBoxes) Then SetError(1, 0, "The aBoxes Parameter is not an array")
    If $iIndex > UBound($aBoxes) - 1 Then SetError(2, 0, "The iIndex Parameter is to big")
    If $iState > 1 Then SetError(3, 0, "Invalid State value, the state must be 1 or 0")
    $aBoxes[$iIndex][1] = $iState
    Switch $aBoxes[$iIndex][1]
        Case 0
            GUICtrlSetBkColor($aBoxes[$iIndex][0], $aBoxes[$iIndex][5])
        Case 1
            GUICtrlSetBkColor($aBoxes[$iIndex][0], $aBoxes[$iIndex][4])
    EndSwitch
    ConsoleWrite($aBoxes[$iIndex][1] & @CRLF)
    Return True
EndFunc   ;==>_SetBoxStateByIndex
Link to comment
Share on other sites

Variables used in a function only exist inside that function unless you declared them global, even then if you reuse a Global variable inside the calling function, as you did with this Func _SetBoxStateByIndex($aBoxes, $iIndex, $iState) code, it becomes a local variable. That's why you don't have to declare the variable names inside the calling function line, they're automatically declared as Local variables. What you SHOULD be doing is using a ByRef in front of the array variable in the Func _SetBoxStateByIndex routine, and giving it a different name in that routine like this Func _SetBoxStateByIndex(ByRef $aBoxes1, $iIndex, $iState) and change the variable names inside the function. Otherwise there's no point in calling the function with the array passed to it, you might as well just leave that variable out of the Func routine but that can lead to problems troubleshooting your code.

By the way, avoid the use of the Dim command when declaring variables, use Global for Globals and Local for locals, Dim isn't recommended to be used any longer.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH thank you, i never used ByRef before, this forces the function to use and modify the Array that i pass to that parameter, i don`t have a good english, but from what you write i undertand this: I shouldn´t use the same name of the Global Array in the Function parameter. is that correct?

Also i change the Dim as you recomend. thank you again.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...