Advertisement

Problem creating AngelScript object in C++

Started by January 23, 2010 09:48 PM
2 comments, last by WitchLord 15 years, 1 month ago
I am trying to create an AngelScript object that inherits from an interface defined in c++. When it is created in c++ I can not cast it, but when its created in AngelScript I can cast it. My AngelScript Player class that inherits the interface BaseEntity that is defined in C++;

class Player : BaseEntity
{
	Player()
	{
		
	}
}

My test script

#include "Player.as"
void Run()
{
	BaseEntity@ gameCreated = entity.CreateEntityByName("player");
	BaseEntity@ localCreated = Player();
	Player@ castedGameCreated = cast<Player>(gameCreated);
	print("castedGameCreated");
	if( castedGameCreated !is null )
	{
		print(" is a player\n");
	}
	else
	{ 
		print(" is not a player\n");
	}
	
	Player@ castedLocalCreated = cast<Player>(localCreated);
	print("castedLocalCreated");
	if( castedLocalCreated !is null )
	{
		print(" is a player\n");
	}
	else
	{ 
		print(" is not a player\n");
	}


}

CreateEntityByName c++ function

asIScriptObject *EntityManager::CreateEntityByName(std::string &ent)
{
	printf("CreateEntityByName\n");
	ScriptEngine *sEngine = ScriptEngine::GetScriptEngine();
	asIScriptEngine *engine = sEngine->GetEngine();
	asIScriptContext *m_context = engine->CreateContext();
	asIScriptModule *BaseEntityModule = sEngine->BuildScript("Player", "Player.as");
	asIObjectType *type = engine->GetObjectTypeById(BaseEntityModule->GetTypeIdByDecl("Player"));
	int funcid = type->GetFactoryIdByIndex(0);
	m_context->Prepare( funcid );
	m_context->Execute();
	asIScriptObject *m_scriptObject = static_cast<asIScriptObject *>(m_context->GetReturnObject());

	return m_scriptObject;
}

The output of the tester script is castedGameCreated is not a player castedLocalCreated is a player Am I creating the object incorrectly?
The problem is that for each object you create with EntityManager::CreateEntityByName() you build a new module. Classes defined in different modules are not the same, even though they may have the exact same implementation.

The BaseEntity interface returned by the CreateEntityByName cannot be cast to the Player type, because it is not the same Player type as the one implemented in the current module. Using a class implemented in a different module has to be done through interfaces.


Do you really need to build a new module for each entity? The only reason for that is if you do not want the entities to share global variables.

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

Advertisement
Is it possible to convert an object to a different version of it's script? The reason I would want to do this is for having scripts reload when they are changed.
Converting one script class to another is currently not possible, even if all the members are the same. This is because the type id and functions ids are all different.

Others are also doing what you want to do. What they do is to build the new module, then they manually enumerate all variables and types in the old module to copy their values to the new module. It is functional, but requires a bit of work to implement.

I have ideas to implement a way to allow a script to be built over an existing module and only replace the bytecode within functions without replacing the function ids themselves. It will be a while before I can implement this though.

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