Jump to content

autoit noob question


Recommended Posts

As the topic states to some of you this might be a silly question but to me its a brick wall in my way cause i just begun learning Autoit.

Now i would like to know how to see information from one function, in another function. eg.

Func DECLARATIONS()
$var = GUICtrlRead($txtbox)
$var0 = GUICtrlRead($txtbox0)
$var1 = GUICtrlRead($txtbox1)
$var1 = GUICtrlRead($txtbox1)
EndFunc

Func INIWRITE()
IniWrite("Save.ini", $var, "Name", $var0)
IniWrite("Save.ini", $var, "Address", $var1)
IniWrite("Save.ini", $var, "Age", $var2)
EndFunc

i want the INIWRITE() function to be able to see the variables i declared in the DECLARATIONS() Function. can someone help me please?

Link to comment
Share on other sites

In Autoit variables in a function are local, you must declare them global.

u mean like this?

Func DECLARATIONS()
Global $var = GUICtrlRead($txtbox)
Global $var0 = GUICtrlRead($txtbox0)
Global $var1 = GUICtrlRead($txtbox1)
Global $var2 = GUICtrlRead($txtbox2)
EndFunc

Func INIWRITE()
IniWrite("Save.ini", $var, "Name", $var0)
IniWrite("Save.ini", $var, "Address", $var1)
IniWrite("Save.ini", $var, "Age", $var2)
EndFunc

cause i still get the errors:

"WARNING: $var: possibly used before declaration."

"WARNING: $var0: possibly used before declaration."

"WARNING: $var1: possibly used before declaration."

"WARNING: $var2: possibly used before declaration."

Link to comment
Share on other sites

This is an example of what the code is. its not the actual progrm i'm writing but it is close in structure.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Main = GUICreate("Test", 187, 97, 192, 124)
$txtbox = GUICtrlCreateInput("", 0, 0, 121, 21)
GUICtrlSetTip(-1, "Name")
$txtbox0 = GUICtrlCreateInput("", 0, 24, 121, 21)
GUICtrlSetTip(-1, "Sex")
$txtbox1 = GUICtrlCreateInput("", 0, 48, 121, 21)
GUICtrlSetTip(-1, "Address")
$txtbox2 = GUICtrlCreateInput("", 0, 72, 121, 21)
GUICtrlSetTip(-1, "Age")
$Button1 = GUICtrlCreateButton("Save", 128, 24, 51, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            INI_WRITE()
    EndSwitch
WEnd

Func DECLARATIONS()
    Global $var = GUICtrlRead($txtbox)
    Global $var0 = GUICtrlRead($txtbox0)
    Global $var1 = GUICtrlRead($txtbox1)
    Global $var2 = GUICtrlRead($txtbox2)
EndFunc

Func INI_WRITE()
    IniWrite("Save.ini", $var, "Sex", $var0)
    IniWrite("Save.ini", $var, "Address", $var1)
    IniWrite("Save.ini", $var, "Age", $var2)
EndFunc

Func INI_WRITE2()
    IniWrite("Save2.ini", $var, "Sex", $var0)
    IniWrite("Save2.ini", $var, "Address", $var1)
    IniWrite("Save2.ini", $var, "Age", $var2)
EndFunc
Edited by Tiboi
Link to comment
Share on other sites

Declare your global variables at the top of your script and then whenever you need to use them just call the populateVariables function to make sure they have the newest infomation.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $var, $var0, $var1, $var2

$Main = GUICreate("Test", 187, 97, 192, 124)
$txtbox = GUICtrlCreateInput("", 0, 0, 121, 21)
GUICtrlSetTip(-1, "Name")
$txtbox0 = GUICtrlCreateInput("", 0, 24, 121, 21)
GUICtrlSetTip(-1, "Sex")
$txtbox1 = GUICtrlCreateInput("", 0, 48, 121, 21)
GUICtrlSetTip(-1, "Address")
$txtbox2 = GUICtrlCreateInput("", 0, 72, 121, 21)
GUICtrlSetTip(-1, "Age")
$Button1 = GUICtrlCreateButton("Save", 128, 24, 51, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            populateVariables()
            INI_WRITE()
    EndSwitch
WEnd

Func populateVariables()
    $var = GUICtrlRead($txtbox)
    $var0 = GUICtrlRead($txtbox0)
    $var1 = GUICtrlRead($txtbox1)
    $var2 = GUICtrlRead($txtbox2)
EndFunc

Func INI_WRITE()
    IniWrite("Save.ini", $var, "Sex", $var0)
    IniWrite("Save.ini", $var, "Address", $var1)
    IniWrite("Save.ini", $var, "Age", $var2)
EndFunc

Func INI_WRITE2()
    IniWrite("Save2.ini", $var, "Sex", $var0)
    IniWrite("Save2.ini", $var, "Address", $var1)
    IniWrite("Save2.ini", $var, "Age", $var2)
EndFunc
Link to comment
Share on other sites

is there a shorter way to write this?

Func INI_WRITE()
    IniWrite("Save.ini", $var, "Sex", $var0)
    IniWrite("Save.ini", $var, "Address", $var1)
    IniWrite("Save.ini", $var, "Age", $var2)
EndFunc
Link to comment
Share on other sites

Shorter? You mean you have many more variables to write?

Func INI_WRITE()
    IniWrite("Save.ini", $var, "Sex", $var0)
    IniWrite("Save.ini", $var, "Address", $var1)
    IniWrite("Save.ini", $var, "Age", $var2)
EndFunc

i mean like writing (IniWrite("Save.ini", $var,) once, then add the rest of the information instead of having to write it 3 times

Link to comment
Share on other sites

  • Developers

Func INI_WRITE()
    IniWrite("Save.ini", $var, "Sex", $var0)
    IniWrite("Save.ini", $var, "Address", $var1)
    IniWrite("Save.ini", $var, "Age", $var2)
EndFunc

i mean like writing (IniWrite("Save.ini", $var,) once, then add the rest of the information instead of having to write it 3 times

In SciTE use Ctrl+D to duplicate the line. Other than that I cannot see your issue.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

would this work, if you didnt want to use globals?

_INIWRITE(DECLARATIONS())

Func DECLARATIONS()
Local $aVars[4]
$aVars[0] = GUICtrlRead($txtbox)
$aVars[1] = GUICtrlRead($txtbox0)
$aVars[2] = GUICtrlRead($txtbox1)
$aVars[3] = GUICtrlRead($txtbox1)
Return $aVars
EndFunc

Func _INIWRITE($array)
IniWrite("Save.ini", $array[0], "Name", $var0)
IniWrite("Save.ini", $array[1], "Address", $var1)
IniWrite("Save.ini", $array[2], "Age", $var2)
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Reply to " monoceres"

Maybe using an 2D array?

Local $array[3][2]=[["sex","male"],["age","undefined"],["address","home"]]
For $i = 0 To Ubound($array)
 IniWrite("save.ini","test",$array[$i][0],$array[0][1])
Next

i'll try this out thank you.

reply to "JohnOne"

would this work, if you didnt want to use globals?

_INIWRITE(DECLARATIONS())

Func DECLARATIONS()
Local $aVars[4]
$aVars[0] = GUICtrlRead($txtbox)
$aVars[1] = GUICtrlRead($txtbox0)
$aVars[2] = GUICtrlRead($txtbox1)
$aVars[3] = GUICtrlRead($txtbox1)
Return $aVars
EndFunc

Func _INIWRITE($array)
IniWrite("Save.ini", $array[0], "Name", $var0)
IniWrite("Save.ini", $array[1], "Address", $var1)
IniWrite("Save.ini", $array[2], "Age", $var2)
EndFunc

thanks for your help but this is way beyond me. only started learning 3 nights ago. i know nothing about arrays. my app comming along great tho :idea: Edited by Tiboi
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...