I'm a newbie!! Registering functions
Hi everyone,
just starting to learn Angelscript.
I've looked through the console.cpp example, and tried it myself.
However the RegisterGlobalFunction returns a -7??
Anything i'm doing wrong? (Most probably am hehehe)
#include <angelscript.h>
#include <iostream>
asIScriptEngine *script;
void testo(int testa)
{
std::cout << testa << std::endl;
}
int main()
{
std::string comm = "testo(14)";
script = asCreateScriptEngine(ANGELSCRIPT_VERSION);
script->RegisterGlobalFunction("void _testo(int)", asFUNCTIONPR(testo, (int), void), asCALL_CDECL);
script->ExecuteString(0, "_testo(14)");
std::cout << comm << std::endl;
}
[Edited by - Yomogi-aren on November 10, 2006 8:34:54 AM]
I just tried compiling the console.cpp example from the SDK and it too gets a return code of -7 on the registerGlobalFunction("float sin(float)" etc line
What could be the problem?
[edit]
I just saw on the WIP page of AngelScript, that the planned changes for the future were adding support for native calling conventions on 64bit and PPC?
I'm compiling these program samples on a PowerPC linux machine and an AMD 64 bit machine! Is that the problem? If so, what is the fix for it??
What could be the problem?
[edit]
I just saw on the WIP page of AngelScript, that the planned changes for the future were adding support for native calling conventions on 64bit and PPC?
I'm compiling these program samples on a PowerPC linux machine and an AMD 64 bit machine! Is that the problem? If so, what is the fix for it??
Indeed. For PPC and AMD64 you need to use the asCALL_GENERIC calling convention.
For your function testo() it would look like this:
You can also take a look at the scriptstring.cpp in the add_on directory for more examples of using the generic calling convention.
For your function testo() it would look like this:
void testo(asIScriptGeneric *gen){int testa = gen->GetArgDWord(0);std::cout << testa << std::endl;}script->RegisterGlobalFunction("void _testo(int)", asFUNCTION(testo), asCALL_GENERIC);
You can also take a look at the scriptstring.cpp in the add_on directory for more examples of using the generic calling convention.
What if i'm using a class function?
for example:
void ScriptHandler::display(std::string& message)
{
cout << message << endl;
}
from another class like ScriptEnginei register the scripting engine.
void ScriptEngine::ScriptEngine()
{
//creating engine stuff
engine->RegisterObjectType("ScriptHandler", sizeof(ScriptHandler), asOBJ_CLASS);
engine->RegisterObjectMethod("ScriptHandler","void _display(string&)", asMethod(ScriptHandler, display), asCALL_GENERIC);
}
would that work?
for example:
void ScriptHandler::display(std::string& message)
{
cout << message << endl;
}
from another class like ScriptEnginei register the scripting engine.
void ScriptEngine::ScriptEngine()
{
//creating engine stuff
engine->RegisterObjectType("ScriptHandler", sizeof(ScriptHandler), asOBJ_CLASS);
engine->RegisterObjectMethod("ScriptHandler","void _display(string&)", asMethod(ScriptHandler, display), asCALL_GENERIC);
}
would that work?
The asCALL_GENERIC calling convention requires that the function signature is:
void __cdecl func(asIScriptEngine *)
Thus you cannot register a class method where a asCALL_GENERIC function is expected. However you can register a asCALL_GENERIC function as a class method in the script.
Remember to look as scriptstring.cpp for more examples.
Regards,
Andreas
void __cdecl func(asIScriptEngine *)
Thus you cannot register a class method where a asCALL_GENERIC function is expected. However you can register a asCALL_GENERIC function as a class method in the script.
void ScriptHandler::display(std::string& message){cout << message << endl;}void ScriptHandler_display(asIScriptGeneric *gen){ ScriptHandler *sh = (ScriptHandler*)gen->GetObject(); std::string *str = (std::string*)gen->GetArgAddress(0); sh->display(*str);}engine->RegisterObjectType("ScriptHandler", sizeof(ScriptHandler), asOBJ_CLASS);engine->RegisterObjectMethod("ScriptHandler","void _display(string&)", asFUNCTION(ScriptHandler_display), asCALL_GENERIC);
Remember to look as scriptstring.cpp for more examples.
Regards,
Andreas
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement