Advertisement

Saving function pointers...

Started by October 26, 1999 11:21 AM
4 comments, last by Starwynd 25 years, 1 month ago
Personally I'm writing a script parser and the way I'm doing it is this.

Each function has a unique name used by the script such as
"Get Distance"
I have an array of structures containing a function name and an enumeration
{
...
"Get Distance", GET_DISTANCE
...
}

where

enum
{
...
GET_DISTANCE,
...
}FUNCTIONS;

and another array containing functions in the same order
function *function[] =
{
...
GetDistance,
...
}
The way I hold all of the functions in a single array is by making them all of type
void* function (void*)
and casting data inside the function.
This only allows up to one parameter for a function (you can, however, also pass arrays or linked lists) but for the game I'm working on this seems to be possible remembering these are only for the condition/action functions and that the functions can get access to member variables of the NPC's anyway.

Hope this has helped, else please ask again .

But thats why the saved games are stuffed when they produce a new version ... very annoying

Advertisement
Is there anything else in your entity structure that hints as to what type of attack is currently activated (hence which function it points to)? You should be able to write the structure intact (function pointers and all) and when you read it back, disregard the value read for the function pointers (since it's meaningless), instead re-assign them to the corresponding function based on the entity state.

If your entity doesnt have any information about its state, you might consider adding one. It'll save you lots of trouble (like the problem you're having now)

foofightr

You could just use c++..
I have a problem involving function pointers in my entity code. Each entity has several pointers to trigger or attack functions, which can be activated by certain events. They are set up when the entities are initialised but can be modified during execution. The problem is that when I go to save my entity states to a file for later retrieval, I cannot simply write the function pointers with the rest of the structure, since they will obviously point to bad locations when I reload. I've considered several options here but they all involve a lot of work or a lot of messy code. Using an ID number instead of a function pointer is good (it saves me memory too, since I won't need more then 65535 different functions), but writing code to enumerate the functions and then having to remember what each ones number is would be a major hassle not to mention making the code horribly unreadable. Using defines is an option but it would make adding new functions a hassle. Dynamically allocating the numbers at run time would be fine, but they do of course need to be allocated the same numbers each time, which might prove difficult to do. The best solution I can see at this time is to initialise a set of data when the program is run that ties each function pointer to a number, and when the entities are saved, write the number in place of the pointer. When the entities are reloaded, the numbers can be looked up again and the new, valid function pointers retrieved and assigned. The hassle of initialising each function is not too bad, but I'm sure there is an easier way to do this. If anyone has any suggestions whatsoever, please reply, I'd be happy to hear from you.

Thanks

Starwynd

Thanks for all the suggestions guys.

MikeD: My situation is slightly different to yours, but I can see what you're getting at. I think my proposed fix works better for my case (I don't need/want to have any contact with the name of the function). Thanks for the advice.

Foofightr: No, nothing in my structure does tell what sort of function it is... but it's not only used for attacks, it can also be used for custom events, and adding in a flag for every event is going to be too much overhead and too much of a hassle.

Jason: How does C++ help me here? Even if I was to use a class for my entities, I'd still need to use function pointers and I'd still have this same problem.
In any case, I think I have a viable solution for now, which is that I have an function run at the start of my code in which I call a function add_fn for each function I'm going to use (what a mouthful!). This assigns each function an ID number and adds them into an array, which I can later look up to retrieve the numbers when saving the map, and convert it back into a valid function pointer when it is reloaded. It also requires a relatively small amount of memory, and the effort of adding each function is minimal (I also have the map saving code warn me if it comes across a function I forgot to add).

Thanks!

Starwynd

This topic is closed to new replies.

Advertisement