On Sat, 8 Jun 2002, Keith Mervine wrote:
> Does it matter if its written as 0x8049ec4 What I want this code to do it
> save the skills structure to a text file so it can be loaded dynamically
> and saved dynamically as well. I don't need the file itself to be read by
> humans. If this does matter then I'll have to dig a little deeper on how
> to convert it to ascii text.
Well, 0x8049ec4 is probably a lot better than the garbage it would
have been printing out before. But it's still not useful.
What you're trying to do is print out a pointer to a function, then
read that pointer back in. A pointer contains a memory address.
Pointers are only good in one process. As soon as that process
goes away (such as when your mud restarts), the pointer is no longer
valid.
What you have to do is convert the pointer into some other format that
you can convert back into a function pointer when you read it back in.
It doesn't have to be ASCII text, but that's probably the easiest.
I would set up a table something like this:
struct func_pointers {
char * name;
SPELL_FUN * function;
};
struct func_pointers my_pointers = {
{ "spell_summon", spell_summon },
{ "spell_gas_breath, spell_gas_breath },
/* other spells here */
{ NULL, NULL }
};
Then to convert from a function pointer to an ascii string and back,
you could use code like this:
char * function_to_ascii ( SPELL_FUN * function ) {
int i;
for ( i = 0; my_pointers [ i ].name != NULL; i++ ) {
if ( function == my_pointers [ i ].function )
return my_pointers [ i ].name;
}
return "none";
}
SPELL_FUN * ascii_to_function ( char * name ) {
int i;
for ( i = 0; my_pointers [ i ].name != NULL; i++ ) {
if ( name == my_pointers [ i ].name )
return my_pointers [ i ].function;
}
return NULL;
}
Then when saving, use something like:
printf ( "%s", function_to_ascii ( function_pointer_to_save ) );
When loading, use something like:
function_pointer_to_load = ascii_to_function ( fread_string(fp) );
I'm not looking at the code right now for the exact syntax of this, but
this should be close.
Dennis