I'm starting to understand a lot more now! Following steps should be:
- Prepare script functions and compile when level is loading (initializing).
-Execute functions whenever a key is pressed or etc...
here's my test script:
void main()
{
bool result = KeyPressed("FireButton");
if(result == true) {
DebugPrint("You hit me!!");
}
}
in C++ upon initialization I have the script engine initialized. I have the function declarations initialized and compiled or built. For executing the script commands I have it ExecuteCall() function.
I'm just working on one function declaration for now until I fully understand how this beautiful scripting works. I did noticed when I recompile the script and execute calls it would hurt performance and cause fps drop from 60 - 31.
bool ScriptManager::compileScript(const char* file, asIScriptEngine *eng, asIScriptContext *context) {
CScriptBuilder builder;
int r = builder.StartNewModule(eng, file);
if (r < 0) {
MessageBoxA(0, "Unable to recover from error. Memory is possible full!", "Script Builder Failed.", 0);
return false;
}
r = builder.AddSectionFromFile(file);
if (r < 0) {
MessageBoxA(0, "Unable to load script file. Be sure the script file is in the proper location.", "Script Builder Failed!", 0);
return false;
}
r = builder.BuildModule();
if (r < 0) {
MessageBoxA(0, "Unable to build script module.", "Script Builder Failed.", 0);
return false;
}
scriptModule = file;
module = eng->GetModule(scriptModule);
function = module->GetFunctionByDecl("void main()");
if (function == 0) {
return false;
}
context = eng->CreateContext();
context->Prepare(function);
return true;
}
int ScriptManager::ExecuteCall(asIScriptContext *context) {
int r = context->Execute();
if (r != asEXECUTION_FINISHED)
{
if (r == asEXECUTION_EXCEPTION)
{
const char * exceptionString = asCtx->GetExceptionString();
const char * exceptionFunction = asCtx->GetExceptionFunction()->GetDeclaration();
int exceptionLineNumber = asCtx->GetExceptionLineNumber();
wchar_t msg[1024] = {};
swprintf_s(msg, L"Exception: %s, Function: %s, Line: %d", exceptionString, exceptionFunction, exceptionLineNumber);
MessageBox(0, msg, L"Script Exception!", 0);
// It is possible to print more information about the location of the
// exception, for example the call stack, values of variables, etc if
// that is of interest.
}
}
return r;
}
So, perhaps the execution isn't the best call during the rendering loop because if there's any mistakes inside the script I'll get infinity message box popping up. Event Handlers......, In C++ whenever there's a key pressed would I execute the functionDelcaration("void main()"); ??