Sign in to follow this  

Passing object to script function

This topic is 4375 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

Heya - I'm currently experimenting with the library what I want to do and how to do it - and already got a problem :) In short, what I understood from object registration is when I don't register any behaviour and set the object to 0 size, it is available to the script only via a) function parameter when called from C or b) as a return parameter from a C function, right? b) I yet haven't tried since I already failed at a) ;)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <angelscript.h>

// ---------------------------------------------------------------------------

class ObjectBase
{
public:
	ObjectBase() { mInteger = 0x1000; }
	virtual ~ObjectBase() { }
	
	int getInteger() { return mInteger; }
	
private:
	int mInteger;
};

// ---------------------------------------------------------------------------

class ObjectChild: public ObjectBase
{
public:
	ObjectChild() { }
	virtual ~ObjectChild() { }
};

// ---------------------------------------------------------------------------

void Out(int i)
{
	printf("Out() i = 0x%08X\n", i);
}

// ---------------------------------------------------------------------------

int main(void)
{
	asIScriptEngine *pEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	
	{	// set up
		char const *test = 
			"void test(Object obj)"
			"{"
			"   Out(obj.getInteger());"
			"}";
	
		pEngine->RegisterObjectType("Object", 0, asOBJ_CLASS);
		pEngine->RegisterObjectMethod("Object", "int getInteger()", asMETHOD(ObjectBase, getInteger), asCALL_THISCALL);
			
		pEngine->RegisterGlobalFunction("void Out(int i)", asFUNCTION(Out), asCALL_CDECL);
		pEngine->AddScriptSection(0, "test", test, strlen(test), 0, false);
		pEngine->Build(0);
	}
	
	{	// execution
		ObjectChild obj;
		asIScriptContext *pContext = pEngine->CreateContext();
		int id = pEngine->GetFunctionIDByDecl(0, "void test(Object obj)");
		
		pContext->Prepare(id);
		pContext->SetArgObject(0, &obj);
		pContext->Execute();

		pContext->Release();
	}
	
	pEngine->Release();

	return 0;
}

The output I'm getting from Out() is any integer, but never 0x1000. What am I doing wrong? (having an obj from ObjectBase has the same (wrong) result - but I didn't expect it to be the problem anyway...)

Share this post


Link to post
Share on other sites
You'll need to write the script function to take a reference to the object.


void test(Object &inout obj)
{
Out(obj.getInteger());
}


There is a third way that the scripts can access these types of objects, and that is through registered global properties.

Regards,
Andreas

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
You'll need to write the script function to take a reference to the object.


void test(Object &inout obj)
{
Out(obj.getInteger());
}



Thanks for your response, but this code gives me a Build() error:
test (1, 1) : Info : Compiling void test(Object&inout)
test (1, 11) : Error : Parameter type can't be 'Object&'

I used the same program as above, just added the MessageStream output -- using Ver.2.4.1c.
(&in generates the same error)

Share this post


Link to post
Share on other sites
Oops! Not even I get it right all the time. ;)

Parameter references are still complicated to use in AngelScript, because they cannot be used the same way as in C++, as that is not safe enough for scripting. I've improved the way they work in 2.5.0 a little, and in a future version I will improve it even more (I hope).

If you could add reference counting to the Object type you would be able to use the object handles to pass the object by pointer. In order for objects to support object handles you must register the ADDREF and RELEASE behaviours.

In this case you would write your script function as such:


void test(Object@ obj)
{
Out(obj.getInteger());
}

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
Oops! Not even I get it right all the time. ;)

Heh - no problem ;)
Thanks for the help, although I might get trouble trying to add reference counting to the objects I plan to use...

Share this post


Link to post
Share on other sites
Perhaps you could make a light wrapper for the objects in order to add reference counting? The asCScriptString is a good example on how this can be done, it wraps the std::string in a way that most functions taking a std::string by parameter reference still can be used without any changes.

Otherwise I'm afraid it will be very difficult to pass an object with size 0 by parameter. The size 0 objects were mostly intended to be handled by object handle, or be registered as singletons.

Regards,
Andreas

Share this post


Link to post
Share on other sites

This topic is 4375 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this