From CryWiki
| About
|
| Author
| ins
|
| Skill Level
| Beginner
|
| Compatibility
| 1.2+
|
| Requirements
| Compiler
|
| Date Added
| 15/09/2008
|
| Last Modified
| 15/09/2008
|
|
Calling a script function
This document explains how to call a script function from c++. A script function is a function written in lua. We will call a function in Singleplayer.lua for this example. (Located at Scripts/GameRules/SinglePlayer.lua)
There are 2 ways to call a lua function
- Using BeginCall, PushParam and EndCall
- Using static functions of the Script struct
For most cases, the second method will work just fine, and it takes fewer lines to write. If you need to pass a lot of arguments to the lua function, you'll have to use the first method.
Using BeginCall, PushParam and EndCall
- Call BeginCall
- Push all parameters with PushParam
- Call EndCall
|
C++
|
// Get the scriptsystem
IScriptSystem *pScriptSystem = gEnv->pScriptSystem;
// Get the ScriptTable object of the current gamerules.
IScriptTable *pScriptTable = g_pGame->GetGameRules()->GetEntity()->GetScriptTable();
// Get the object to pass on to the function, here the client actor.
IScriptTable *pClientActor = g_pGame->GetIGameFramework()->GetClientActor()->GetEntity()->GetScriptTable();
// This will store the return value
float returnValue;
// Make sure we don't have NULL pointers
if (pScriptSystem && pScriptTable && pClientActor)
{
// Call the GetEnergyAbsorption function on the pScriptTable object (which is the script table of the gamerules)
pScriptSystem->BeginCall(pScriptTable, "GetEnergyAbsorption");
// ALWAYS pass on the ScriptTable as first parameter, even when calling a function without arguments
pScriptSystem->PushFuncParam(pScriptTable);
// Push our actual parameter
pScriptSystem->PushFuncParam(pClientActor);
// End the call, and store the result in returnValue
pScriptSystem->EndCall(returnValue);
CryLogAlways("The function returned %f", returnValue);
} else { // (pScriptTable or pScriptSystem or pClientActor) == NULL
CryLogAlways("Failed execute the function.");
}
|
|
| Note
|
| When calling a function without arguments, you still have to pass on the ScriptTable of the entity as an argument.
|
|
Using static functions of the Script struct
These functions can be found at Code/CryEngine/CryCommon/ScriptHelpers.h, inside the Script struct. All the functions are static so you don't need to create an instance of the Script struct.
|
C++
|
// Get the ScriptTable object of the current gamerules.
IScriptTable *pScriptTable = g_pGame->GetGameRules()->GetEntity()->GetScriptTable();
// This will store the return value
float returnValue;
// Check if the function exists
HSCRIPTFUNCTION getEnergyAbsorption = 0;
if (pScriptTable && pScriptTable->GetValue("GetEnergyAbsorption", getEnergyAbsorption))
{
// Always pass on pScriptTable as the first argument, even when calling a function without arguments
Script::CallReturn(gEnv->pScriptSystem, getEnergyAbsorption, pScriptTable, returnValue);
CryLogAlways("The function returned %f", returnValue);
} else { // function does not exist
CryLogAlways("The function does not exist.");
}
|
|
Aside from CallReturn, there's also:
- Script::Call - For a function that doesn't return a value, or you don't need it.
- Script::CallMethod - Automaticly passes on the ScriptTable, you can also use a string for the function here
|
C++
|
IScriptTable *pScriptTable = g_pGame->GetGameRules()->GetEntity()->GetScriptTable();
Script::CallMethod(pScriptTable, "EndLevel");
|
|