Opening an external file in an AGS game?

Started by , Tue 30/03/2004 21:12:59

Previous topic - Next topic

ElectricMonk

#20
Interesting... I'm trying to write a simple "save - run another exe - restore" test game, and as soon as I run the .bat file, it tries to run each program after another without waiting for the previous one to end, which also means by the time the first AGS game starts, the "check.txt" is already deleted again.
Is that because I'm not working in "real" MS-DOS mode?

EDIT: Even worse - The number I'm trying to read from the text file gets read wrong.

I use (in the first room, AFTER fadein for convenience right now, so I can read the result):

int checknumber;
int handle = FileOpen ("check.txt", FILE_READ);
checknumber = FileReadRawInt(handle);
FileClose (handle);
string buffer;
StrFormat (buffer, "%d", checknumber);
Display(buffer);  // show the number you've read in the file
if (checknumber ==1) RestoreGameSlot(30); // if it's 1, restore.

The file "check.txt" at the moment contains nothing else than the number "1" in pure ASCII format. And yes, it's in the "Compiled" folder.

Apparently this whole idea is not as easy as I had previously thought. Any ideas, anyone?


EDIT #2:

At least I solved the "stupid batch file, wait until the first program has finished!" problem now:

echo 0 > check.txt
start /wait testrun.exe
start /wait othergame.exe
del check.txt
echo 1 > check.txt
start /wait testrun.exe
del check.txt

wombat

#21
Ok, ive made sure that everything is in the correct place, but the same thing is still happening. What you're telling me should happen is that I should be able to double click on the batch file, it will open up my ags game, when the ags game quits it will open up the other game, and when that quits it should re-open my ags game.

Well thats not happening. ive written the exact code that i showed in the previous post, and all thats happening is an ms-dos window flashes open and closed once, and then nothing happens. I dont even get either program opening up. When i check the batch file all that appears is a 0.

Can anyone find an error in my scripting?

[EDIT]

after just reading the last edit from electric monk, i am even more confused. Should I be putting that kind of script into my game? And also, what can you tell me about file handles, i seem to have not found that bit in the tutorial...

finally, can i make whether you win or lose the game (not ags game) affect what happens when you come back into ags?

[EDIT #2]
Could electric monk (or anyone else who happens to see this post) write me a script for my game that would open the batch file (like electric monk did in last post except using "spygame.exe" as the ags game, "shootergame.exe" as the other game and "check.bat" as the batch file. i am confused

strazer

Quoteas soon as I run the .bat file, it tries to run each program after another without waiting for the previous one to end

Using CALL will wait for a batch file to finish before continuing, I don't know if it works with exe's:

echo 0 > check.dat
call spygame.exe
call shootergame.exe
del check.dat
echo 1 > check.dat
call spygame.exe
del check.dat

?

ElectricMonk

#23
Quote from: wombat on Sat 03/04/2004 20:48:52
ive written the exact code that i showed in the previous post, and all thats happening is an ms-dos window flashes open and closed once, and then nothing happens. I dont even get either program opening up. When i check the batch file all that appears is a 0.

Can you put the batch file up on a website somewhere so I can take a look at it? Normally, you should be able to open it with notepad and see your script, not just a 0.


Quote
after just reading the last edit from electric monk, i am even more confused. Should I be putting that kind of script into my game?

Something like that, anyway. I'm not sure about it myself.

Quote
And also, what can you tell me about file handles, i seem to have not found that bit in the tutorial...

I'm pretty much a newbie myself, I just took the info from the help file and changed the names of the variables. No idea why you need that "handle" stuff.

Quote
finally, can i make whether you win or lose the game (not ags game) affect what happens when you come back into ags?

That depends whether your other game is able to export some sort of text file that, again, must then be read by AGS in order to make that decision.


EDIT:
Strazer: "call file.exe" doesn't work, at least not in my Win98 environment. It still starts all of them at once.
"start /wait file.exe" works though.

ElectricMonk

No disrespect, but I don't think this thread should be archived yet as it's not yet resolved.

When I use
Quote
int checknumber;
int handle = FileOpen ("check.txt", FILE_READ);
checknumber = FileReadRawInt(handle);
FileClose (handle);
string buffer;
StrFormat (buffer, "%d", checknumber);
Display(buffer); // show the number you've read in the file
to read the number in a text file called "check.txt" that only contains the number "1", I get "-1" displayed as a result. The same happens when the file contains "0".
Anyone know why?

foz

Can i ask why you want to launch a gamefactory game while playing an ags game...?

wombat

<tee hee>

the reason is that in ags i cant find any way to create a ... kind of... 'minigame' that i can make the user play in order to get a certain item. If you know any more about this than i do, please reply... or it may be on a different thread...

ElectricMonk

#27
Also, I'm sure many people will find it running other .exes from AGS handy - for flash animations, for example.

Personally, the whole "reading stuff from external files" is an important issue for me as it may well crop up in future projects.

Pumaman

FileReadRawInt reads a raw 32-bit integer from the file, not a number in ASCII representation. The easiest way to do what you want would be to write the letter 'A' to the file, then use FileReadRawChar to check it.

ElectricMonk

Thank you, now it's working! ;D

To sum up:

The start.bat file that goes into your Compiled folder:
Quote
echo A > check.txt
start /wait agsgame.exe
start /wait othergame.exe
del check.txt
echo B > check.txt
start /wait agsgame.exe
del check.txt

(BTW, this means you manually need to put a "check.txt" with the proper contents in your Compiled folder if you want to test the game from inside the AGS editor, since the .bat file deletes "check.txt" at the end.)

The action to quit the game and save the position at the end of the first run:
Quote
SaveGameSlot(30, "inbetween");
QuitGame(0);


In the Global Script, in function game_start(), the script to make AGS restore the position in the second run:
Quote
{
string checkchar;
int handle = FileOpen ("check.txt", FILE_READ);
StrFormat (checkchar, "%c", FileReadRawChar (handle));
FileClose (handle);
if (StrCaseComp(checkchar, "B") ==0) RestoreGameSlot(30);
}

(Which only leaves me with the newbyish question: What are raw 32-bit integer numbers, and how do I convert them to the decimal system?) :-[

mätzyboy

#30
A raw 32 bit integer is an integer (no decimals) with 32 bits of data (possibility to store numbers from 0 to 2^32).

Your actual question is what is ASCII. ASCII is a standard for representing alphabetical characters using numbers. For example the letter A is represented as the number 65 in decimal numbers or 1000001 in binary numbers. Check http://www.neurophys.wisc.edu/www/comp/docs/ascii.html for a complete translation guide...

With a little thought you'll see that converting ASCII to numbers (given that you KNOW you will be converting a number between 0 and 9 NOT a letter) is to subtract 42 from the raw integer containing the imported asciicode...

Hopefully I managed to make things a little more clear  :)

Pumaman

Yeah, the FileReadRawInt function is designed for reading binary files output by other programs. It's not useful for reading in text files.

Hollister Man

I have constructed a *semi* functional AGS launcher that currently runs two different AGS games alternating, but isn't quite working right.  I am trying to make it as transparent as possible, but it will also have a GUI available. 
With luck, it will be working sometime this weekend.  I'm just having trouble figuring out why it is still running the batch file even though I've overwritten it already. :)

We're planning on including it with DQ3, for use with the sub-game structure.  This way, all units can be viewed and dissected separately, but can still be played as a whole. 
That's like looking through a microscope at a bacterial culture and seeing a THOUSAND DANCING HAMSTERS!

Your whole planet is gonna blow up!  Your whole DAMN planet...

Gankkizz

Can you read a file in batch?

First run a batch file, write "0" to file.dat, run an AGS game, write "1" to the file, exit, read the file in batch and run another game if the number has been changed...

Snarky

Quote from: wombat on Sat 03/04/2004 20:48:52
all thats happening is an ms-dos window flashes open and closed once, and then nothing happens. I dont even get either program opening up. When i check the batch file all that appears is a 0.

Wombats, I know why this is happening. Let's have a look at your script:

Quote from: wombat on Thu 01/04/2004 20:45:42
echo 0 > check.bat
spygame.exe
shootergame.exe
del check.bat
echo 1 > check.bat
spygame.exe
del check.bat

I bet you stored all of this in a file called "check.bat". What happens is that when you run it, on the very first line, "echo 0 > check.bat", it writes "0" to the file "check.bat"... itself! So all the following commands are overwritten, and nothing more happens.

Try changing the commands to say "echo 0 > check.txt" (or "check.dat", or whatever) etc., and naming your batchfile "rungame.bat". That way you won't be confused.

SMF spam blocked by CleanTalk