🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Getting assert upon calling script function

Started by
2 comments, last by WitchLord 10 years, 7 months ago

Hello

I want to pass native 'this' to script object


asIScriptObject* CUIScriptable::CreateScriptClass (const char* sClassName)
{
	dbg_assert(m_pObjectType == NULL);
	dbg_assert(m_pScriptObject == NULL);
	dbg_assert(m_pScriptContext == NULL);

	int Res;

	//context
	m_pScriptContext = GetScriptManager()->GetEngine()->CreateContext();
	dbg_assert(m_pScriptContext);

	//object type
	asIScriptModule* pModule = GetScriptManager()->GetEngine()->GetModule(0);
	dbg_assert(pModule);

	m_pObjectType = GetScriptManager()->GetEngine()->GetObjectTypeById(pModule->GetTypeIdByDecl(sClassName));
	dbg_assert(m_pObjectType);
	
	//script object
	asIScriptFunction* pFactory = m_pObjectType->GetFactoryByDecl(va("%s @%s()", sClassName, sClassName));
	dbg_assert(pFactory);

	Res = m_pScriptContext->Prepare(pFactory);
	dbg_assert(Res >= 0);

	Res = m_pScriptContext->Execute();
	dbg_assert(Res >= 0);

	m_pScriptObject = *(asIScriptObject**)m_pScriptContext->GetAddressOfReturnValue();
	dbg_assert(m_pScriptObject);
	
	// If you're going to store the object you must increase the reference,
	// otherwise it will be destroyed when the context is reused or destroyed.
//	m_pScriptObject->AddRef();

	//call SetThat()
	asIScriptFunction* pFunc = m_pObjectType->GetMethodByDecl("void SetThat (CUIScriptable@ That)"); dbg_assert(pFunc);
	Res = m_pScriptContext->Prepare(pFunc); dbg_assert(Res >= 0);
	Res = m_pScriptContext->SetArgObject(0, this); dbg_assert(Res >= 0);
	Res = m_pScriptContext->Execute(); dbg_assert(Res == asEXECUTION_FINISHED);
	
	//
	return m_pScriptObject;
}

sClassName is "CHUD_Test"

CHUD_Test.as:



#include 'CUIElement.as'

class CHUD_Test : CUIElement
{
}

CUIElement.as:



class CUIElement : IUIElement
{
	void SetThat (CUIScriptable@ That)
	{
		@that = @That;
	}

	CUIScriptable@ that;
}

it asserts during last Context::Execute() call, line 1140, comment says "This shouldn't happen" and m_currentFunction->funcType == asFUNC_VIRTUAL

Any ideas?

Thanks in advance

Advertisement

The problem is that you forgot to inform the context the object which the method will be called upon. There is also a bug in AngelScript since it didn't detect this and reported a proper error (I'll have this corrected a.s.a.p)

 // If you're going to store the object you must increase the reference,
// otherwise it will be destroyed when the context is reused or destroyed.
 
// Don't forget to add a reference to keep the object alive, otherwise the next call to Prepare will release and destroy the object
m_pScriptObject->AddRef();

//call SetThat()
asIScriptFunction* pFunc = m_pObjectType->GetMethodByDecl("void SetThat (CUIScriptable@ That)"); dbg_assert(pFunc);
Res = m_pScriptContext->Prepare(pFunc); dbg_assert(Res >= 0);
 
// Remember to set the object pointer when calling class methods
Res = m_pScriptContext->SetObject(m_pScriptObject); dbg_assert(Res >= 0);

Res = m_pScriptContext->SetArgObject(0, this); dbg_assert(Res >= 0);
Res = m_pScriptContext->Execute(); dbg_assert(Res == asEXECUTION_FINISHED);
 
 

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

Works like a charm!

Grand merci Andreas!

I've fixed the assert failure in revision 1781. asIScriptException will now return asEXECUTION_EXCEPTION with a "Null pointer access" error if one forgets to call SetObject for a class method call.

Thanks,

Andreas

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

This topic is closed to new replies.

Advertisement