Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by M_D. 04/26/24 20:22
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (M_D, AndrewAMD, Quad, Ayumi), 806 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Problem with saving a string array (game_save / game_load) #453370
07/23/15 10:19
07/23/15 10:19
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Hi,
I try to save my game data using the 'game_save' function.
Every time the save is succesful and when I load the game without
closing the program before, the loading also works.
But if I restart the game and want to load the saved game data, the strings don't load correctly.
Therefore i've wrote a short example code:

Code:
#include <acknex.h>
#include <default.c>

STRING* FileNames[256];
int iTest;

function saveLevel();
function loadLevel();
function outpRandomString();

function main()
{
   int i;
   random_seed(0);
   level_load("");
   for(i=0;i<256;i++)
      FileNames[i] = str_create("testName");
   iTest = 1234;
      
   on_f1 = saveLevel;
   on_f2 = loadLevel;
   on_f3 = outpRandomString;
      
   while(1) wait(1);
   return;
}

function saveLevel()   // saving the complete level
{
   int Save = game_save(str_create("test"),1,SV_ALL); // SV_VARS|SV_POINTERS|SV_STRINGS -> also don't works
   if(Save <= 0)
      printf("Error: saving");         
}

function loadLevel()   // loading the save game
{
   int i;
//   for(i=0;i<256;i++)
//      FileNames[i] = "";
        
   int Load = game_load(str_create("test"),1);
   if(Load <= 0)
      printf("Error: Loading");
   
   printf("iTest: %i",iTest);   // correct  
   outpRandomString();   // nothing, rubish or game crash         
}

function outpRandomString()   // outputs the string content at a random index
{
   int i = integer(random(256));   
   printf("Index:%i | String:%s",i,_chr(FileNames[i]));
}



Where is the mistake?
Thanks for help

P.S.: deutsche Antworten sind mir auch sehr recht ;-)

Last edited by Nicros; 07/23/15 10:20.
Re: Problem with saving a string array (game_save / game_load) [Re: Nicros] #453390
07/24/15 06:43
07/24/15 06:43
Joined: Jan 2006
Posts: 968
EpsiloN Offline
User
EpsiloN  Offline
User

Joined: Jan 2006
Posts: 968
I havent used game_save, but I think you're saving the pointers to a dynamically created strings.

Instead, you should str_cpy the contents of the dynamic string into your pre-defined string. The problem with pointers is that they're different every time you start the app, if they exist at all...
Generate the dynamic string in a 'tempString' and str_cpy tempString to FileNames[i]...

Also, for saving/loading, I dont think its necessary to str_create the name of the save file...you can just use quotes ""


Extensive Multiplayer tutorial:
http://mesetts.com/index.php?page=201
Re: Problem with saving a string array (game_save / game_load) [Re: EpsiloN] #453395
07/24/15 10:08
07/24/15 10:08
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Thanks for your reply.
I also think that it is a problem with the pointers.
But I don't know how to generate this strings not dynamically.
Because at the beginning the strings point to Zero and I can't use str_cpy() if they don't point to a certain memory region.
Here is my try after your answer:

Code:
STRING* FileNames[256];
STRING* tempStr;

[...]

tempStr = "testName";
for(i=0;i<256;i++)
{
   str_cpy(FileNames[i],tempStr);   //crash: invalid function arguments
}



How would you make this or have you another idea ?

Regards

Re: Problem with saving a string array (game_save / game_load) [Re: Nicros] #453401
07/24/15 15:59
07/24/15 15:59
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
If you simply want to save an array of string don't use the game save but write a txt file and then load back up as needed. The game save setup from 3dgs is designed to load a whole world. Way to complicated and time consuming compared to a simple text file.


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Re: Problem with saving a string array (game_save / game_load) [Re: FoxHound] #453403
07/24/15 18:16
07/24/15 18:16
Joined: Jan 2006
Posts: 968
EpsiloN Offline
User
EpsiloN  Offline
User

Joined: Jan 2006
Posts: 968
At least its a good experience with pointers grin

I'm not sure if you can do this, but try setting all strings to point to an empty dinamically created strings. Just initialize with str_create... all strings in the array, then copy content there. I think it wont work, but its fast to try.

Another way is to use a text object for storing array of strings... Just copy everything you need in them and save.

Using a file is good practise, btw.


Extensive Multiplayer tutorial:
http://mesetts.com/index.php?page=201
Re: Problem with saving a string array (game_save / game_load) [Re: EpsiloN] #453409
07/25/15 09:18
07/25/15 09:18
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Thanks for your suggestions. It works now laugh (not ... see Edit ->)
I've used the method with the text object because I have to save the whole game but there are two global string arrays defined.
Here is my new code. I've watched the used memory and I think without the 'ptr_remove'-call there will be a memory leak !?
Code:
int i;

[...]

TEXT* txtStringSave = {
   strings=256;
}

[...]

function saveLevel()
{
   for(i=0;i<256;i++)   // save in text object
   {
      str_cpy((txtStringSave.pstring)[i],FileNames[i]);
      // FileNames[i] = NULL;  don't do this -> memory leak
      ptr_remove(FileNames[i]);   // instead   
   }
   int Save = game_save(str_create("test"),1,SV_ALL); // SV_VARS|SV_POINTERS|SV_STRINGS
   if(Save <= 0)
      printf("Error: saving");
   for(i=0;i<256;i++)   // restore array
   {
      FileNames[i] = str_create((txtStringSave.pstring)[i]);
      str_cpy((txtStringSave.pstring)[i],""); 
   }         
}

function loadLevel()
{
   iTest = 0;
        
   int Load = game_load(str_create("test"),1);
   if(Load <= 0)
      printf("Error: Loading");
      
   for(i=0;i<256;i++)   // restore string array
   {
      FileNames[i] = str_create((txtStringSave.pstring)[i]);
      str_cpy((txtStringSave.pstring)[i],""); 
   }
   
   printf("iTest: %i",iTest);  
   outpRandomString();         
}



grin

Edit: Oh, sry... it does not work yet. If I change this line:
Code:
for(i=0;i<256;i++)
   FileNames[i] = str_create("testName");


to this for example:
Code:
for(i=0;i<256;i++)
	   FileNames[i] = str_create("Name");


Then: Script crash in main frown
Any ideas ?
... it seems that it doesn't load the text object correctly.


Last edited by Nicros; 07/25/15 09:46.
Re: Problem with saving a string array (game_save / game_load) [Re: Nicros] #453410
07/25/15 09:53
07/25/15 09:53
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
I haven't had a full look at your code and issue but I don't see a reason to remove or recreate them at all. Just use str_cpy to fill them with new content, once initialized.

Additionally, you overuse str_create way too much. Every time you call str_create you need a corresponding ptr_remove in your code, which means that something like
game_save(str_create("test"),1,SV_ALL);
will result in one new string floating in your memory which you cannot clear because you didn't save the pointer to it. Just write
game_save("test",1,SV_ALL);
instead.
When you keep the strings alive (I suggest you initialize them once at the start of the game) be aware that stuff like
FileNames[i] = str_create((txtStringSave.pstring)[i]);
has to be adapted to a single str_cpy call instead.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Problem with saving a string array (game_save / game_load) [Re: Superku] #453413
07/25/15 11:47
07/25/15 11:47
Joined: Jan 2006
Posts: 968
EpsiloN Offline
User
EpsiloN  Offline
User

Joined: Jan 2006
Posts: 968
Yes, dont destroy your string pointers, there is no need to. The engine will take care of memory leaks for you (thats why we get an engine laugh ).

Another thing, you dont have to use a string array at all, if you're using a text object...It holds your array and has allocated strings by default. You just have to str_copy into or out of them, nothing more...

As for your error, I suggest you wait at least one or two frames before/after you save/load a level.
When you execute a save statement, the program might not have finished everything it was doing to the strings. And, when you're loading, again wait for the program to stop doing anything and then load...And after that, wait one frame to make sure everything is fully loaded and working (or saved...)


Extensive Multiplayer tutorial:
http://mesetts.com/index.php?page=201
Re: Problem with saving a string array (game_save / game_load) [Re: EpsiloN] #453418
07/25/15 15:07
07/25/15 15:07
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Thanks. I thought that I have to use str_create to convert a char array into a STRING struct cry.
Now I've removed the superfluous str_create calls and use only a text object to store the strings.
But I think that the game_save function doesn't save the content of the text object...
Can I change this behavior ?
Code:
int iTest, i;

function saveLevel();
function loadLevel();
function outpRandomString();

TEXT* txtStringSave = {
   pos_x = 0;
   pos_y = 0;
   strings=256;
   flags = SHOW | LIGHT;
}

function main()
{
   [...]
}

function saveLevel()   
{   
   int Save = game_save("test",1, SV_ALL);
   if(Save <= 0)
      printf("Error: saving");       
}

function loadLevel()
{
   iTest = 0;
   
   int Load = game_load("test",1);
   if(Load <= 0)
      printf("Error: Loading");
   
   printf("iTest: %i",iTest);
   outpRandomString();         
}

function outpRandomString()
{
   i = integer(random(256));   
   printf("Index:%i | String:%s",i,_chr((txtStringSave.pstring)[i]));
}


Re: Problem with saving a string array (game_save / game_load) [Re: FoxHound] #453422
07/25/15 16:19
07/25/15 16:19
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
IMO game_save() is not really intended for any real project, it's just a quick and newbie friendly way (see following limitations) to realize a let's say quicksave and -load feature (which is supposed to save all entity positions and states in a level).
It's a really old command from times where you basically could not create anything but entities dynamically, which is why calling panel or text (/ ...) create instructions break the game_load function. I'd say only use that function on a very simple project which doesn't generate anything dynamically.
Otherwise, do as FoxHound said:
Originally Posted By: FoxHound
If you simply want to save an array of string don't use the game save but write a txt file and then load back up as needed. The game save setup from 3dgs is designed to load a whole world. Way to complicated and time consuming compared to a simple text file.

When it comes to saving your game progress just save some variables into a text file which describe the player's progress sufficiently, then load those variables and create a corresponding game state when necessary.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1