[Beginner Q] Save/Load Variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Pharma
Posts: 26
Joined: 13 Dec 2016, 18:14

[Beginner Q] Save/Load Variables

19 Jan 2018, 14:04

Edit: Put in everything. I'll be converting it to .json soon

Hey all,

Very beginner question.
I'm looking to save a few variables to a .txt file (stuff in blue) and load them.
So my script is saving/loading the following: vAcct, vMsgOp, vMsgHK, vGameName, vGameOp, vGameHK

Code: Select all

; MAPPING	
	`::Suspend
	RAlt::Reload

; SCRIPT	
	RCtrl::
			Gui 1:Default
			Gui, Add, GroupBox, Section h170 w200 ym+5 +Center, Load/Input then use Hotkeys
			
			Gui, Add, Text, x18 y38, Name:
			Gui, Add, Edit, x75 y35 vAcct 				
			Gui, Add, DDL, x75 y58 w60 vMsgOp Choose1,   |Ctrl|Alt|Shift
			Gui, Add, Edit, x138 y58 w57 h20 vMsgHK Right, PgDn 
			
			Gui, Add, Text, x18 y98, Game:
			Gui, Add, Edit, x75 y93 vGameName 			
			Gui, Add, DDL, x75 y115 w60 vGameOp Choose1,   |Ctrl|Alt|Shift			
			Gui, Add, Edit, x138 y115 w57 h20 vGameHK Right, PgUp
			
			Gui, Add, Button, x75 y150, Save
			Gui, Add, Button, x115 y150, Load
			
			Gui, Show, AutoSize Center, D2Faster 
	
			Return 
			
							
	ButtonSave:	
			Gui, Submit, NoHide
			FileDelete, %A_ScriptDir%\Test.txt
			FileAppend, %Acct%`n%MsgOp%`n%MsgHK%`n%GameName%`n%GameOp%`n%GameHK%, %A_ScriptDir%\Test.txt

			Return
			
	ButtonLoad:		
			FileReadLine, Acct, %A_ScriptDir%\Test.txt, 1
			FileReadLine, MsgOp_Input, %A_ScriptDir%\Test.txt, 2			
			FileReadLine, MsgHK, %A_ScriptDir%\Test.txt, 3
			FileReadLine, GameName, %A_ScriptDir%\Test.txt, 4
			FileReadLine, GameOp_Input, %A_ScriptDir%\Test.txt, 5
			FileReadLine, GameHK, %A_ScriptDir%\Test.txt, 6	
			GuiControl, , Acct, %Acct%
			GuiControl, , MsgHK, %MsgHK%
			GuiControl, , GameName, %GameName%
			GuiControl, , GameHK, %GameHK%
			GuiControl, ChooseString, MsgOp, %MsgOp_Input%
			GuiControl, ChooseString, GameOp, %GameOp_Input%			
			Return
				
Last edited by Pharma on 19 Jan 2018, 15:47, edited 2 times in total.
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: [Beginner Q] Save/Load Variables

19 Jan 2018, 14:38

Any method saving and loading to file will suffice, as long you can format it to later parse it back when loading. Inbuilt solution is ini (IniRead & IniWrite), but I suggest JSON - this way you can save arrays as well.

To get variable content from GUI use GuiControlGet.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: [Beginner Q] Save/Load Variables

19 Jan 2018, 15:21

Regarding the post you deleted:

Code: Select all

#include <JXON>

my_config_filename := "config.json"

; Saving to file
my_todays_menu := 
(Join QC
{
	"vegetables":
	[
		"apple",
		"banana",
		"carrot"
	],
	"meat":
	[
		"kiełbasa",
		"ham"
	],
	"fluid": "milk"
}
)
; Test
MsgBox % my_todays_menu["vegetables", 2]	; returns: banana

stringified_array := JXON_Dump(my_todays_menu)	; turning the array into JSON string
FileDelete, % my_config_filename
FileAppend, % stringified_array, % my_config_filename, UTF-8


; Clean up before next test
my_todays_menu := ""	; removing variable just to be sure
MsgBox % my_todays_menu["vegetables", 2]	; see? it's gone



; Loading from file
FileRead, file_content, % my_config_filename
my_new_array := JXON_Load(file_content)

; Test
MsgBox % my_new_array["vegetables", 2]	; returns: banana (again!)
Ensure to put JXON library into lib folder within AHK's directory.

That's how config.json (the file saved by code above) looks like:

Code: Select all

{"fluid":"milk","meat":["kie\u0142basa","ham"],"vegetables":["apple","banana","carrot"]}
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
Pharma
Posts: 26
Joined: 13 Dec 2016, 18:14

Re: [Beginner Q] Save/Load Variables

19 Jan 2018, 15:30

SirRFI wrote:Regarding the post you deleted:

Code: Select all

#include <JXON>

my_config_filename := "config.json"

; Saving to file
my_todays_menu := 
(Join QC
{
	"vegetables":
	[
		"apple",
		"banana",
		"carrot"
	],
	"meat":
	[
		"kiełbasa",
		"ham"
	],
	"fluid": "milk"
}
)
; Test
MsgBox % my_todays_menu["vegetables", 2]	; returns: banana

stringified_array := JXON_Dump(my_todays_menu)	; turning the array into JSON string
FileDelete, % my_config_filename
FileAppend, % stringified_array, % my_config_filename, UTF-8


; Clean up before next test
my_todays_menu := ""	; removing variable just to be sure
MsgBox % my_todays_menu["vegetables", 2]	; see? it's gone



; Loading from file
FileRead, file_content, % my_config_filename
my_new_array := JXON_Load(file_content)

; Test
MsgBox % my_new_array["vegetables", 2]	; returns: banana (again!)
Ensure to put JXON library into lib folder within AHK's directory.

That's how config.json (the file saved by code above) looks like:

Code: Select all

{"fluid":"milk","meat":["kie\u0142basa","ham"],"vegetables":["apple","banana","carrot"]}
I will convert my current export/import to .json at the end.
This is much appreciated.

Currently, I'm stuck on getting dropdown list to understand the imported text.

Code: Select all

			
			if (MsgOp_Input = Alt)
				{
					GuiControl, Choose, MsgOp, 2 	
				}
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: [Beginner Q] Save/Load Variables

19 Jan 2018, 16:43

i am uncertain what your current issue is, but perhaps this will help:

Code: Select all

selected_choice := 3

Gui, Add, DDL, x75 y115 w60 vGameOp Choose%selected_choice% AltSubmit, % "|Ctrl|Alt|Shift"	; "AltSubmit" causes to return position from DDL instead of it's content
Gui, Show

F9::
GuiControlGet, outputvar,, GameOp
MsgBox % outputvar
return
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Leli196, Rohwedder and 310 guests