Sign in to follow this  
MahanGM

How to get public member variables from script class in AngelScript

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@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?

Edited by MahanGM

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by MahanGM

Share this post


Link to post
Share on other sites

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.

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