• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BlackMoons

Registering an object property

9 posts in this topic

Hi, I am having a small problem.

I have 'ObjectRef' registered as a reference type

It has the standard ref counting/addref/release functionality.

I also have a copy constructor registered as:

r = scriptEngine->RegisterObjectMethod("ObjectRef", "ObjectRef@ opAssign(const ObjectRef &in other)", asMETHOD(ObjectRef,operator=), asCALL_THISCALL); assert( r >= 0 );

 

And right now I am encountering two problems...

I have an angelscript class like this:

class FireSource
{
    ObjectRef @ mOwner;
    Light mLight;
    FireSource()
    {
        gConsole.AddItem("Constructed!");
    }

    void Initilize(ObjectRef @ ref)
    {
        gConsole.AddItem("Testing!");
        mOwner = ref;
        gConsole.AddItem("Testing!1.01");
        mLight.mOwner = ref;
        gConsole.AddItem("Testing!1.1");
    }
};

I construct the class on my C++ side and call Initilize:


        asIScriptFunction * init = objDetails->mUserDataType->GetMethodByDecl("void Initilize(ObjectRef @)");

        Engine::mScript->mContext->Prepare(init);
        ObjectRef * ref = new ObjectRef();
        ref->SetObject(this);
        Engine::mScript->mContext->SetArgObject(0, ref);
        Engine::mScript->mContext->SetObject(mObjectData);
        // Execute the call
        Engine::mScript->mContext->Execute();
        ref->Release();

I can confirm the object is constructed, and the first 'gConsole.AddItem("Testing!");' is executed.

After that, it returns from "Engine::mScript->mContext->Execute();" without calling the ObjectRef copy assignment or any further instructions, No error is logged or reported?

 

If I comment out '//mOwner = ref;', it continues on to call the ObjectRef copy constructor for the line "mLight.mOwner = ref;"

 

And then it crashes. (MSVC2010)

"File crt\src\dbgdel.cpp line 52

Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUSE)"

 

 

For reference, the C++ side of Light is (its also registered as ref type)

class Light : public ScriptRef
{
public:
    ObjectRef mOwner;
};
...
r = scriptEngine->RegisterObjectProperty("Light", "ObjectRef mOwner", asOFFSET(Light,mOwner)); assert( r >= 0 );

So yea, two problems, I am not exactly sure what I did wrong.

Edited by BlackMoons
0

Share this post


Link to post
Share on other sites

PS, why does virtual int asIScriptContext::SetObject(void*obj) take a void*? Shouldn't it take a asIScriptObject* instead for slightly more type safety?

0

Share this post


Link to post
Share on other sites

I think I figured out the first problem was the line should of been '@mOwner = @ref;'

But I am still wondering about the crash when assigning "mLight.mOwner = ref;"

0

Share this post


Link to post
Share on other sites

Oh, Here a call stack on the crash:

     Client.exe!operator delete(void * pUserData)  Line 52 + 0x51 bytes    C++
     Client.exe!ObjectRef::`scalar deleting destructor'()  + 0x3c bytes    C++
     Client.exe!ScriptRef::Release()  Line 127 + 0x34 bytes    C++
     Client.exe!asCScriptEngine::CallObjectMethod(void * obj, asSSystemFunctionInterface * i, asCScriptFunction * s)  Line 3568 + 0x8 bytes    C++
     Client.exe!asCScriptEngine::CallObjectMethod(void * obj, int func)  Line 3522    C++
     Client.exe!asCContext::ExecuteNext()  Line 2522    C++
     Client.exe!asCContext::Execute()  Line 1144 + 0x8 bytes    C++

It seems to be calling release on mLight.mOwner but why would it do that?

<edit> Did some more testing. Increasing mOwner's refcount in mLight's constructor didn't help anything. So I have no idea what ObjectRef it is releasing or why.

<edit> Updated to 2.27.1, Still crashes.

Adding AddRef() to ObjectRef's constructor solves the crash, but most likely results in memory leaks.

Incrementing ref's ref count or Light.mOwner's ref count did not fix the crash.

<edit>

Ok, I think I figured it out..

the copy constructor was defined like "ObjectRef@ opAssign(const ObjectRef &in other)" meaning it returned a handle that would then be released since my script does not use it right? And my copy constructor if defined like that should increment the handle it returns?

 

Changing it to "ObjectRef& opAssign(const ObjectRef &in other)" seems to have fixed it. But I am wondering is this the correct fix? Or does my copy constructor need to increment the handle it returns instead? I would rather not make duplicate copy constructors for C++ and Angelscript.

Edited by BlackMoons
0

Share this post


Link to post
Share on other sites

There are lots of questions and errors (some of it you appear to have figured out on your already). Let me try to address them all in this post in no specific order.

 

 

1. The script code should probably do handle assignments rather than value assignments. With value assignment the script will attempt to call the opAssign method of the left-hand object, but if the handle in the left-hand object is null then you'll get a script exception. With a handle assignment you set the left-hand handle to refer to the same object given in the right-hand expression.

 

    void Initilize(ObjectRef @ ref)
    {
        gConsole.AddItem("Testing!");
        @mOwner = ref;
        gConsole.AddItem("Testing!1.01");
        @mLight.mOwner = ref;
        gConsole.AddItem("Testing!1.1");
    }

 

2. You are not checking the return code on the context method calls. If you did you would most likely see that the Execute() method returns asEXECUTION_EXCEPTION ( == 3 ), rather than asEXECUTION_FINISHED ( == 0 ). When Execute() returns asEXECUTION_EXCEPTION the script execution failed, and you can use the methods on the context to get information about the location of the error (see e.g. the PrintException helper function in the add-ons).

 

3. Is the Light::mOwner on the C++ side really an instance of the ObjectRef type? Shouldn't it be a pointer to an ObjectRef type? If ObjectRef is a reference counted type, then you should probably not be using it a a value type. Otherwise you may get dangling pointers as the ObjectRef instance can be destroyed while there are still active references to it. Or in your case, it appears that the Release method of ObjectRef is attempting to delete the member of the Light object, which causes the crash as the ObjectRef wasn't allocated by itself.

 

4. asIScriptContext::SetObject(void*obj) takes a void* because it is not always asIScriptObject pointers that are given. You can use asIScriptContext to call object methods on registered types too.

 

5. opAssign is the assignment operator, not the copy constructor. The copy constructor for ObjectRef would look something like this (if you have it):

 

ObjectRef::ObjectRef(const ObjectRef &other)  
{
   // Set the refCount of the new instance to 1
   refCount = 1;
  
   // Copy the content from the other instance (except the refCount)
   ...
}

 

6. Normally the opAssign method should be declared to return the object by reference, and not by handle. If you register it to return the object by handle, then the C++ operator= method must increase the refCount of the before returning, which I assume isn't what you want.

 

 

 

I recommend you read the following entries in the manual to get a better understanding on how handles work:

 

 

Let me know if this clears up your doubts or if you need any further explanations.

 

Regards,

Andreas

0

Share this post


Link to post
Share on other sites

1: Ah.

 

2: Great, I'll work on a system for wrapping all my executes into my error reporting. Man even run time error reporting, Angelscript is great!

 

3: Yes its really an instance of ObjectRef, I thought making it an instance would make things a little cleaner.. One less thing to allocate and keep track of. The problem was returning handle of the ObjectRef in opAssign causing it to deincrement and release itself. mLight never tries to release its mOwner because it knows its local and just needs to be cleaned up as local. You do bring up a valid point about dangling pointers however. I might look into changing that for that reason.

 

4: I can call my C++ code via angelscript via C++ by passing registered objects instead of Angelscript objects? I have no idea why I would want to do that, but that seems cool. Very unified. Wait... That means I can transparently use registered C++ classes or user designed Angelscript classes on a class by class basis?... Awesome!

 

5: sorry called it the wrong thing. Meant assignment operator.

 

6: Ahhh ok. I copied the opAssign prototype from the string registration function, but that is a value type so I guess it was a bad prototype to copy. I have read the entire Angelscript documentation and thank you for taking the time to write it all. Its just the difference between handles, references, value types and the exact details on ref counting is taking a little time to sink in.

 

Some details of the documentation are kind of hidden on only one page and that makes it a little harder to use as a quick reference. While I understand you don't want to be redundant in your documentation, Maybe some more hyperlinking between related pages would help? "See also:" section maybe?

 

Is there any difference in returning a handle instead of a reference other then angelscript will decrement a handles ref count when it expires? I assume if one takes a handle of the returned ref, its ref count will be incremented?

 

Are references there mainly to support C++'s typical 'ignorance' of ref counting?

Edited by BlackMoons
0

Share this post


Link to post
Share on other sites

I try to use 'see also' links in the documentation. But I really am a programmer first hand, and as most programmers I'm not really that good at writing documentation. Any feedback on where improvements is needed is most welcome. :)

 

Perhaps the real difference between handles and references is that handles can be null while references cannot, and handles can be reassigned and references cannot. This is similar to pointers and references in C++.

0

Share this post


Link to post
Share on other sites

Oh trust me, you are amazing at writing documentation. This is by far the best documented open source library I am using. I love going to read Angelscript documentation because it usually answers my question and even has little source tidbits showing usage.

 

But as a tip, Maybe http://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_handle.html could link back to http://www.angelcode.com/angelscript/sdk/docs/manual/doc_obj_handle.html

 

One links to the other, but no links back, in fact no links leave that page at all.

0

Share this post


Link to post
Share on other sites

Thanks. I believe the documentation is only this detailed because I'm treating it much like the library itself. With each release I try to add a little more information or little tweaks here and there. It's possible to write quite a lot in 10 years, despite putting down only a few hours with each release.

 

Thanks for the feedback. I'll consider adding this back-link.

 

I haven't done so before because I've tried to keep the script language section free of links into the application developer sections because the script writers doesn't really need to know how things work under the hood. I should probably reconsider this entire decision.

0

Share this post


Link to post
Share on other sites

Understandable, It could confuse some since the C++ code is similar to the anglescript code if a scripter got into the wrong section of the documentation he might think he has to (or can) do things that must be done on the application side.

 

Reconsider the decision for sure, although I am not sure what you should decide as there is pros and cons for each. Maybe making the links back into the application section clearly linked as "For application developers, Also see:" or something similar?

0

Share this post


Link to post
Share on other sites

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  
Followers 0