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
2 registered members (AndrewAMD, Quad), 843 guests, and 1 spider.
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 1 of 4 1 2 3 4
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 Offline OP
Member
Caermundh  Offline 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:
Code:
<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 Offline
Serious User
Widi  Offline
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: Widi] #364790
03/20/11 21:06
03/20/11 21:06
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
Anyone? No one has any ideas?

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 Offline
Expert
Uhrwerk  Offline
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: Uhrwerk] #364799
03/20/11 22:57
03/20/11 22:57
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
Ok..it sounds like im not understanding the concept then. I'm more than ready to admit ignorance on the subject, but what good does it do to save an area of memory without saving the contents of that area of memory? I'm sorry but i just dont get how thats useful or what can be done with that.

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 Offline
Expert
Uhrwerk  Offline
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
P
paracharlie Offline
Member
paracharlie  Offline
Member
P

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.

Code:
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: paracharlie] #364804
03/21/11 01:34
03/21/11 01:34
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
@Uhwerk:

Im not using handles because i guess i didnt know to. As well, a handle points to a global engine object such as a golbal panel, string or bitmap right?

im using structs because im reading all the items for the game from a text file. There is an unknown number of objects, so i use malloc to grab as much memory as i need. If i use global panel and string arrays, i would have these huge arrays that i'd end up only using like half of.

Granted, if the only way i can save a string, panel, or bmap is to make it global and use handles, then i guess ill just have to put up with huge arrays. I would dearly love a more sleek solution, though.

@Paracharlie:

you are using sys_malloc instead of malloc? what exactly is the difference? and what does the call to memset() do? ive never heard of that function.

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
P
paracharlie Offline
Member
paracharlie  Offline
Member
P

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.

Code:
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 Offline
Expert
Uhrwerk  Offline
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...
Page 1 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