Advertisement

Instantiating script class from method of another class?

Started by April 27, 2020 06:52 PM
5 comments, last by henris42 4 years, 6 months ago

Hi, This might be a silly newbie question, but how can I in script instatiate a class by calling a method from another class? Say in script I'd like to do something like this:

Foo a;
Bar b = a.getNewBar(); 

In C++, I'd have something like this:

class Bar {
	Bar();
	void Addref();
	void ReleaseRef();
};

class Foo {
	Foo();
	void Addref();
	void ReleaseRef();
	Bar* getNewBar() {return new Bar();}
};

I've tried to find a suitable function registration to do that, but I'm not getting anything working. I'm assuming that class Bar should be without factory, is that correct? (There is no need to create plain Bar object)

I was also wondering if getNewBar() should return asIScriptObject*, but what then I should do with the factory, as described in the docs?

There is a valid reason for this structure, in the actual use case there are 3 levels of hierarchy coming from external API this way, and I'd like to expose this API to the script as nicely as possible.

I posted this in Angelcode, but it ended up in AI.. :( I cannot fix it.

Advertisement

The Bar type doesn't need to have the factory registered if you don't want the script to be able to create new instances without calling getNewBar. The getNewBar() method should be registered like this:

engine->RegisterObjectType("Bar", 0, asOBJ_REF);

engine->RegisterObjectType("Foo", 0, asOBJ_REF);
engine->RegisterObjectBehaviour("Foo", "Foo @f()", asFUNCTION(Foo_Factory), asCALL_CDECL);
engine->RegisterObjectMethod("Foo", “Bar @getNewBar()”, asMETHOD(Foo::getNewBar), asCALL_THISCALL);

(I left out the registration of the addref/release behaviours for brevity)

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks, I got it working, with Bar@ b = a.getNewBar();

Other question, about delegates. I'm trying to replicate C++ API in Angelscript, like this:

class Controller {
	void didConnect() {
		print("hello");
	}
	void init() {
		device.setConnectDelegate(didConnect);
	}
	Device device;
}
void main() {
Controller c;
c.init();
}

I implemented the C++ side of delegate as in example, and it worked well until I wrapped the API into a class. Class structure like this would be required, otherwise this would become spaghetti…

Running this I get

ERR : No matching signatures to …

ERR : Can't pass class method as arg directly. Use a delegate object instead

Any pointers on correct way to get something like implemented?

Delegates needs to be composed of both the object and method.

http://www.angelcode.com/angelscript/sdk/docs/manual/doc_datatypes_funcptr.html#doc_datatypes_delegate

I'm not sure what the name of your funcdef is, but assuming it is CALLBACK the syntax would be:

device.setConnectDelegate(CALLBACK(this.didConnect));

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Yes, that made trick! Its actually quite obvious when you think about it.. ? Thanks!

This topic is closed to new replies.

Advertisement