Simple Load/Save Character Dialog Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Pheonix1987
Posts: 3
Joined: 08 Jul 2019, 13:52

Simple Load/Save Character Dialog

08 Jul 2019, 14:45

Hi all,

I'm a new user of Auto Hotkeys and I'm looking for some help with a simple Load/Save system I've added into a game I am making.

I've been hunting through the forums for a way to do things in my program, and on most occasions I have found a way to get things working without having to ask... but this one eludes me!

The characters (at the moment, for testing) only have 3 key variables: Pname,PClass, and PRace. I'm aware there are much easier ways to achieve this as it stands, but these files will eventually become much more complicated, and will be sent between machines by email, so ideally I need to have the save games in separate files.

For the example, these will be Name, Class and Race Respectively.

I have made a save file system that works ok:

Code: Select all

SaveChar: 
;Checks to see if character information has been added (vTest = 'None') and if not, returns to Opening Menu Gui
If (Pname = Test) and (PRace = Test) and (PClass = Test)
{
MsgBox, No Character Selected!
goto, OpenScr
}
;Warning that saving the character will overwrite existing, and go back to Opening Screen if no. 
MsgBox, 4, , This will overwrite an existing character with this filename. Are you sure?
IfMsgBox, No
	goto, OpenScr
;Deletes the existing file
FileDelete, %A_ScriptDir%\Program Files\Saved Characters\%PName%.ahk
;Creates the new save file as a .ahk
FileAppend,
(
PName = %Pname% 
PClass = %PClass%
PRace = %PRace%
)
, %A_ScriptDir%\Program Files\Saved Characters\%Pname%.ahk
Msgbox, Character Saved Successfully!
This generates the file Name.ahk:

Code: Select all

PName = Name 
PClass = Class
PRace = Race
The issue lies in trying to Load the File:

Code: Select all

LoadChar:
;Checks to ensure can overwrite if existing, if no return to Gui
MsgBox, 4, , This will overwrite the current character. Are you Sure? 
IfMsgBox, No
goto, OpenScr
;Prompt to select Character file
FileSelectFile, CharLoadFile , Options, %A_ScriptDir%\Program Files\Saved Characters, Select Your Character!, Documents (*.ahk)
;Reads the file, and adds to vCharLoadFileContents
FileRead, CharLoadFileContents, %CharLoadFile%
;Adds the text from the specified file path to temp.ahk
FileAppend, %CharLoadFileContents%, temp.ahk
;Runs Ahk2exe.exe to convert temp.ahk to an exe
RunWait, Ahk2exe.exe /in temp.ahk
;Runs temp.exe to add the new variables
RunWait temp.exe
;Deletes the temporary files
FileDelete, temp.ahk
FileDelete, temp.exe
;Adds other details for the new character
goto, Add%Prace%Stats
I debugged and saved the generated Temp.ahk file (below), and before becoming a .exe it contains the correct Variable setting commands, so I'm not sure what I am doing wrong? I'm sure it will be something simple I have missed ;)

Code: Select all

PName = Name
PClass = Class
PRace = Race
Edit - Should say the result really :?
The next stage of the process gives an error I added for when there are no details in the Pname, PClass and Prace parameters, so the program isn't seeing the loaded details.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Simple Load/Save Character Dialog

08 Jul 2019, 15:28

RunWait temp.exe
When temp.exe is done, the variables which were available in temp.exe are lost, I think.

You might be better off with Ini-files.
Pheonix1987
Posts: 3
Joined: 08 Jul 2019, 13:52

Re: Simple Load/Save Character Dialog

08 Jul 2019, 16:36

wolf_II wrote:
08 Jul 2019, 15:28
RunWait temp.exe
When temp.exe is done, the variables which were available in temp.exe are lost, I think.

You might be better off with Ini-files.
Hi Wolf,

Thank you for this, although I was hoping to avoid .ini files if possible, as they are complicated and I wanted to try and keep things simple :C

If the values are lost once the .exe is gone, is there anything I can put into the save files to transfer the values before it finishes?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Simple Load/Save Character Dialog

08 Jul 2019, 18:07

is there anything I can put into the save files to transfer the values before it finishes?
I'm not sure. I don't think so... there may be ways, but they would be more complicated than Ini-files.
Pheonix1987
Posts: 3
Joined: 08 Jul 2019, 13:52

Re: Simple Load/Save Character Dialog  Topic is solved

12 Jul 2019, 14:50

Soooo.... I managed to find a way to achieve this.

If you don't mind storing your data to what is essentially a .CSV and extracting it via Arrays, that is!

Code: Select all

;-------------------------------------------------------------------------------------------------------------
; 2d Load Character button 
LoadChar:
MsgBox, 4, , This will overwrite the current character. Are you Sure? 
IfMsgBox, No
goto, OpenScr
;Prompt to select Character file
FileSelectFile, CharLoadFile , Options, %A_ScriptDir%\Program Files\Saved Characters, Select Your Character!, Documents (*.ahk)
;Reads the file, and adds to Correct Variables
FileRead, LoadString, %CharLoadFile%
LoadArray:= StrSplit(LoadString,",")
PName := LoadArray[1]
PClass := LoadArray[2]
PRace := LoadArray[3]
;Adds the new character with the new details
goto, Add%Prace%Stats
;-------------------------------------------------------------------------------------------------------------
; 2e Save Character button 
SaveChar: 
If (Pname = Test) and (PRace = Test) and (PClass = Test)
{
MsgBox, No Character Selected!
goto, OpenScr
}
MsgBox, 4, , This will overwrite an existing character with this filename. Are you sure?
IfMsgBox, No
	goto, OpenScr
FileDelete, %A_ScriptDir%\Program Files\Saved Characters\%PName%.ahk
FileAppend,(%PName%,%PClass%,%PRace%),*%A_ScriptDir%\Program Files\Saved Characters\%Pname%.ahk
Msgbox, Character Saved Successfully!
return
goto, OpenScr
;-------------------------------------------------------------------------------------------------------------

Return to “Ask For Help”

Who is online

Users browsing this forum: AdamvsMaximvs, AHKStudent, Bing [Bot], Bodhi, boiler, davidjmorin, Gushou, hd0202, keylo, mikeyww, mmmax and 44 guests