Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 975 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 3 of 4 1 2 3 4
Re: Saving the *contents* of a structure with game_save()... [Re: paracharlie] #364922
03/22/11 01:13
03/22/11 01:13
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Originally Posted By: paracharlie
You should not have to write out every member of a structure or class at any time. Think about it. What if your structure or class were 3,4 or 500 lines easily done in gaming industry.

I don't get your point here. So what if a structure was 500 lines long? Well, you can be pretty sure your design uhm... well ... leaves some space for improvements... but I don't see how this is connected to a saving or loading mechanism. You can either use game_save/game_load to let the engine do all the dirty work for you. Of course this approach cannot be controlled that fine-granular. But on the other hand you can of course use file_open_write and write out exactly what you want and skip any fields you want. You of course then have a lot more work to do, but that is the price you have to pay for more flexibility.


Always learn from history, to be sure you make the same mistakes again...
Re: Saving the *contents* of a structure with game_save()... [Re: Uhrwerk] #364923
03/22/11 01:21
03/22/11 01:21
Joined: Mar 2009
Posts: 146
USA
P
paracharlie Offline
Member
paracharlie  Offline
Member
P

Joined: Mar 2009
Posts: 146
USA
Your right. My way is wrong please dont use it. It will lead to problems in gamestudio. Please do exactly what uhrwerk said to do. He is right.
And another thing. That code isn't mine. It was written by a very respected user of this forum.

Last edited by paracharlie; 03/22/11 01:34.

A8 Commercial
Re: Saving the *contents* of a structure with game_save()... [Re: paracharlie] #364965
03/22/11 14:15
03/22/11 14:15
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
Actually, upon closer examination i have found that this code doesnt work - i made the following change to mem_clear():

Code:
function mem_clear()
{   str_cpy((save_text.pstring)[0],"   ");
    str_cpy((save_text.pstring)[1],"   ");
    str_cpy((save_text.pstring)[2],"   ");
    first_struct->value1 = 0;
    first_struct->value2 = 0;
    str_remove(first_struct->str1);
    str_remove(first_struct->str2);
    str_remove(first_struct->str3);
    free(first_struct);
    str_cpy((status_text.pstring)[0],"Status: Memory Cleared.");
}



I wanted to make sure to clear out *everything* in memory to make sure that strings were being re-loaded from the save file. The reason it looked like it worked before is because the strings were still in memory. Once i clear the save_text.pstrings the strings are still blank after a reload.

dammit - this is really starting to frustrate me.

Re: Saving the *contents* of a structure with game_save()... [Re: Caermundh] #364966
03/22/11 14:18
03/22/11 14:18
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
I'm specifically making sure that im saving strings - and making sure that the strings are global. I just dont get why this doesnt work

EDIT: ok i decided to RTFM and i think i found the problem - SV_STRINGS mode in game_save saves all *modified* strings only. Not sure how a string created with str_create *doesn't* count as a modified string, but there you go. I think I have to use SV_ALL.

EDIT2: ok SV_ALL doesnt work either......for crying out loud why is this so difficult?

Last edited by Caermundh; 03/22/11 14:27.
Re: Saving the *contents* of a structure with game_save()... [Re: Caermundh] #364973
03/22/11 14:49
03/22/11 14:49
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
So i decided to simplify and make sure game_save is working:

Code:
//////////////////////////////////////////////////////////////////////
// test.c

#include <acknex.h>
#include <default.c>
#include "headers.h"

FONT* standard_font = "ackfont.pcx";

PANEL* info_panel =
{   pos_x = 20;
    pos_y = 20;
    flags = SHOW;
    digits = 0,0,3.0,standard_font,1,test;
    digits = 0,10,3.0,standard_font,1,test2;
}

TEXT* status_text = 
{   pos_x = 20;
    pos_y = 70;
    flags = SHOW;
    font = standard_font;
    strings = 1;
    string("Status: N/A");
}

TEXT* save_text = 
{   pos_x = 20;
    pos_y = 40;
    flags = SHOW;
    font = standard_font;
    strings = 3;
    string("This is the first string","This is the second string","This is the third string");
}

function save_game()
{   game_save("test",1,SV_ALL);
    str_cpy((status_text.pstring)[0],"Status: Game Saved.");
}

function mem_clear()
{   str_cpy((save_text.pstring)[0],"   ");
    str_cpy((save_text.pstring)[1],"   ");
    str_cpy((save_text.pstring)[2],"   ");
    str_cpy((status_text.pstring)[0],"Status: Memory Cleared.");
}

function load_game()
{   game_load("test",1);
    str_cpy((status_text.pstring)[0],"Status: Game Loaded.");
}

function main()
{   on_f1 = save_game;
    on_f2 = mem_clear;
    on_f3 = load_game;
    while(key_esc==0)
    {   wait(1);
    }
}



To my surprise, this code doesnt work either. After game_load, the pstrings in save_text were still blank. As far as I can tell, game_save is just outright not working. Is it maybe bugged i wonder? Im currently using Gamestudio Pro 7.82 - i know its not the latest and greatest, but upgrading to A8 just causes me all kinds of other problems.

Is this maybe a bug in 7.82?

Last edited by Caermundh; 03/22/11 15:35.
Re: Saving the *contents* of a structure with game_save()... [Re: Caermundh] #364977
03/22/11 15:39
03/22/11 15:39
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
No, it's not a bug. It's a feature. ;-)

Originally Posted By: The Almighty Manual
For not unnecessarily inflating the save file, SV_STRINGS only saves modified strings. If you modify a string after game_save, it was not saved, and is consequently not overwritten to its initial content on game_load. So modify all strings that should be saved and loaded before game_save; for instance set them to their initial content.


If you insert
Code:
str_cat((save_text.pstring)[0]," ");
str_cat((save_text.pstring)[1]," ");
str_cat((save_text.pstring)[2]," ");

this into your save function it will work as you would have expected.


Always learn from history, to be sure you make the same mistakes again...
Re: Saving the *contents* of a structure with game_save()... [Re: Uhrwerk] #364989
03/22/11 17:43
03/22/11 17:43
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
OK....so the following code really works (I really mean it this time LOL - I tested it three times and it is loading the strings from the save file):

Code:
///////////////////////////////////////////////////////////////////
// headers.h - headers file for test

typedef struct test_struct
{   var value1;
    var value2;
    STRING* str1;
    STRING* str2;
    STRING* str3;
    struct test_struct* next;
} test_struct;

typedef struct save_struct
{   var value1;
    var value2;
    var str1;
    var str2;
    var str3;
    struct save_struct* next;
} save_struct;

test_struct* first_struct;
test_struct* curr_struct;

save_struct* first_save;
save_struct* curr_save;

var test;
var test2;

//////////////////////////////////////////////////////////////////////
// test.c

#include <acknex.h>
#include <default.c>
#include "headers.h"

FONT* standard_font = "ackfont.pcx";

PANEL* info_panel =
{   pos_x = 20;
    pos_y = 20;
    flags = SHOW;
    digits = 0,0,3.0,standard_font,1,test;
    digits = 0,10,3.0,standard_font,1,test2;
}

TEXT* info_text = 
{   pos_x = 20;
    pos_y = 40;
    flags = SHOW;
    font = standard_font;
    strings = 3;
    string("             ","             ","             ");
}

TEXT* status_text = 
{   pos_x = 20;
    pos_y = 70;
    flags = SHOW;
    font = standard_font;
    strings = 1;
    string("Status: N/A");
}

function save_game()
{   str_cat(first_struct->str1," ");
    str_cat(first_struct->str2," ");
    str_cat(first_struct->str3," ");
    first_save->value1 = first_struct->value1;
    first_save->value2 = first_struct->value2;
    first_save->str1 = handle(first_struct->str1);
    first_save->str2 = handle(first_struct->str2);
    first_save->str3 = handle(first_struct->str3);
    add_struct(first_save,sizeof(save_struct));
    game_save("test",1,SV_ALL);
    str_cpy((status_text.pstring)[0],"Status: Game Saved.");
}

function mem_clear()
{   first_struct->value1 = 0;
    first_struct->value2 = 0;
    str_cpy(first_struct->str1," ");
    str_cpy(first_struct->str2," ");
    str_cpy(first_struct->str3," ");
    first_save->value1 = 0;
    first_save->value2 = 0;
    first_save->str1 = 0;
    first_save->str2 = 0;
    first_save->str3 = 0;
    str_cpy((status_text.pstring)[0],"Status: Memory Cleared.");
}

function load_game()
{   game_load("test",1);
    first_struct->value1 = first_save->value1;
    first_struct->value2 = first_save->value2;
    first_struct->str1 = ptr_for_handle(first_save->str1);
    first_struct->str2 = ptr_for_handle(first_save->str2);
    first_struct->str3 = ptr_for_handle(first_save->str3);
    str_cpy((status_text.pstring)[0],"Status: Game Loaded.");
}

function main()
{   on_f1 = save_game;
    on_f2 = mem_clear;
    on_f3 = load_game;
    first_struct = (test_struct*)malloc(sizeof(test_struct));
    first_struct->value1 = 5;
    first_struct->value2 = 10;
    first_struct->str1 = str_create("This is the first str");
    first_struct->str2 = str_create("This is the second str");
    first_struct->str3 = str_create("This is the third str");
    first_struct->next = NULL;
    first_save = (save_struct*)malloc(sizeof(save_struct));
    first_save->value1 = 0;
    first_save->value2 = 0;
    first_save->str1 = 0;
    first_save->str2 = 0;
    first_save->str3 = 0;
    first_save->next = NULL;
    while(key_esc==0)
    {   test = first_struct->value1;
        test2 = first_struct->value2;
        (info_text.pstring)[0] = first_struct->str1;
        (info_text.pstring)[1] = first_struct->str2;
        (info_text.pstring)[2] = first_struct->str3;
        wait(1);
    }
}



There were only two problems with the code as I had it before. One was that I was not modifying my strings so they weren't getting saved - just like Uhrwerk pointed out. (nice catch Uhrwerk - that one would have had me going for a year). Two was that I was doing a str_remove in the mem_clear function. Even once i *did* get the code to save strings, there was no string for it to be assigned back to if i do a str_remove.

Re: Saving the *contents* of a structure with game_save()... [Re: Caermundh] #364997
03/22/11 18:30
03/22/11 18:30
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
So I wasn't happy with the idea of not being able to remove those strings - (im anal retentive about memory management - if I dont need it i *will* have it released) i came up with the following, which works as well and doesnt leave a bunch of empty strings hanging around in memory:

Code:
///////////////////////////////////////////////////////////////////
// headers.h - headers file for test

typedef struct test_struct
{   var value1;
    var value2;
    STRING* str1;
    STRING* str2;
    STRING* str3;
    struct test_struct* next;
} test_struct;

typedef struct save_struct
{   var value1;
    var value2;
    STRING* str1;
    STRING* str2;
    STRING* str3;
    struct save_struct* next;
} save_struct;

test_struct* first_struct;
test_struct* curr_struct;

save_struct* first_save;
save_struct* curr_save;

var test;
var test2;

//////////////////////////////////////////////////////////////////////
// test.c

#include <acknex.h>
#include <default.c>
#include "headers.h"

FONT* standard_font = "ackfont.pcx";

PANEL* info_panel =
{   pos_x = 20;
    pos_y = 20;
    flags = SHOW;
    digits = 0,0,3.0,standard_font,1,test;
    digits = 0,10,3.0,standard_font,1,test2;
}

TEXT* info_text = 
{   pos_x = 20;
    pos_y = 40;
    flags = SHOW;
    font = standard_font;
    strings = 3;
    string("             ","             ","             ");
}

TEXT* status_text = 
{   pos_x = 20;
    pos_y = 70;
    flags = SHOW;
    font = standard_font;
    strings = 1;
    string("Status: N/A");
}

function save_game()
{   file_delete("test1.sav");
    first_save->value1 = first_struct->value1;
    first_save->value2 = first_struct->value2;
    first_save->str1 = str_create(first_struct->str1);
    first_save->str2 = str_create(first_struct->str2);
    first_save->str3 = str_create(first_struct->str3);
    str_cat(first_save->str1," ");
    str_cat(first_save->str2," ");
    str_cat(first_save->str3," ");
    game_save("test",1,SV_ALL);
    str_cpy((status_text.pstring)[0],"Status: Game Saved.");
}

function mem_clear()
{   first_struct->value1 = 0;
    first_struct->value2 = 0;
    str_remove(first_struct->str1);   // <--- notice all strings and even
    str_remove(first_struct->str2);   // first_struct itself are being re-
    str_remove(first_struct->str3);   // leased from memory.
    free(first_struct);
    first_save->value1 = 0;
    first_save->value2 = 0;
    first_save->str1 = NULL;
    first_save->str2 = NULL;
    first_save->str3 = NULL;
    str_cpy((status_text.pstring)[0],"Status: Memory Cleared.");
}

function load_game()
{   game_load("test",1);
    first_struct = (test_struct*)malloc(sizeof(test_struct));
    first_struct->value1 = first_save->value1;
    first_struct->value2 = first_save->value2;
    first_struct->str1 = first_save->str1;
    first_struct->str2 = first_save->str2;
    first_struct->str3 = first_save->str3;
    str_cpy((status_text.pstring)[0],"Status: Game Loaded.");
}

function main()
{   on_f1 = save_game;
    on_f2 = mem_clear;
    on_f3 = load_game;
    first_struct = (test_struct*)malloc(sizeof(test_struct));
    first_struct->value1 = 5;
    first_struct->value2 = 10;
    first_struct->str1 = str_create("This is the first str");
    first_struct->str2 = str_create("This is the second str");
    first_struct->str3 = str_create("This is the third str");
    first_struct->next = NULL;
    first_save = (save_struct*)malloc(sizeof(save_struct));
    first_save->value1 = 0;
    first_save->value2 = 0;
    first_save->str1 = 0;
    first_save->str2 = 0;
    first_save->str3 = 0;
    first_save->next = NULL;
    add_struct(first_save,sizeof(save_struct));
    while(key_esc==0)
    {   test = first_struct->value1;
        test2 = first_struct->value2;
        (info_text.pstring)[0] = first_struct->str1;
        (info_text.pstring)[1] = first_struct->str2;
        (info_text.pstring)[2] = first_struct->str3;
        wait(1);
    }
}



Re: Saving the *contents* of a structure with game_save()... [Re: Caermundh] #365026
03/22/11 21:38
03/22/11 21:38
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
I don't understand how you jump to the conclusion that there are non-used strings in memory when you use game_save..?


Always learn from history, to be sure you make the same mistakes again...
Re: Saving the *contents* of a structure with game_save()... [Re: Uhrwerk] #365034
03/22/11 22:32
03/22/11 22:32
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
no - its not game_save thats making non-used strings - its my own code. Look at first_struct - in the previous version i was not releasing the strings, merely copying " " to them. I am releasing first struct in mem_clear, then re-allocating it at game_load, so those strings arent used anymore. They should be released. For some reason though, if you did str_remove instead of str_cpy in the previous one, when i did a load game the strings in first_struct would still be blank, even when i did a str-create first to make sure there was a string to be assigned to.

Page 3 of 4 1 2 3 4

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