Advertisement Jump to content
Sign in to follow this  
simps

Getting assert upon calling script function

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

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

Share this post


Link to post
Share on other sites
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);
 
 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!