Difference between revisions of "CallFunction - ScriptObject"
Jump to navigation
Jump to search
imported>Plplecuyer |
EgoBallistic (talk | contribs) (Updated Notes regarding parameters) |
||
Line 41: | Line 41: | ||
== Notes == | == Notes == | ||
*If the function name | *If the function name doesn't exist, the function will error. You may want to check for the existence of the mod you want to talk to before using this. | ||
*The parameter types must match ''exactly''. You cannot insert a float into the param array if the function expects an int, or a Actor if the function expects an ObjectReference. Normally the compiler would do these casts for you in a normal function call, but it doesn't know what the parameters are, so it is your responsibility to match them up. | *The parameter types must match ''exactly''. You cannot insert a float into the param array if the function expects an int, or a Actor if the function expects an ObjectReference. Normally the compiler would do these casts for you in a normal function call, but it doesn't know what the parameters are, so it is your responsibility to match them up. | ||
*Optional parameters (i.e. parameters with default values in the function declaration) must be supplied with a value. | |||
*The compiler cannot check the function name or parameters for you, so make sure to triple check everything! | *The compiler cannot check the function name or parameters for you, so make sure to triple check everything! | ||
Latest revision as of 06:15, 30 August 2023
Member of: ScriptObject Script
Calls a function on this script synchronously and returns whatever that function does.
Syntax[edit | edit source]
Var Function CallFunction(string asFuncName, Var[] aParams) native
Parameters[edit | edit source]
- asFuncName: The name of the function to call
- aParams: The list of parameters to pass
Return Value[edit | edit source]
Whatever the function returns.
Examples[edit | edit source]
; Call "int Function NumberOfInfections()" on script "CommonCold" and make a choice based on the return value
; We use CastAs to make sure we don't depend on the script we want to call the function on
ScriptObject coldScript = FormFromOtherMod.CastAs("CommonCold")
Var result = coldScript.CallFunction("NumberOfInfections", new Var[0])
int count = result as int
if (count > 10000)
Debug.Trace("Someone should probably make some chicken soup...")
endIf
; Call "Function Infect(Form aTarget)" on script "CommonCold"
; We use CastAs to make sure we don't depend on the script we want to call the function on
ScriptObject coldScript = FormFromOtherMod.CastAs("CommonCold")
Var[] params = new Var[1]
params[0] = Game.GetPlayer() as Form ; Must cast as form by hand, even though actor is derived from form
coldScript.CallFunction("Infect", params)
Notes[edit | edit source]
- If the function name doesn't exist, the function will error. You may want to check for the existence of the mod you want to talk to before using this.
- The parameter types must match exactly. You cannot insert a float into the param array if the function expects an int, or a Actor if the function expects an ObjectReference. Normally the compiler would do these casts for you in a normal function call, but it doesn't know what the parameters are, so it is your responsibility to match them up.
- Optional parameters (i.e. parameters with default values in the function declaration) must be supplied with a value.
- The compiler cannot check the function name or parameters for you, so make sure to triple check everything!