Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

_Joco_

Function Pointer Question...

This topic is 5560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm currently working on a scripting language for my 2d arcade game, and I am just about to finish it but I've got a little problem with function pointers. If I want to call a function from the script, wich isn't declared there, I need a pointer to the func. So I'd need a class like this : class SysFunction { char function return type list parameters function pointer } Of course the func. pointer is dependent from the first 2 variables. So my question would be how I could write a class like SysFunction wich can handle all types of functions. could someone help plz ? ...and I'm using C++... [edited by - _Joco_ on April 1, 2003 8:28:16 AM] [edited by - _Joco_ on April 1, 2003 8:28:58 AM]

Share this post


Link to post
Share on other sites
Advertisement
Personally, I would create a parameter class which contains a list of all the parameters:

class Parameters
{
public:
const AnyType &operator [] (const int parameter_index) const
{
check parameter exists
return parameter indexed by argument;
}
private:
list < AnyType >
m_parameters;
};

and then your functions called by the scripting system would have the format:

void ScriptFunction (const Parameters ¶ms);

and I'd defined the class AnyType to be similar to VBs Variant type (i.e. any type). If you want multiple return values (like Lua) then add a return argument list to the function parameters and have a void return.

Skizz

[edited by - Skizz on April 1, 2003 8:50:34 AM]

Share this post


Link to post
Share on other sites
Actually, most dynamically-typed programming languages implemented in statically-typed languages like C and C++ use a class hierarchy to represent types, deriving all types from an abstract base class and thus allowing transparent function invocation and dispatch:

class Scr_Type
{
protected:
virtual Scr_Type() = 0;
virtual ~Scr_Type() = 0;
virtual std::string Type() { return typename; }
std::string typename;
};

Or something similar. Python, for example, derives all types from PyObject. Being written in C, though, it doesn''t use OO inheritance but some properties-based mechanism (similar to how some games are data-driven).

This is similar to simply storing the typenames as strings and using object factories to invoke the appropriate constructors and translators.

Share this post


Link to post
Share on other sites
Exactly. You need a function that takes a list of dynamic types and a mechanism to return zero or more values. When creating your dynamic type, add some type checking macros that can be removed in a final, bug free build by (un)defining a global preprocessor symbol. i.e. when a string is being accessed as an int, do an assert or whatever.

Skizz

P.S. Anyone else seeing that bizarre character in my earlier post. Never seen that one before. And 'Communism for Beginners' - there's something afoot here methinks.

[edited by - Skizz on April 1, 2003 9:32:49 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!