Jump to content
  • Advertisement
Sign in to follow this  
MahanGM

How to get public member variables from script class in AngelScript

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

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
Advertisement

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

@Andreas Jonsson

 

That's a good solution.

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

 

Thanks for the assist.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!