Casting and Inheritance.

Started by
2 comments, last by WitchLord 9 years, 9 months ago

Whats the best way to get around this.

I have a C++ component class where some C++ implemented components inherit from.


class Component;
class Transform : public Component;
// etc...

When it was all just C++ only the application would also create and inherit from this component. However I faked the Component class in AS by doing.


class ASComponent
{
    Component @baseImpl; // C++ component
}

The issue comes when I wish to retrieve components as ASComponent doesn't inherit from Component.


Component@ getComponent(string componentName);

I doubt I'm the first to run into this issue.

Advertisement

Perhaps you can have a special C++ Component class that holds the pointer to the script class that implements the modified behaviour?

This specialized C++ Component can then have a method for retrieving the actual script class if necessary. You can possibly even implement it as a ref cast behaviour.

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

That sounds promising. I have some of that already.


class ScriptComponent : public Component;

Is the impl inside ASComponent, with asIScriptFunction for the engine callbacks.

Although i'm not sure how ref cast with asBEHAVE_REF_CAST or asBEHAVE_IMPLICIT_REF_CAST works with script types unless I've missed something.

You could implement the asBEHAVE_REF_CAST with the variable argument type similarly to what I've done in the generic CScriptHandle add-on:

Example:

// In script used as '@obj = cast<ASComponent>(Component);'
// Register as asBEHAVE_REF_CAST with declaration 'void f(?&out)'
void ScriptComponent::Cast(void **outRef, int typeId)
{
  // If we hold a null handle, then just return null
  if( m_type == 0 )
  {
    *outRef = 0;
    return;
  }
 
  // It is expected that the outRef is always a handle
  assert( typeId & asTYPEID_OBJHANDLE );
 
  // Compare the type id of the actual script object
  typeId &= ~asTYPEID_OBJHANDLE;
  asIScriptEngine  *engine = m_type->GetEngine();
  asIObjectType    *type   = engine->GetObjectTypeById(typeId);
 
  *outRef = 0;
 
  if( type == m_type )
  {
    // Must increase the ref count as we're returning a new reference to the object
    engine->AddRefScriptObject(m_ref, m_type);
    *outRef = m_ref;
  }
}

Regards,

Andreas

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

This topic is closed to new replies.

Advertisement