Loading/Saving with INI 2nd version.
Author: | AlanC
|
Submitted: | 16th July, 2004
|
Views: | 4467
| Rated: |
|
|
I know there several articles on how to use the INI extension, but this has gone a bit further. It shows you how to save and load objects, their positions on the play area and also saving/loading the frame(level).
You will need: The INI and FILE extensions and any active object so that the Alterable Value A can be used. I will call this object "Save". You will also need 2 buttons on frame 2 called "yes" and "no". You will need an active object called "gun" in frame 3.
First create 4 blank frames.
Frame 1 will test if the "game.ini" file exists.
Frame 2 will ask you if you want to load a saved game if the file exists. The options are "yes" and "no". If the file does not exist then it will go from frame 1 to frame 3. If the file does exist and you say "yes" then it will go to the frame that was saved last. If you say "no" then it will go to frame 3 ie the start of the game.
Frame 3 is the start of the game.
Frame 4 is the second frame of the game.
Frame 1.
Place the File extension outside the play area.
Add this code.
- Upon pressing any key
+ File: Appdrive$ + Appdir$ + game.ini is a file
Storyboard: Jump to frame number 2
- Upon pressing any key
+X File: Appdrive$ + Appdir$ + game.ini is a file
Storyboard: Jump to frame number 3
Frame 2
Place the INI extension and the Save objects outside the play are. Place the 2 buttons on the play area.
Add this code.
- Start of frame
Save: set Alterable Value A to 0
- Button no clicked
Storyboard: Jump to frame number 3
-Button yes clicked
Ini: Set current file to Appdrive$ + Appdir$ + game.ini
Ini: Set current group to save flag
Ini: Set current item to Save
Save: Set Alterable Value A to value of(ini)
- Alterable Value A of Save>0
Storyboard: Jump to frame number Alterable Value A(Save)
Frame 3
Place gun object in play area. Place INI and Save objects outside play area.
- Start of frame
gun: Set Alterable Value A to 0
(SAVE GAME) start of group event
- Upon pressing F12
Ini: Set current file to Appdrive$ + Appdir$ + game.ini
Ini: Set current group to save flag
Ini: Set current item to Save
Ini: Set item value frame
Ini: Save Alterable Value A to frame
Save: Set Alterable Value A to frame
Ini: Save position of gun
Ini: Save value Alterable Value A(gun) to item gun in group items
(LOAD GAME) start of group event
- Run this event once
Ini: Set current file to Appdrive$ + Appdir$ + game.ini
Ini: Set current group to save flag
Ini: Set current item to Save
- Run this event once
Save: Set Alterable Value A to value of(ini)
- Run this event once
+ Alterable Value A of Save>0
Ini: Load position of (gun)
gun: set Alterable Value A to groupitemvalue(ini,items,gun)
That's it!! SO what does this lot do?
Frame 1 will detect if the game.ini file exists using the file extension object.
If it exists then jump to frame 2
If not then jump to frame 3(the start of the game)
Frame 2 will detect if the buttons are clicked.
"no" jumps to frame 3
"yes" loads from the ini file the value for the Alterable Value A of the "Save" object.
The "Save" object is used as a flag to determine when in frame 3, that the "yes" button was clicked to load a saved game.
Frame 3 is the start of the game. In mine I use a parser to get the verb and noun, ie, "get gun", "drop gun" etc. When you "drop gun" and move to somewhere else you need to know where it is when you save the game and reload it. "Load position of gun" in the LOAD GAME group event will place the gun in the correct position where you dropped it. So here you will load all the items that you saved with the "F12" key. The Alterable Value A for the gun is used to tell the program if it is dropped or is picked up.ie 0=dropped, 1=picked up. I use this for all items that are pickable. And also Alterable Value B to indicate if eg torch is switched on or off.
If you were in say frame 4 and you pressed F12 to save the game. When you run the game again it will, if you select the "yes" button, jump to frame 4 and set up all the items for you. I didn't code frame 4 but you see how it works for subsequent frames.
I'm sure there are other ways to do this, but this is the way I do it, and it works well.
If I have missed anything please comment me below.
This is a rewrite of first article which I will delete.
Alan
|
|
AlanC
Registered 27/06/2004
Points 206
|