[SOLVED] Brightness being saved on game reset...

Started by Snake, Tue 19/02/2008 14:17:09

Previous topic - Next topic

Snake

Hey, guys,

I can't do this by myself, I've tried the last couple of days and so I've made myself post for help.

How can I make the brightness (gamma) stay where the player puts it so when the game resets they don't have to do it again.

Even better would be to have it save permanently so, if they ever decide they want to play it again, it'll be where they left it.

Thanks in advance,


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

DoorKnobHandle

Use the File-functions to create a file (maybe called "config.cfg" or something) that stores the int-value of brightness the user chooses and whenever he changes it and starts playing the game, the game stores the value in the file. When the game loads, it loads the old value from the file.

Tell us if you need help working with files, but you should try it using the manual first, it's not that hard with the given examples in there.

Snake

#2
Thank you for responding,
Yes, I need help with the files.

I know how to write to a file and that's it, I did it for the easter eggs, but how do I store the gamma's value?

I've tried this:
I know I'm very off, but, bare with me... please.
Yes, the manual tells you what it does and gives an example piece of code, but it doesn't go any further than that. I don't even know where to start. So here's what I did...

I put this into the repeatedly exec in the global script:
Code: ags

int bright = System.Gamma;
File *mark = File.Open("Brightness.cfg", eFileWrite);
mark.WriteInt(bright);
mark.Close();

I put this into the game start section:
Code: ags

int bright;
File *fmark = File.Open("brightness.txt", eFileRead);
bright = fmark.ReadInt();
fmark.Close();


It gave me an error, which at least I understood, that told me that the brightness.cfg file wasn't there to be opened.

So I changed it to brightness.txt and created the file and saved it in the compiled folder.
Now it says, "FileReadInt: File read back in wrong order".

I have no idea what I'm doing. I need a little boost :)
Or a big boost.

Thanks in advance,


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

GarageGothic

#3
Well, first of all, in the code you're writing to "Brightness.cfg" and reading from "brightness.txt". But I assume that's from your fix.

Instead of putting the write to file code in your repeatedly_execute script (which will slow down your game), just put it in the code for closing the options menu where the player changes the gamma. There's no need to rewrite the file unless the setting has changed, right?

The reading back code should have a check for whether the file exists or not, since it will be missing the first time the game is run, unless you include it in your distribution. Like so:

Code: ags

File *fmark = File.Open("brightness.txt", eFileRead);
if (fmark != null) {
   System.Gamma = fmark.ReadInt();
   fmark.Close();
   }


What do you mean that you "created" the file? That you created an empty file using notepad or similar? If so, it's no wonder you get an error. Unless you use the ReadRaw functions, only try to read back files created by AGS itself.

Edit: You should put a similar null check for the writing code, although there's very few circumstances (such as a full drive or a protected folder) that could cause an error.

Snake

#4
I'm ready to pull my hair out :-\

This is the code for when the game is run (initial room before SPE Presents - setting up transitions and such...):
Code: ags

File *fmark = File.Open("Brightness.cfg", eFileRead);
if (fmark == null) {
  System.Gamma = 100;
  fmark.Close();
  }
else if (fmark != null) {
  System.Gamma = fmark.ReadInt();
  fmark.Close();
  }


It was working until I deleted the brightness.cfg file to test if it would revert to the gamma being at 100%.

It's now giving me this error, "File read back in wrong order".

I tried taking out the else and just leaving the if, that didn't work, I tried switching the ifs around, still the same error messege.

The line of code that it is refering to is:
Code: ags
System.Gamma = fmark.ReadInt();


In responce to your previous questions:
QuoteWell, first of all, in the code you're writing to "Brightness.cfg" and reading from "brightness.txt". But I assume that's from your fix.
Yes, I copied the newest one by accident. They were both .txt at the time.

QuoteInstead of putting the write to file code in your repeatedly_execute script (which will slow down your game), just put it in the code for closing the options menu where the player changes the gamma. There's no need to rewrite the file unless the setting has changed, right?
Haha, yeah, you're absolutely right - don't know why I even tried that since I already thought that it would probably cause an error or slow-down because it would constantly be opening, writing and closing the file ::)
So, like a good boy, I put the writing code in the closing brightness script.

QuoteWhat do you mean that you "created" the file?
I created a text file and named it "Brightness".
I did the same thing again this morning, "Brightness.cfg", and everything worked great until I deleted it to test if it would go back to 100%.

--EDIT--
Shit, I knew this would happen.

It wasn't the deleting of the file that messed things up. I just tried commenting out the if (fmark ==null) bit of the code and it worked...

Now I have to store the values of the gui text that tells you the number of the brightness value...

ergh.

Thank you for all your help,


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Shane 'ProgZmax' Stevens

Snake, how about this for an easier option?  At the options page, when they select their options and save, make a SAVEGAME instead (after it returns to the title screen), which RESTORES when the game initially loads (taking them to the title screen).  In this way you can save all the settings made up until that point, such as gamma, max lives, or any control configs.  A separate file is more interesting, yeah, but for small settings in a game where you don't have save files this should work just fine.  The default save will have the default settings, of course!

Radiant

Make sure you're using the same set of read and write functions.

One does binary data; the other does an AGS-specific format. You want to not mix them up.

Also, "if (x) { } else if (not x) { }" is redundant. And also also, if fmark == null, then fmark.Close() is apt to fail.

Snake

Haha, I never thought of the savegame idea... brilliant!

Ah, well. I've got it working anyway, so hopefully it stays working when I release it again and people don't start telling me that their getting a bunch of errors.

I'm working on something special for the player right now as I type (besides trying to make it a little easier without being too easy) so I'm pretty excited about that - hopefully I'll get better reviews this time ;)

Thanks for your input, guys!


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

GarageGothic

I didn't realize that your game doesn't allow savegames, then of course ProgZmax' suggestion is much easier to implement. I'm storing gamma settings (along with other game options such as audio volume) in the acsetup.cfg file exactly to avoid the problem of them being changed whenever the user restores a savegame with older settings, and I just assumed you wanted the same. Good luck with the game!

Snake

Thank you!

I'm glad you assumed that because I learned how to do this today.
Every day I work on this game I learn something new - it's great - and thanks to all of you guys having patience with everybody :)

And thanks for the good luck, I need it ;)


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

SMF spam blocked by CleanTalk