Suggestion: Call a conditional script from within another script and evaluate its return value
prwo
Member Posts: 69
Currently, the ExecuteScript command will only invoke the main procedure of a script. To my knowledge it does not work on conditional scripts (with the StartingConditional function).
There should be a way to invoke a conditional script from within another script and evaluate its return value.
E.g. by having a ExecuteConditionalScript that will do exactly that:
Conditional scripts are often used in dialogs to branch out options based on conditions. The same branching may apply in "ordinary" scripts as well. Being able to execute a conditional script from within another script and evaluating its return value would help to reuse these scripts and to avoid code-duplication.
There is a (less elegant) workaround for this today:
There should be a way to invoke a conditional script from within another script and evaluate its return value.
E.g. by having a ExecuteConditionalScript that will do exactly that:
// Make oTarget run sScript's StartingConditional function and then return it's value to the calling script.
int ExecuteConditionalScript (string sScript, object oTarget)
Conditional scripts are often used in dialogs to branch out options based on conditions. The same branching may apply in "ordinary" scripts as well. Being able to execute a conditional script from within another script and evaluating its return value would help to reuse these scripts and to avoid code-duplication.
There is a (less elegant) workaround for this today:
- Use a "normal" (main) scripts to evaluate the condition.
- Set the determined return value as an int on a quasi-static (always existing) object, e.g. GetFirstPlayer, Module variable, etc.
- Call the quasi conditional script using ExecuteScript and evaluate its "return value" by reading the previously set int.
Post edited by prwo on
0
Comments
Dave
There was also GetScriptReturnValue() in nwnx_events, but that hasn't been implemented in NWNXEE yet.
The workaround using include files would be to extract your StartingConditional function into a separate inculde file, rename the function to something meaningful and then call this function in your StartingConditional file as well as everywhere else where you need the conditional to be evaluated.
The drawback with this approach is that you have to recompile all dependant files if you change the code of your included function. The safest way is to recompile everything. See also: here
//---------------------------------------------------------------------------- // Wrapper for Execute Script to execute a script and get an integer // return value. Do not nest this function! //---------------------------------------------------------------------------- int ExecuteScriptAndReturnInt(string sScript, object oTarget) { DeleteLocalInt(oTarget,"X2_L_LAST_RETVAR"); ExecuteScript(sScript,oTarget); int nRet = GetLocalInt(oTarget,"X2_L_LAST_RETVAR"); DeleteLocalInt(oTarget,"X2_L_LAST_RETVAR"); return nRet; } //---------------------------------------------------------------------------- // Helper function for ExecuteScriptAndReturnInt //---------------------------------------------------------------------------- void SetExecutedScriptReturnValue(int nValue = X2_EXECUTE_SCRIPT_CONTINUE) { SetLocalInt(OBJECT_SELF,"X2_L_LAST_RETVAR",nValue); }
Interesting fact: Although the documentation says otherwise, this approach is perfectly nestable (because NWN scripts are not executed in parallel). That's only an option if you want to use NWNX, which basically fiddles around in the game executable's process memory at runtime to compensate for things the original can't do.
Edit: and don't set the return value before calling nested scripts. Some of the scripts that use this in the default code do that if I recall.
I think the ExecuteScriptAndReturnInt is used for more than just returning a value. It is supposed to determine the caller's program flow. At least the default in SetExecutedScriptReturnValue would suggest that. Nevertheless, it is a good example of how to pass a return value.