• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TechRogue

Template methods

3 posts in this topic

I have a class which is responsible for creating instances of my component classes, registered as a singleton. Currently it looks like this:

[source lang = "cpp"]
// IComponent is an abstract C++ class that all of my components extend in the application
IComponent@ animation = componentFactory.get("SpriteAnimation");
[/source]

Obviously this isn't ideal; for one thing, errors won't be caught until the line is executed. I'm wondering if there's any way for a method to take a template parameter, like this:

[source lang = "cpp"]
IComponent@ animation = componentFactory.get<SpriteAnimation>(); // returns an instance of SpriteAnimation
[/source]

All I can find in the docs is a way to register template types, which won't help me with this. Is there something I'm missing, or could this be added in a future release?

Thanks Edited by Jake Albano
0

Share this post


Link to post
Share on other sites
You could create a special template type, that will simply provide type info rather than being a container itself.

It might for example look like this:

[code]
IComponent@ animation = componentFactory.get(type<SpriteAnimation>());
[/code]

The 'type' object would then be instanciated similarly to how the CScriptArray is instanciated, and would allow the C++ application to determine what type is desired.

You could even use the template callback behaviour to prevent the script from instanciating types your application doesn't support.

This is actually a pretty cool idea. I might implement this as an add-on. The type object could even provide further information on the type, for example allow a script to enumerate properties and class methods, etc.
0

Share this post


Link to post
Share on other sites
That's an interesting approach, and one I probably wouldn't have thought of. The one issue I can see is that I would have to create a specific type for each situation, or it still wouldn't be caught at compile time, but that's certainly better than the way I've been doing it so far. Thanks for the help!
0

Share this post


Link to post
Share on other sites
I've actually exposed asIObjectType this way to scripts and it works fairly well. The only thing I dislike is that you're (Jake) not writing idiomatic code, but with that said it's insanely flexible and can be implemented wholly with pre-implemented features.

EDIT(2): My implementation. It's not going to be a perfect cut-and-paste job, but this should get the ball rolling.
[code]/*==================================================================*\
AngelscriptReflection.cpp
------------------------------------------------------------------
Purpose:

------------------------------------------------------------------
©2010-2012 Eldritch Entertainment, LLC.
\*==================================================================*/

//==================================================================//
// INCLUDES
//==================================================================//
#include <Scripting/Angelscript/AngelscriptReflection.hpp>
#include <Scripting/Angelscript/AngelscriptContextExtraData.hpp>
#include <Scripting/Angelscript/AngelscriptCommon.hpp>
#include <Util/Containers/UTF8String.hpp>
#include <Util/Assert.hpp>
#include <angelscript.h>
//------------------------------------------------------------------//
//==================================================================//
// LIBRARIES
//==================================================================//
ET_LINK_LIBRARY( "angelscript.lib" )
//------------------------------------------------------------------//
using namespace ::Eldritch2;
using namespace ::Eldritch2::Scripting;
using namespace ::Eldritch2::Util;
#define TYPE "Type"
#define FUNCTION "Function"
#define MEMBER "Member"
#define ATTRIBUTE "Attribute"
namespace
{
static const char typeName[] = TYPE;
static const char functionName[] = FUNCTION;
static const char memberName[] = MEMBER;
static const char attributeName[] = ATTRIBUTE;
// ---------------------------------------------------
static UTF8String TypeGetNameProperty( asIObjectType* const thisPtr )
{
AngelscriptContextExtraData* const contextExtraData = static_cast<AngelscriptContextExtraData*>( asGetActiveContext()->GetUserData() );
return UTF8String( thisPtr->GetName(), contextExtraData->allocator );
}
// ---------------------------------------------------
static UTF8String TypeGetNamespaceProperty( asIObjectType* const thisPtr )
{
AngelscriptContextExtraData* const contextExtraData = static_cast<AngelscriptContextExtraData*>( asGetActiveContext()->GetUserData() );
return UTF8String( thisPtr->GetNamespace(), contextExtraData->allocator );
}
// ---------------------------------------------------
static ETNoAliasHint asIObjectType* TypeGetBaseTypeProperty( asIObjectType* const thisPtr )
{
asIObjectType* const basePtr = thisPtr->GetBaseType();
if( ETBranchLikelyHint( nullptr != basePtr ) )
{
basePtr->AddRef();
}
return basePtr;
}
// ---------------------------------------------------
static ETNoAliasHint asIObjectType* TypeGetSubtypeProperty( asIObjectType* const thisPtr )
{
asIObjectType* const basePtr = thisPtr->GetSubType();
if( ETBranchLikelyHint( nullptr != basePtr ) )
{
basePtr->AddRef();
}
return basePtr;
}
// ---------------------------------------------------
static ETNoAliasHint bool ETCDecl TypeIsInterface( asIObjectType* const thisPtr )
{
return ( ( 0u == thisPtr->GetSize() ) & !!( asOBJ_SCRIPT_OBJECT & thisPtr->GetFlags() ) );
}
// ---------------------------------------------------
static ETNoAliasHint bool ETCDecl TypeIsFinal( asIObjectType* const thisPtr )
{
return !!( asOBJ_NOINHERIT & thisPtr->GetFlags() );
}
// ---------------------------------------------------
static ETNoAliasHint asIScriptFunction* ETCDecl TypeGetMethodProperty( asIObjectType* const thisPtr, uint32 methodIndex )
{
asIScriptFunction* const function = thisPtr->GetMethodByIndex( methodIndex );
if( ETBranchLikelyHint( nullptr != function ) )
{
function->AddRef();
}
return function;
}
// ---------------------------------------------------
static ETNoAliasHint asIObjectType* ETCDecl TypeOf( void* const object, int typeID )
{
ETUnreferencedParameter( object );
if( asIObjectType* const result = asGetActiveContext()->GetEngine()->GetObjectTypeById( typeID ) )
{
result->AddRef();
return result;
}
return nullptr;
}
// ---------------------------------------------------
static asIObjectType* ETCDecl GetTypeByName( const UTF8String& name )
{
asIScriptEngine* const scriptEngine = asGetActiveContext()->GetEngine();
AngelscriptNamespace typeNamespace;
AngelscriptClassName typeName;
if( ExtractClassPath( typeNamespace, typeName, name ) )
{
if( asIScriptModule* const scriptModule = scriptEngine->GetModule( typeNamespace ) )
{
asIObjectType* const objectType = scriptEngine->GetObjectTypeById( scriptModule->GetTypeIdByDecl( typeName ) );
if( objectType )
{
objectType->AddRef();
return objectType;
}
}
}
return nullptr;
}
// ---------------------------------------------------
static asIObjectType* ETCDecl GetTypeByName( const UTF8String& name, const UTF8String& package )
{
asIScriptEngine* const scriptEngine = asGetActiveContext()->GetEngine();
AngelscriptNamespace typeNamespace;
AngelscriptClassName typeName;
StrCpy( typeName, name );
StrCpy( typeNamespace, package );
if( asIScriptModule* const scriptModule = scriptEngine->GetModule( typeNamespace ) )
{
if( asIObjectType* const objectType = scriptEngine->GetObjectTypeById( scriptModule->GetTypeIdByDecl( typeName ) ) )
{
objectType->AddRef();
return objectType;
}
}
return nullptr;
}
} // anonymous namespace
namespace Eldritch2
{
namespace Scripting
{
void RegisterReflection( asIScriptEngine* const engine )
{
ETRuntimeVerification( 0 <= engine->RegisterObjectType( typeName, 0u, asOBJ_REF ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectType( functionName, 0u, asOBJ_REF ) );
// ETRuntimeVerification( 0 <= engine->RegisterObjectType( memberName, 0u, asOBJ_REF ) );
ETRuntimeVerification( 0 <= engine->RegisterInterface( attributeName ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( typeName, asBEHAVE_ADDREF, "void f()",
asMETHOD( asIObjectType, AddRef ), asCALL_THISCALL ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( typeName, asBEHAVE_RELEASE, "void f()",
asMETHOD( asIObjectType, Release ), asCALL_THISCALL ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( functionName, asBEHAVE_ADDREF, "void f()",
asMETHOD( asIScriptFunction, AddRef ), asCALL_THISCALL ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( functionName, asBEHAVE_RELEASE, "void f()",
asMETHOD( asIScriptFunction, Release ), asCALL_THISCALL ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, TYPE "@ get_BaseType() const",
asFUNCTIONPR( TypeGetBaseTypeProperty, ( asIObjectType* const ), asIObjectType* ), asCALL_CDECL_OBJFIRST ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, TYPE "@ get_Subtype() const",
asFUNCTIONPR( TypeGetSubtypeProperty, ( asIObjectType* const ), asIObjectType* ), asCALL_CDECL_OBJFIRST ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "string get_Name() const",
asFUNCTIONPR( TypeGetNameProperty, ( asIObjectType* const ), UTF8String ), asCALL_CDECL_OBJFIRST ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "string get_Namespace() const",
asFUNCTIONPR( TypeGetNamespaceProperty, ( asIObjectType* const ), UTF8String ), asCALL_CDECL_OBJFIRST ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool get_IsInterface() const",
asFUNCTIONPR( TypeIsInterface, ( asIObjectType* const ), bool ), asCALL_CDECL_OBJFIRST ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool get_IsFinal() const",
asFUNCTIONPR( TypeIsFinal, ( asIObjectType* const ), bool ), asCALL_CDECL_OBJFIRST ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool DerivesFrom( const " TYPE "@ ) const",
asMETHODPR( asIObjectType, DerivesFrom, ( const asIObjectType* ) const, bool ), asCALL_THISCALL ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool Implements( const " TYPE "@ ) const",
asMETHODPR( asIObjectType, Implements, ( const asIObjectType* ) const, bool ), asCALL_THISCALL ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "uint32 get_MethodCount() const",
asMETHODPR( asIObjectType, GetMethodCount, () const, asUINT ), asCALL_THISCALL ) );
ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, FUNCTION "@ get_Method( uint32 ) const",
asFUNCTIONPR( TypeGetMethodProperty, ( asIObjectType* const, uint32 ), asIScriptFunction* ), asCALL_CDECL_OBJFIRST ) );
ETRuntimeVerification( 0 <= engine->RegisterGlobalFunction( TYPE "@ TypeOf( ?&in )",
asFUNCTIONPR( TypeOf, ( void* const, int ), asIObjectType* ), asCALL_CDECL ) );
ETRuntimeVerification( 0 <= engine->RegisterGlobalFunction( TYPE "@ GetTypeByName( const string&in )",
asFUNCTIONPR( GetTypeByName, ( const UTF8String& ), asIObjectType* ), asCALL_CDECL ) );
ETRuntimeVerification( 0 <= engine->RegisterGlobalFunction( TYPE "@ GetTypeByName( const string&in, const string&in )",
asFUNCTIONPR( GetTypeByName, ( const UTF8String&, const UTF8String& ), asIObjectType* ), asCALL_CDECL ) );
}
} // namespace Scripting
} // namespace Eldritch2[/code]

EDIT 3: One thing I really, really would like to look into is attributes Like What C# Has™, on a totally unrelated note. I can think of a way to hack it together again using stock functionality, but on the subject of reflection it's really a powerful concept. Edited by InvalidPointer
1

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  
Followers 0