• Advertisement
Sign in to follow this  

asINVALID_ARG troubles

This topic is 2004 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'll currently run into a dead end trying to fix this error. I'm trying to pass a asIScriptObject ("*asObj") to the script and keep receiving the asINVALID_ARG return code. The factory function returns a valid pointer to the asIScriptObject and everything runs smoothly until I pass that same pointer to the script engine for the "update function".
I would be more than happy to post more code as needed to help.
Here's some of the revelant source code

C++ file "Script_Manager.cpp"
[source lang="java"]
void Script_Manager::init()
{
//...code left out for simplicity....//
// Register the object methods
r = ScriptEngine->RegisterObjectMethod("SceneNode", "vector3 &getPosition() const", asMETHOD(SceneNode, getPosition), asCALL_THISCALL); assert( r >= 0);
r = ScriptEngine->RegisterObjectMethod("SceneNode", "void setPosition(const vector3 &in)", asMETHODPR(SceneNode, setPosition, (const Vector3 &pos), void), asCALL_THISCALL); assert( r >= 0);

/////////GAME MANAGER REGISTERING//////////
//Register all the reference types so the Script ScriptEngine can interpret them in the scripts
//Register the 'GameManager' which is really an alias for the BaseApplication Singleton class
r = ScriptEngine->RegisterObjectType("GameManager", 0, asOBJ_REF | asOBJ_NOCOUNT);

//////////GAME ENTITY REGISTERING//////////
//Register the 'GameEntity' type which is really an alias for the Game_Entity class
r = ScriptEngine->RegisterObjectType("GameEntity", 0, asOBJ_REF);

//Register the 'GameEntity' addRef/release behaviours
r = ScriptEngine->RegisterObjectBehaviour("GameEntity", asBEHAVE_ADDREF, "void addRef()", asMETHOD(Game_Entity, addRef), asCALL_THISCALL);
r = ScriptEngine->RegisterObjectBehaviour("GameEntity", asBEHAVE_RELEASE, "void release()", asMETHOD(Game_Entity, release), asCALL_THISCALL);
//r = ScriptEngine->RegisterObjectMethod("GameEntity", "void setID(int newID)", asMETHOD(Game_Entity, setID), asCALL_THISCALL);
r = ScriptEngine->RegisterObjectMethod("GameEntity", "SceneNode@ getSceneNode()", asMETHOD(Game_Entity, getSceneNode), asCALL_THISCALL); assert( r >= 0);

//local instance that shouldn't be used outside of this 'init' function
CScriptBuilder ScriptBuilder;

ScriptBuilder.StartNewModule(ScriptEngine, "ScriptModule");

r = ScriptBuilder.AddSectionFromFile("apple.as");

r = ScriptBuilder.BuildModule();

ScriptModule = ScriptEngine->GetModule("ScriptModule");

//create a pointer to a asIScriptContext
ctx = ScriptEngine->CreateContext();
ctx->SetExceptionCallback(asFUNCTION(ExceptionCallback), 0, asCALL_CDECL);

// Get the object type
asIObjectType *type = ScriptEngine->GetObjectTypeById(ScriptModule->GetTypeIdByDecl("Player"));

ScriptFunction = type->GetFactoryByDecl("Player@ Player(GameEntity@ Entity)");

UpdateFunction = type->GetMethodByDecl("void Update()");
}
[/source]

C++ file "Entity_Manager.cpp"
[source lang="cpp"]void Entity_Manager::addGame_Entity(Game_Entity* addedEntity)
{
EntityList.push_back(addedEntity);
addedEntity->setID(EntityList.size());
}

void Entity_Manager::spawnGame_Entity(BaseApplication* GameManager, std::string filename, Ogre::Vector3 pos)
{
Game_Entity* entity = new Game_Entity();
entity->gEntity = GameManager->getSceneMgr()->createEntity(filename, filename + ".mesh");
entity->gSceneNode = GameManager->getSceneMgr()->getRootSceneNode()->createChildSceneNode();
entity->gSceneNode->attachObject(entity->gEntity);
entity->init(GameManager);
//set the spawn position
entity->gSceneNode->setPosition(pos);

addGame_Entity(entity);
}

//This is called in the Gameplay State... (left out for simplicity)
void Entity_Manager::update(BaseApplication* GameManager)
{
for(EntityIterator = EntityList.begin(); EntityIterator != EntityList.end(); ++EntityIterator)
{
(*EntityIterator)->update(GameManager);
}
}[/source]

C++ file "Game_Entity.cpp"
[source lang="cpp"]void Game_Entity::init(BaseApplication* GameManager)
{
int result;
GameManager->getScriptManager()->getScriptContext()->Prepare(GameManager->getScriptManager()->getFactoryFunction());
result = GameManager->getScriptManager()->getScriptContext()->SetArgObject(0, this);
result = GameManager->getScriptManager()->getScriptContext()->Execute();
asObj = *(asIScriptObject**)GameManager->getScriptManager()->getScriptContext()->GetAddressOfReturnValue();

asObj->AddRef();
}

void Game_Entity::update(BaseApplication* GameManager)
{
int result;
result = GameManager->getScriptManager()->getScriptContext()->Prepare(GameManager->getScriptManager()->getUpdateFunction());
//this appears to be the source of my woes....
result = GameManager->getScriptManager()->getScriptContext()->SetArgObject(0, this->asObj);
result = GameManager->getScriptManager()->getScriptContext()->Execute();
}[/source]

AngelScript file "Player.as"
[source lang="plain"]class Player
{
Player(GameEntity @Entity)
{
health = 100;
@self = Entity;
}

void Update()
{
self.getSceneNode().setPosition(self.getSceneNode().getPosition() + vector3(.25, 0, 0));
}

GameEntity@ self;
int health;
}[/source]

-THANKS IN ADVANCE!

Share this post


Link to post
Share on other sites
Advertisement
The Player::Update() method doesn't take any arguments, so any call to SetArg...() would be wrong.

You need to call ctx->SetObject(this->asObj) to set the object for which the method should be invoked.

Regards,
Andreas

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement