Android Ini Files Problem

D

deva56

Guest
Hello everyone. Been messing around with this problem for a few days now and I really cant find any solution as there is no tutorials for this on net as far as I have found , and I really have no more ideas to try anymore combinations as not a single one isnt working the way I want it. So the thing is im creating an android game and i have just two levels now for testing purposes only. When the game starts it goes into the room which is shown in the picture under. Main menu to be exact thing. If you press play it takes you to another room on second picture where you can select between level 1 and 2.
help1.png help2.png First level is always unlocked , even when you start ur game for the first time but each next level like level 2 can be unlocked only by colliding with the object from the previous level of the level being unlocked. So for instance in level 1 i have a planet that needs to be collided and only when u collide with that planet only for once at least you can then play level 2. I would make this for all levels in the game but i made only two for now for testing. Problem is that without using ini files everything is working as it should only it affcourse doesnt remember anything and when u exit the game you need to start everything again which i dont want to and it has some minor bugs when it resets its value under some condition but nevermind. Things is when i try to make it with ini files it doesnt do properly and doesnt remember things. As much as I hit the plannet in level 1 level 2 wont unlock. I really tried all combinations and it isnt working. Maybe im doing something in wrong order or simply messing something really nice.So lets start. Firstly I have controller object in which i have create event with this code :
global.archive = 0;
ini_open("LevelsData");
ini_write_real("GeneralLevels", "Points", global.archive = 0);
ini_close();
It is defyning a variable that is later used to determine that some level is unlocked or not. The object is persistent to stay inside every room.
Secondly I have object in my first level that when collided with my player adds a point to unlock the next level.
It is collision event with the next code.
ini_open("LevelsData");
ini_read_real("GeneralLevels", "Points", 0);
if global.archive<1
{
ini_write_real("Level2", "Points", global.archive = 1);
ini_close();
}
room_goto(rm_LevelSelect);
It checks if the code from the controller object is less then 1 and if it is adds a value of 1 to the ini file and then goes to to room where you can again chose levels and where you now should be able to play level 2 but u cant .
Thirdly I have my object to select level 2 with room start and left mouse pressed code.
Room start code :
ini_open("LevelsData");
ini_read_real("Level2", "Points", 0);
ini_close();
left mouse pressed code:
if global.archive >= 1
{
room_goto(rm_Level1);
}
else
{
show_message("Not unlocked");
}
In room start code it reads the ini file that should have the value od 1 so when u press left mouse button it checks that it is at least 1 or more and then get you to the next level. But it doesnt it always show that level is not unlocked. So i hope I presented it right, probably too much text but i wanted to give u as many informations as possible . Any help would be aprreciated . Thank you in advance.
 
D

deva56

Guest
It is a variable that is making a game know if a level is unlocked or not. Actually the whole level system not using ini files it was modified by me is used by looking at this tutorial.
So if u have any missunderstoods you can see in the video that it is made on the behalf of the tutorial.
 
D

deva56

Guest
Anybody? If somebody can tell me how to store my variables after restarting the game , the level system is working perfectly only that levels reset after each game shutdown. I need them to always stay there even when u exit and enter the game.
 
D

deva56

Guest
If there is any better way of level unlocking system i would gladly take it.
 

chamaeleon

Member
Well according to your code in your first post you are apparently writing out an initial value for GeneralLevels upon creation, presumably overwriting any previously stored value. So if you want a written value to persist, don't do that. Figure what logic you want to apply with respect to your data persistence and write to ini files appropriately. Use whatever you can to manage it, be it checking whether an ini file exist before writing, or if a particular section or key exist in an ini file before writing a critical value.
 

KhMaIBQ

Member
I would change your create code to the following...

Code:
ini_open( "LevelsData.ini" ); // Put .ini at the end of the file name

// Check if key for level progression exists
if ( !ini_key_exists( "GeneralLevels", "Points" ) )
{
    // Init the level progression save data
    ini_write_real( "GeneralLevels", "Points", 0 );
}

// Read the level progression save data and store it in the global.archive variable
global.archive = ini_read_real( "GeneralLevels", "Points", 0 );

// Close ini file now that we are done
ini_close();
Also, when you use the ini_read_real function, you need to specify a variable to store it to or use it in a logical expression. Otherwise, the read data doesn't get used properly. Your reads from the ini currently go to nothing which is the main issue you are having. That is why your level progression check is not working.

EDIT: I would elaborate more on the issue you are having, but I am currently at work. If I have time later, I'll try to make a more thorough post. I hope this at least helps you for now. Good luck!
 

ph101

Member
It is a variable that is making a game know if a level is unlocked or not.
You really don't want to be putting an = sign into that argument. As the manual describes:

ini_write_real(section, key, value);

section The section of the .ini to write to.
key The key within the relevant section of the .ini to write to.
value The real value to write to the relevant destination.
 
D

deva56

Guest
I would change your create code to the following...

Code:
ini_open( "LevelsData.ini" ); // Put .ini at the end of the file name

// Check if key for level progression exists
if ( !ini_key_exists( "GeneralLevels", "Points" ) )
{
    // Init the level progression save data
    ini_write_real( "GeneralLevels", "Points", 0 );
}

// Read the level progression save data and store it in the global.archive variable
global.archive = ini_read_real( "GeneralLevels", "Points", 0 );

// Close ini file now that we are done
ini_close();
Also, when you use the ini_read_real function, you need to specify a variable to store it to or use it in a logical expression. Otherwise, the read data doesn't get used properly. Your reads from the ini currently go to nothing which is the main issue you are having. That is why your level progression check is not working.

EDIT: I would elaborate more on the issue you are having, but I am currently at work. If I have time later, I'll try to make a more thorough post. I hope this at least helps you for now. Good luck!
I used yours code for the create event and i changed other codes a little as well and now everything is working the way i want. I added one more level just to make sure it is working like that to an infinite number of levels if i want to. Each unique planet in previous planet is unlocking the next level , and no matter how much times u replay the value will always stay the same so only the current and previous levels can be played. Now also no matter i do or restart the game it always remembers which levels were already unlocked. Thank you very much for yours help and time and everybody else. I appreciate it very much. Nobody says we wont talk again about something else :D but for now this is it.
 

KhMaIBQ

Member
I am glad to hear things worked out for you. There is a way to do what you are doing without using a global variable. However, if that global variable is being used for other things, then it is probably better to just use the global variable. I try to avoid using global variables when I can.
 
Top