Difference between revisions of "Simulating new functions"
Jump to navigation
Jump to search
Moved the discussion to the "Discussion" page and put the actual solution into the "article".
imported>JustTim |
imported>JustTim (Moved the discussion to the "Discussion" page and put the actual solution into the "article".) |
||
Line 1: | Line 1: | ||
Writing complex scripts for oblivion can be very painful and frustrating. Not being able to write reusable functions that execute directly on call instead of the next frame is one of the major problems (at least for me). | |||
But Kkuhlmann from Bethesda had a great idea to make a workaround: Quest Stages! | |||
When you call setStage in a script, the related stage result script will be executed immediately BEFORE the current script continues! This is therefore a great way to write immediately executing function calls! | |||
I've spent MANY hours to advance this idea to a fully reusable function framework that can easily be used in your scripts. Here is what i came up with: | |||
Create a Quest just called "f" (for "function"), ACTIVATE "ALLOW REPEATED STAGES" and attach the following quest script: | |||
<pre>ScriptName FunctionQuestScript | <pre>ScriptName FunctionQuestScript | ||
Line 227: | Line 47: | ||
; Set constants first | ; Set constants first | ||
Begin Gamemode | Begin Gamemode | ||
if doOnce == 0 | |||
set rad to 57.2957792 | |||
set pi to 3.1415927 | |||
set doOnce to 1 | |||
endif | |||
End</pre> | End</pre> | ||
This is a generalized function framework that can be re-used for as many functions as you like. | This is a generalized function framework that can be re-used for as many functions as you like. | ||
Line 288: | Line 107: | ||
set f.fout to f.ang</pre> | set f.fout to f.ang</pre> | ||
Okay, the function setup is complete. Now to get the | Okay, the function setup is complete. Now you've already got 3 functions: One to calculate the squareroot, one to calculate the Hypotenuse in a triangle and one to calculate the angle of a vector. To get the angle between two objects in a script just type: | ||
<pre>;CALL float getAngle(float x, float y) | <pre>;CALL float getAngle(float x, float y) | ||
set f.fin1 to ( Object2.getPos x - Object1.getPos x ) | set f.fin1 to ( Object2.getPos x - Object1.getPos x ) | ||
set f.fin2 to ( Object2.getPos y - Object1.getPos y ) | set f.fin2 to ( Object2.getPos y - Object1.getPos y ) | ||
setStage f 20 | setStage f 20 | ||
set | set ResultingAngleZ to f.fout</pre> | ||
Et voilà, here is your Z-Angle between Object 1 and 2! You can reuse the function wherever you like, how often you wish to and you can even call functions from inside another stage of "f" (aka. another function)! | |||
( | |||
I is also very easy to share the functions with others since you only need to share the code of the stage result script for your function. | |||
The only downsides are that you have to define all vars in the quest script (it compiles when you define them in the stage result script, but they'll always be 0) and the limited maximum length of each quest result script (which isn't a big problem since you are able to split a single function into multiple stages). | |||
Many thanks go tu Kkuhlmann for his great idea and to Galerion for the Square Root function! | |||
The above function isn't very accurate, which may be due to my bad mathematics skills. If anyone has an idea on how to improve these scripts for accuracy and performance then please contribute! | |||
[[Category:Useful Code]] | [[Category:Useful Code]] | ||
[[Category:Solutions]] | [[Category:Solutions]] |