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 on other sites

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

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)
{

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)
{

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 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 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 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 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 on other sites

@Andreas Jonsson

That's a good solution.

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

Thanks for the assist.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628367
• Total Posts
2982278

• 10
• 9
• 13
• 24
• 11