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 (Ayumi, AndrewAMD), 833 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 2 of 4 1 2 3 4
Re: Saving the *contents* of a structure with game_save()... [Re: Uhrwerk] #364807
03/21/11 02:11
03/21/11 02:11
Joined: Mar 2009
Posts: 146
USA
P
paracharlie Offline
Member
paracharlie  Offline
Member
P

Joined: Mar 2009
Posts: 146
USA
I think that is what memset does? I dont know I been using C++ and new and havent used that in awhile. I dont even remember now to be honest.

Oh wait I remember, your just setting the size of the memory sys_malloc is allocating sorry for misleading you. I dont retain info for very long lol...

Last edited by paracharlie; 03/21/11 02:13.

A8 Commercial
Re: Saving the *contents* of a structure with game_save()... [Re: paracharlie] #364823
03/21/11 09:47
03/21/11 09:47
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline
Serious User
MrGuest  Offline
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
memset has nothing to do with setting memory to the heap, as Uhrwerk said it is setting the memory address to the character you specify (usually 0 to clear the previous data)

Example:
Code:
// crt_memset.c
/* This program uses memset to
 * set the first four chars of buffer to "*".
 */

#include <memory.h>
#include <stdio.h>

int main( void )
{
   char buffer[] = "This is a test of the memset function";

   printf( "Before: %s\n", buffer );
   memset( buffer, '*', 4 );
   printf( "After:  %s\n", buffer );
}



Output:
Code:
Before: This is a test of the memset function
After:  **** is a test of the memset function


Resource

Re: Saving the *contents* of a structure with game_save()... [Re: MrGuest] #364842
03/21/11 13:22
03/21/11 13:22
Joined: Mar 2009
Posts: 146
USA
P
paracharlie Offline
Member
paracharlie  Offline
Member
P

Joined: Mar 2009
Posts: 146
USA
void * memset ( void * ptr, int value, size_t num );
It exists on the heap.
as does your memory buffer.
which can be *array or array[0] both the same. Arrays are pointers.
The thing I dont understand though; shouldnt it be '\0' for null character? Instead of 0 and why does that work?

Last edited by paracharlie; 03/21/11 13:41.

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

Joined: Aug 2005
Posts: 238
Paracharlie:

I tried using sys_malloc instead of malloc, and the result is the same - the strings were not saved. I think Uhwerk is on the right track - I have to make the strings global, set the struct to store the handle of the global str and do a game_save() with SV_STRINGS.

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

Joined: Jan 2002
Posts: 4,225
Germany / Essen
@paracharlie memset doesn't give a f*** if the pointer points to the heap or to the stack. MrGuest called it character, although the term byte seems a bit better to me. memset is not limited to writing characters, as the the term "char" might suggest. Technically speaking '\0', NULL, and 0 are the same, i.e. a lot of bits where no bit corresponds to '1'. '\0' can be 8 or 16 bits, NULL is 32 bits as well as 0 as it is by default interpreted as an integer constant.

Caermundh: I already thought that wouldn't work. It would require some magic to work. However: The handle thing is not a more of work for you. You cannot have local strings anyways. A string is always an engine object, so there is no need to make it global in some way.


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] #364846
03/21/11 14:19
03/21/11 14:19
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
SUCCESS!!!!

The following code works:

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");
}

TEXT* save_text;

function save_game()
{   save_text = txt_create(3,1);
    (save_text.pstring)[0] = str_create(first_struct->str1);
    (save_text.pstring)[1] = str_create(first_struct->str2);
    (save_text.pstring)[2] = str_create(first_struct->str3);
    first_save->value1 = first_struct->value1;
    first_save->value2 = first_struct->value2;
    first_save->str1 = handle((save_text.pstring)[0]);
    first_save->str2 = handle((save_text.pstring)[1]);
    first_save->str3 = handle((save_text.pstring)[2]);
    game_save("test",1,SV_VARS+SV_STRUCT+SV_STRINGS);
    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);
    str_remove(first_struct->str2);
    str_remove(first_struct->str3);
    free(first_struct);
    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 = str_create(ptr_for_handle(first_save->str1));
    first_struct->str2 = str_create(ptr_for_handle(first_save->str2));
    first_struct->str3 = str_create(ptr_for_handle(first_save->str3));
    str_cpy((status_text.pstring)[0],"Status: Game Loaded.");
}

function main()
{   STRING* test_str2 = "This is the local str";
    wait(3);
    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);
    }
}



After doing a game load, the strings were equal to "This is the xxx str" instead of random garbage. Kind of a pain to set up, but at least it works.
Thank you everyone for all your help.

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

Joined: Aug 2005
Posts: 238
@ Uhrwerk:

I can't have local strings? I declare stings inside of functions all the time - like so:

Code:
function my_func()
{   STRING* my_str = "              ";
    str_cpy(my_str,"this is my str");
    return(0);
}



Are you saying that my_str, in this instance, is still global even though I declared it locally?

EDIT: sorry, after re-reading your post, I think I see what your saying. Even my_str or my_struct->str1 is a string that I can feed to handle() and get a handle for it right?



Last edited by Caermundh; 03/21/11 14:28.
Re: Saving the *contents* of a structure with game_save()... [Re: Caermundh] #364852
03/21/11 14:46
03/21/11 14:46
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Right. I don't know how the code you posted here behaves and can only guess here. My guess is, that the 32 bit for the pointer my_str are of course allocated on the stack and the memory required for the string itself is allocated on the heap as if you used "str_create(" ");".


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] #364912
03/21/11 23:45
03/21/11 23:45
Joined: Mar 2009
Posts: 146
USA
P
paracharlie Offline
Member
paracharlie  Offline
Member
P

Joined: Mar 2009
Posts: 146
USA
I'm glad you have it working. I wonder if it's the way that pstring is being handled. I'll definately keep an eye on this. I use local strings for battle text in a text box using a logger script which is local and it saves everything. Kind of annoying actually and took alot of work to clear the text box after clicking load using game_save and game_load. Problem with game_save is it will save everything and anything unless you set it up properly. My problem was that I could never seem to get it set 'properly'


A8 Commercial
Re: Saving the *contents* of a structure with game_save()... [Re: paracharlie] #364918
03/22/11 00:34
03/22/11 00:34
Joined: Mar 2009
Posts: 146
USA
P
paracharlie Offline
Member
paracharlie  Offline
Member
P

Joined: Mar 2009
Posts: 146
USA
Here is a stand alone example. 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'm Finding out by using this engine more and more that the norm is not the norm and forget about what you know when you use this engine. Been using another engine in visual c++ and honestly, there is many many easier ways that wont work in gamestudio.

Code:
//test add_struct

///////////////////////////////
#include <acknex.h>
#include <default.c>

///////////////////////////////

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;
}

TEXT* test =
{
	strings = 2;
   pos_x = 10;
   pos_y = 10;
   flags= SHOW;
}


function save()
{
	if(game_save("save",1,SV_ALL) <= 0)
	{
		error("save not found");
	}else{
		error("save success");
	}
}

function load()
{
	if(game_load("save",1) <= 0)
	{
		error("Load save error");
		sys_exit(NULL);
	}else{
		error("loaded save success");
		
	}
}

function quit()
{
	sys_exit(NULL);
}

function send()
{
	Person.nickname = "Toto";		

}

function main()
{

	video_mode = 7;
	video_depth = 32;
	video_screen = 2;
	level_load(NULL);
	
	on_f2 = send ;
	on_f3 = save;
	on_f4 = load;
	on_esc = quit;
	on_close = quit;
	
	Person = new_person();
	
	while(1) {
		str_cpy((test.pstring)[0], _str(Person.nickname));	
					
		wait(1);
	}
}




A8 Commercial
Page 2 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