|
Saving the *contents* of a structure with game_save()...
#363620
03/13/11 17:19
03/13/11 17:19
|
Joined: Aug 2005
Posts: 238
Caermundh
OP
Member
|
OP
Member
Joined: Aug 2005
Posts: 238
|
How do I save the contents of a structure with game_save()? I have the following, which doesnt quite work:
<headers.h>
typedef struct test_struct
{ var value1;
var value2;
STRING* str1;
STRING* str2;
STRING* str3;
struct test_struct* next;
} test_struct;
test_struct* first_struct;
test_struct* curr_struct;
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()
{ game_save("test",1,SV_VARS+SV_STRUCT);
str_cpy((status_text.pstring)[0],"Status: Game Saved.");
}
function load_game()
{ str_remove(first_struct->str1);
str_remove(first_struct->str2);
str_remove(first_struct->str3);
free(first_struct);
game_load("test",1);
str_cpy((status_text.pstring)[0],"Status: Game Loaded.");
}
function main()
{ on_f1 = save_game;
on_f2 = load_game;
first_struct = (test_struct*)malloc(sizeof(test_struct));
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->value1 = 5;
first_struct->value2 = 10;
first_struct->next = NULL;
add_struct(first_struct,sizeof(test_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);
}
}
when i do a game_save() with this code, it saves first_struct->value1 & value2, and it saves the *pointers* to str1, str2, and str3, but it does not save the strings themselves. (IE when i load the game first_struct->str1 isnt equal to "This is the first str" - it is equal to whatever garbage happens to be in the memory area that the str pointer is pointing at)
Last edited by Caermundh; 03/14/11 00:20.
|
|
|
Re: Saving the *contents* of a structure with game_save()...
[Re: Caermundh]
#363656
03/13/11 21:53
03/13/11 21:53
|
Joined: Aug 2007
Posts: 1,922 Schweiz
Widi
Serious User
|
Serious User
Joined: Aug 2007
Posts: 1,922
Schweiz
|
Please edit your post with the [ code ] tag.
[ code ] ...your code [ /code ]
(without spaces) It is not so easy to read code if it is not indent!!!
Last edited by Widi; 03/14/11 00:11.
|
|
|
Re: Saving the *contents* of a structure with game_save()...
[Re: Caermundh]
#364794
03/20/11 22:02
03/20/11 22:02
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Ideas concerning what? That is exactly the intended behaviour. If you don't want this behaviour use handles or texts instead.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Saving the *contents* of a structure with game_save()...
[Re: Caermundh]
#364800
03/21/11 00:42
03/21/11 00:42
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
The content of the area of memory is saved. Unfortunately in your case the content is an address. You can of course save the address to a harddisk and load it later on. The big question then is, if that address is still valid, i.e. if the pointer points at a valid address with usefull data in it. The concept nevertheless does a lot of good, because you can save arbitrary data with it - with the exception for pointers, which can be loaded and saved as well, but do not make any sense after loading. Why don't you just use handles instead of the pointers?
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]
#364802
03/21/11 01:04
03/21/11 01:04
|
Joined: Mar 2009
Posts: 146 USA
paracharlie
Member
|
Member
Joined: Mar 2009
Posts: 146
USA
|
You have to create a new object using sys_malloc. Then from the memory it is saved. Then to call the information back you just recall it back from main and it will reset the info that was saved and populate the members of struct.
typedef struct PERSO{
int age;
char* name;
char* nickname;
} PERSO;
PERSO* Person;
PERSO* new_person()
{
PERSO* new = sys_malloc(sizeof(PERSO));
memset(new,0,sizeof(PERSO));
new->nickname = "Toto_2"; // test name
new->age = 18;
add_struct(new,sizeof(PERSO));
return new;
}
void main()
{
Person = new_person(); //
}
Last edited by paracharlie; 03/21/11 01:06.
A8 Commercial
|
|
|
Re: Saving the *contents* of a structure with game_save()...
[Re: Caermundh]
#364805
03/21/11 02:00
03/21/11 02:00
|
Joined: Mar 2009
Posts: 146 USA
paracharlie
Member
|
Member
Joined: Mar 2009
Posts: 146
USA
|
Memset is C and its allocating a block of memory to the heap. sys_malloc is a predefine in gamestudio. malloc and new is supposably slower and is to C what sys_malloc is to gamestudio. You can click and read the description in gamestudio. Explains it better then I can. Basically you are creating a pointer to your struct. Then your allocating a block of dynamic memory to hold the data that is stored in your struct and now that it's safely tucked away in memory you can save it with game_save. Here is actual code from my game and yes it is this easy.
typedef struct PLR
{
var p_STR; //Roll 3D6
var p_CON; //Roll 3D6
var p_DEX; //Roll 3D6
var p_SIZ; //Roll 2D6
var p_INT; //Roll 2D6 + 6
var p_POW; //Roll 3D6
var p_CHA; //Roll 3D6
var p_PHY; //Roll 3d6
}PLR;
PLR* plr;//create a pointer variable called plr used in game_load() see main.
//make stat struct saveable
function p_save_struct()//create a function that will dynamically hold the contents of PLR struct.
{
PLR* new = sys_malloc(sizeof(PLR));//create a pointer variable called new(call it whatever you want), get the size of PLR struct and point to it.
memset(new,0,sizeof(PLR));//allocate memory for it on the heap. Remember there is stack and heap?
add_struct(new,sizeof(PLR));//add the size of the struct PLR to add_struct, making it game_save() saveable.
return new;//now return the value of new so that p_save_struct is worth something
}
void main()
{
plr = p_save_struct();
}
Last edited by paracharlie; 03/21/11 02:09.
A8 Commercial
|
|
|
Re: Saving the *contents* of a structure with game_save()...
[Re: Caermundh]
#364806
03/21/11 02:01
03/21/11 02:01
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
memset sets an area of memory to the value specified. In paracharlies example the memory area "new" points to is overwritten with the value "0" at a length of "sizeof(PERSO)" (= 12) bytes. This is a good practice in c to avoid vagabonding pointers.
I didn't know that there is a connection betweeen sys_malloc and game_save. I didn't find any hints in the manual on that topic...
The handle thing is really easy. You can call handle() to get the handle of any engine object, regardless if it is string, a bitmap, or whatever. You can convert that handle to a pointer easily by calling ptr_for_handle(). It works the same as handle(), just the other way around. The advantage is, that handles - unlike pointers - are even valid after a save / load or even after a complete game or computer restart. If you just want to save "pointers" to engine objects like panels, strings and the like a combination of handles and structs could be an elegant solution for you.
EDIT: Charlie was faster... ^^
EDIT2: memset has absolutely nothing to do with memory allocation. See my explanation above... The code above does not really help with the problem as the struct does not contain any pointers...
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|