🎉 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!

Yet another question about accessing objects from a script

Started by
3 comments, last by Risto Hietanen 18 years, 6 months ago
Hi all, Here comes yet another question regarding accessing objects and their variables/functions from a script. Here is the scenario: The application creates a number (say 10) of a certain object: CTestObject *m_instance_1 = new CTestObject; CTestObject *m_instance_2 = new CTestObject; ... ... ... CTestObject *m_instance_10 = new CTestObject; Script may not create any objects, it may only access objects already created by the application. Thus, the CTestObject class is registered to angelscript with size 0: m_scriptEngine->RegisterObjectType("CTestObject" , 0, asOBJ_CLASS); It also registers the addref and release behaviour as: m_scriptEngine->RegisterObjectBehaviour("CTestObject", asBEHAVE_ADDREF, "void f()", asFUNCTION(AddRef), asCALL_CDECL_OBJLAST); m_scriptEngine->RegisterObjectBehaviour("CTestObject", asBEHAVE_RELEASE, "void f()", asFUNCTION(Release), asCALL_CDECL_OBJLAST); Finnaly the application registers a member function for the CTestObject class as: m_scriptEngine->RegisterObjectMethod("CTestObject", "void SetUp(void)", asMETHOD(CTestObject, SetUp), asCALL_THISCALL); When the application has created the objects, each object shall call an scripted setup function passing a pointer to itself: ... ... m_context->SetArgObject(0, this); // Where this is the actual instace of the object. ... ... The scripted setup function shall then access a member function in the separate objects. The setup functions are defined in the script as void SetupObject(CTestObject @obj) { obj.SetUp(); } However...it all results in a crash and when i debug I get the following message: "Unhandled exception at 0x005c8ca6 in TestApplication.exe: 0xC000008C: Array bounds exceeded." and the debugger leads to line 640 of the angelscript file as_context.cpp So my question is: If scripts are not allowed to create objects but only modify existing ones, how should I implement this? Best Regards, Risto
Advertisement
What version of AngelScript are you using? Line 640 in as_context.cpp is just an 'else'.

From what I can see, you seem to have registered the object correctly. You're also calling the script function correctly, I believe. I'm guessing there is some other problem going on (and I'm not discarding a bug in AngelScript).

- Does your CTestObject constructor initialize the reference counter to 1?

- What array is being accessed when the exception is raised?


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

Hi,

I'm using version [2.4.1d DEBUG].

My addref and Release functions are empty dummy functions in the CTestObject class implemented as follows:

void CTestObject ::AddRef(){;}
void CTestObject ::Release(){;}

The application creates the objets and adds them to a list defined as:
std::list <CTestObject*> m_testObjects with an iterator defined as
std::list <CTestObject *>::iterator m_testObjects It;

The objects are never destroyed during the time the application runs. They are created in a "factory" by the application, added to the list and then the list is iterated through and the setup function for each object is called passing the pointer to the objects where the pointer to the object is set as:(*m_objectsVectorIt).

The debugger halted at line 640 but i'm not sure that 640 is the actual line where the problem did occur. But something related to the SetArgObject() function is messing up. I figure it has something to do with the pointer to the CTestObject.

Best Regards,
Risto

It's probably the line engine->CallObjectMethod(obj, beh->addref); that fails, which is the statement just before line 640.

You say that the AddRef method was defined as void CTestObject::AddRef() {;} but you register it with m_scriptEngine->RegisterObjectBehaviour("CTestObject", asBEHAVE_ADDREF, "void f()", asFUNCTION(AddRef), asCALL_CDECL_OBJLAST);. You should have registered it with:

m_scriptEngine->RegisterObjectBehaviour("CTestObject", asBEHAVE_ADDREF, "void f()", asMETHOD(CTestObject,AddRef), asCALL_THISCALL);


Other than this it looks like you're doing everything correctly. But just in case, have you verified that the obj variable used in the CallObjectMethod() call is a valid CTestObject pointer?

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

Hi!


Ahhhhhhhhhhrgh!!!! Silly me! I shall bang my head against my desk a few times :).
You were absolutely right. I registered the Addref and Release methods the wrong way. When I changed it according to Your suggestion everythings works perfectly!

Thanks a lot and have a Merry Christmas and a Happy New Year!

Best Regards,
Risto

This topic is closed to new replies.

Advertisement