1.

Solve : Game saving trouble?

Answer»

Okay, hello! New here, hope you can help.

Anywho, sort of new to batch, I can do STUFF like giving the user a selection of actions and such. My problem is that I'm making a story-line game adventure thing. And I want to save the game occasionally through the story. I.e. if you didn't bother to go down the first path at the start of the game and find the pouch or not, which will affect something later in the game, the save checkpoint is somewhere in between. What it will affect I don't know I've only really just started. I've seen people save stuff before in batch games so I know it's possible. I made a test script here for saving and loading:
Code: [Select]@echo off
echo initializing....
mkdir %~dp0\testfolder
set /p testvar= Enter test variable here:
echo Will now save your variable to file.
echo %testvar% > "%~dp0\testfolder\testvar.txt"
echo Will now set active variable to random.
set testvar= %random%
pause
echo variable is now %testvar%
echo Will now load variable frim file.
set testvar < "%~dp0\testfolder\testvar.txt"
pause
apparently the %~dp0 makes it the drive, the path of the file and name of running file the location, but this seemed to glitch out, making a folder called 'stuff' (the parent folder of current folder is called 'game stuff') with a subfolder 'My' ('My game' is the folder with the batch in.) also a folder called %mylastname%. and the console window said something about problems with the folder C:\Users\%myfirstname%(confidentiality). which doesn't exist, plus some others of similar weirdness.
Possibly because of issues with security? Tried running as admin, it just didn't make the folders and obviously, couldn't find them when calling for them.
Any help?
Or some (detailed but simple) explanation of how to do this some other simpler way? like all in one file?

Thanks anyone who can help!! Quote from: gogglebot on December 07, 2012, 09:48:32 AM

mkdir %~dp0\testfolder

Quote
apparently the %~dp0 makes it the drive, the path of the file and name of running file the location, but this seemed to glitch out, making a folder called 'stuff' (the parent folder of current folder is called 'game stuff') with a subfolder 'My' ('My game' is the folder with the batch in.) also a folder called %mylastname%. and the console window said something about problems with the folder C:\Users\%myfirstname%(confidentiality). which doesn't exist, plus some others of similar weirdness.
Possibly because of issues with security? Tried running as admin, it just didn't make the folders and obviously, couldn't find them when calling for them.
Any help?
Or some (detailed but simple) explanation of how to do this some other simpler way? like all in one file?

Thanks anyone who can help!!

Standard variable MODIFIERS documented in the FOR help (type FOR /? at the prompt)


%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only

These work not only for FOR metavariables but also batch parameters %0 (which is special, being the batch file itself) and %1 to %9 (passed parameters)

So you can see that %~dp0 is going to expand to the combination of:

%~d0 the drive letter (with colon) on which the batch script is located (NOT necessarily the current directory)
%~p0 the folder path to the batch script is located

I believe the issue you have is not security, it is spaces in the path. If the batch script is located in a folder whose path has any spaces, you have to use quotes so try mkdir "%~dp0\testfolder"

I see you USED quotes later on when echoing to testvar.txt



90% fixed! Stupid mistake that was!

Problem now is that it doesn't read the file and just comes out with the random number again.
The random number was just to test if it WOULD read the old one from the file and to make sure it wasn't just repeating what had just been entered instead of the file.

Now I see that it doesn't read the file.

Help? thanks again!This is one way to read line one of a file.

Code: [Select]set /p "testvar="< "%~dp0\testfolder\testvar.txt" >nulChanged a bit. Somehow more broken than previous.
code:
Code: [Select]@echo off
echo initializing....
mkdir "%~dp0\testfolder"
set /p testvar = Enter test variable here:
echo Will now save your variable to file.
echo %testvar% > "%~dp0\testfolder\testvar.txt"
echo Will now set active variable to random.
set testvar = %random%
echo variable is now %testvar%
echo Will now load variable from file.
pause
set "testvar" < "%~dp0\testfolder\testvar.txt" >nul
echo variable was: %testvar%
pause

The result:

initializing....
Enter test variable here:sdfvgbh
Will now save your variable to file.
Will now set active variable to random.
variable is now //Noteing that it isn't displaying the random number.
Will now load variable from file.
Press any key to continue . . .
variable was: //Also blank
Press any key to continue . . .


Oh dear....
Remember if anyone knows an alternate way to save stuff to file and then read it when the game is next run tell me :L
Just need it explaining well and simply. I posted just before you. You aren't reading the file correctly, and I posted one method.

set testvar=and
set testvar =two

are two different variables.So, what would be the correct code? I inserted your line you posted and put spaces between the ='s and such. that's what I posted just then. set testvar=123
echo %testvar%

Quote from: gogglebot on December 07, 2012, 12:56:25 PM
I ... put spaces between the ='s

This is what you should not do! Batch scripting language does not ignore spaces, unlike many other languages.

Alright, Salmon you seem to better explain things rather than throw bits of code at me. What lines and such specifically need changing?

Thanks@echo off
echo initializing....
mkdir "%~dp0\testfolder"
set /p testvar=Enter test variable here:
echo Will now save your variable to file.
echo %testvar% > "%~dp0\testfolder\testvar.txt"
echo Will now set active variable to random.
set testvar=%random%
echo variable is now %testvar%
echo Will now load variable from file.
pause
set /p testvar= < "%~dp0\testfolder\testvar.txt"
echo variable was: %testvar%
pause

Quote from: gogglebot on December 07, 2012, 01:18:59 PM
Alright, Salmon you seem to better explain things rather than throw bits of code at me.

You, my friend, didn't say thanks to me for either post.

If you want to learn to code, then expect to get lines of code in reply. It is up to you to study them.


Quote from: foxidrive on December 07, 2012, 02:00:28 PM
You, my friend, didn't say thanks to me for either post.

If you want to learn to code, then expect to get lines of code in reply. It is up to you to study them.

I agree 100% with foxidrive.
You can save one or more variable name/value pairs, separated by an equals sign, to one file (or create the file manually in an editor, or make a script generate them) and use FOR to retrieve them.

c:\>echo animal=cat > datastore.txt

c:\>echo colour=red >> datastore.txt

c:\>echo country=Russia >> datastore.txt

c:\>for /f "delims=" %A in (datastore.txt) do @set %A

c:\>echo %animal%
cat

c:\>echo %colour%
red

c:\>echo %country%
Russia

This was done at the prompt; in the FOR line use double percent signs (e.g. %%A instead of %A) in a batch script.

This is what datastore.txt contains

animal=cat
colour=red
country=russia

Handy tip: FOR skips lines starting with a semicolon.



Actually the final line of datastore.txt is now

country=Russia

The name of Russia correctly starting with a capital R. (I edited the script but forgot to correct the earlier version I posted before)



Discussion

No Comment Found