Difference between revisions of "Simulating new functions"
imported>Kkuhlmann |
imported>Tegid (More function call options) |
||
Line 83: | Line 83: | ||
--[[User:Kkuhlmann|Kkuhlmann]] 14:02, 18 April 2006 (EDT) | --[[User:Kkuhlmann|Kkuhlmann]] 14:02, 18 April 2006 (EDT) | ||
I've been doing this for awhile now with OnActivate functions placed on activators. The reference you use to activate it acts as a parameter to tell it how to act. Then when that function is done it activates the calling object to let it know that it is finished and it is time to process the results. | |||
So for instance | |||
<pre>scn ActivateFunction | |||
ref incoming | |||
begin OnActivate | |||
set incoming to GetActionRef | |||
if (incoming == CallingScriptRef.Me) | |||
set CallingScriptRef.Value to CallingScriptRef.Value + 3 | |||
CallingScriptRef.Activate CallingScriptRef.ActivateFunctionRef 1 | |||
return | |||
endif | |||
end</pre> | |||
<pre>scn CallingScript | |||
ref Me | |||
ref ActivateFunctionRef | |||
long Value | |||
ref incoming | |||
begin OnLoad | |||
set Me to GetSelf | |||
set Value to 0 | |||
Message "I am storing a value of %.0f", Value, 1 | |||
end | |||
begin OnActivate | |||
set incoming to GetActionRef | |||
if (incoming == ActivateFunctionRef) | |||
Message "Now I am storing a value of %.0f", Value, 1 | |||
return | |||
endif | |||
ActivateFunctionRef.Activate Me 1 | |||
end | |||
</pre> | |||
This removes the need for a game mode block and allows function passing (or setting in a persistent reference). I've used this quite successfully for some cool stuff I'm finishing the testing on. | |||
--[[User:Tegid|Tegid]] 14:44, 18 April 2006 (EDT) | |||
[[Category:Questions]] | [[Category:Questions]] |
Revision as of 13:44, 18 April 2006
I've tried creating a custom "function" that can be called by other scripts. The basic approach I took was to make the function a quest script, and have the calling script call the function via |StartQuest function_quest|. Ex:
I have a quest called testfunc that is initially disabled and contains the following script:
scn testfuncscript float param1 float param2 float result begin GameMode Message "start func", 1 set result to param1 + param2 Message "end func: %.0f", result, 1 StopQuest testfunc end
In another script, I call this "function":
scn testscript float r begin OnActivate Message "before call", 1 set testfunc.param1 to 10 set testfunc.param2 to 20 StartQuest testfunc set r to testfunc.result Message "after call: %.0f", r, 1 end
However, the above code doesn't work. The order of the output illustrates why:
before call after call: 0 start func end func: 30
This is what it should be:
before call start func end func: 30 after call: 30
The problem is that the calling script doesn't wait for the function script to execute, and instead the function script is delayed until after the calling script finishes executing.
So is there any way that can bypass this problem without resorting to nasty "wait for the other script to finish" hacks? (I could make the calling script have a GameMode block that would repeatly check when the function finishes executing, but that would be a serious PITA.) I suspect this same problem would've arisen if I had tried creating a function script by attaching it to a persistent object instead of a quest (and calling it via the Activate function).
--Maian 03:59, 18 April 2006 (EDT)
I'm just kind of brainstorming here, but you might try making a quest, marked "Allow repeated stages" so you can call the stages repeatedly, and make (say) stage 10 be your "function". Quest stage results are executed immediately, while the current script is processing, so you wouldn't have to wait for the results.
Keep your variables in your quest script. In stage 10 results, you'd have:
Message "start func", 1 set testfunc.result to testfunc.param1 + testfunc.param2 Message "end func: %.0f", testfunc.result, 1
To call this function, you'd do the following:
scn testscript float r begin OnActivate Message "before call", 1 set testfunc.param1 to 10 set testfunc.param2 to 20 setstage testfunc 10 ; call the "function" set r to testfunc.result Message "after call: %.0f", r, 1 end
I think this should work (I haven't tried it).
--Kkuhlmann 14:02, 18 April 2006 (EDT)
I've been doing this for awhile now with OnActivate functions placed on activators. The reference you use to activate it acts as a parameter to tell it how to act. Then when that function is done it activates the calling object to let it know that it is finished and it is time to process the results.
So for instance
scn ActivateFunction ref incoming begin OnActivate set incoming to GetActionRef if (incoming == CallingScriptRef.Me) set CallingScriptRef.Value to CallingScriptRef.Value + 3 CallingScriptRef.Activate CallingScriptRef.ActivateFunctionRef 1 return endif end
scn CallingScript ref Me ref ActivateFunctionRef long Value ref incoming begin OnLoad set Me to GetSelf set Value to 0 Message "I am storing a value of %.0f", Value, 1 end begin OnActivate set incoming to GetActionRef if (incoming == ActivateFunctionRef) Message "Now I am storing a value of %.0f", Value, 1 return endif ActivateFunctionRef.Activate Me 1 end
This removes the need for a game mode block and allows function passing (or setting in a persistent reference). I've used this quite successfully for some cool stuff I'm finishing the testing on. --Tegid 14:44, 18 April 2006 (EDT)