How to get public member variables from script class in AngelScript

Started by
6 comments, last by MahanGM 9 years, 3 months ago

I'm trying to get member variables declared in an anglescript script class from my host application. There is a function for getting the global properties, but none for getting script class properties.

Advertisement

Moving you to the AngelCode forum for this one -- you'll likely get better answers there! :)

- Jason Astle-Adams

Here is how you can get all variables of a script-class:


        Instance::Instance(asIScriptObject& object, asIObjectType& type, asIScriptContext& context) : m_pObject(&object), m_pType(&type),
            m_pContext(&context)
        {
            m_pObject->AddRef();

            const size_t numAttributes = type.GetPropertyCount();
            for(unsigned int i = 0; i < numAttributes; i++)
            {
                const char* pName = nullptr;
                int id;
                bool isPrivate, isReference;
                type.GetProperty(i, &pName, &id, &isPrivate, nullptr, &isReference, nullptr);

                if(!isPrivate && !isReference)
                {
                    const Attribute::Type attribType = Attribute::TypeFromId(id);
                    if(attribType != Attribute::Type::UNKNOWN)
                    {
                        void* pData = object.GetAddressOfProperty(i);

                        m_mAttributes[conv::ToW(pName)] = Attribute(pName, attribType, pData);
                    }
                }
            }
        }

You don't even need all of what I did here, but I posted the whole ctor so you can understand everything properly.

@jbadams Thanks man. I was looking for this forum.

@Juliean Thanks. I've seen GetProperty method in object type class before. I didn't test it then but I now have, but since my script classes are derived from another base class, this will give me all member variables declared in both classes. Is there a way to avoid this?

I haven't used AngleScript in a while now so I don't know if there is a way, maybe through one of the GetProperty-Paramters I've passed nullptr to? Regardless, why do you even wan't to exclude public/protected derived member variables? Unless GetProperty() also returns private variables of the base class, I would consider it important in any kind of reflection-system to be able to also modify the base class variables. Of course you might have a special use case, thats why I'm asking.

Generally speaking you're right, but my case is not general. I'm using a base class to provide some behind the scene benefits to script classes at runtime but don't want to involve them in my host app. I'm trying to get public member variables to architect a unity3d like scripting system where you can assign values to your public members before runtime and some other stuff. That's why.

I was checking GetProperty parameters now, there is nothing for this kind of stuff.

If you need to know which of the members were inherited from a base class, then you need to check if the same member is declared in the parent class too. You can get the parent class with the method asIObjectType::GetBaseType.

If you want to know if the member was declared as public you'll need to check the indicator isPrivate returned from asIObjectType::GetProperty. (Note, in 2.30.0 WIP I've added support for protected members too, so a second indicator isProtected is also available. If neither is true, then the member is public).

You may also want to look into using meta data in the script classes to for example allow the script to inform the editor what values are allowed. The CScriptBuilder add-on shows one way of how meta data can be implemented and collected from scripts, if you wish to use that.

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

@Andreas Jonsson

That's a good solution.

And by the way, I completely forgot about meta data.

Thanks for the assist.

This topic is closed to new replies.

Advertisement