Difference between revisions of "Simulating new functions"

Jump to navigation Jump to search
1,210 bytes removed ,  23:05, 6 May 2006
no edit summary
imported>JustTim
imported>JustTim
Line 187: Line 187:
Now imagine this:
Now imagine this:


Create a Quest called "Function" with the following script:
Create a Quest just called "f" (for "function") with the following script:
<pre>ScriptName FunctionQuestScript
<pre>ScriptName FunctionQuestScript


short sParam1
; Function In- and Output
short sParam2
float fin1
short sParam3
float fin2
short sResult
float fin3
short sResult2
float fout
float fout2


float fParam1
ref rin1
float fParam2
ref rin2
float fParam3
ref rin3
float fResult
ref rout
float fResult2
ref rout2


ref rParam1
;Specific Function Vars
ref rParam2
;Stage10 FUNCTION Hypotenuse
ref rParam3
float sqr
ref rResult
float n</pre>
ref rResult2</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.


And now make stage 10 for this quest with the following code:
And now make stage 10 for this quest with the following code:
<pre>; FUNCTION float Hypotenuse(float CathetusA, float CathetusB)
<pre>; FUNCTION float Hypotenuse(float CathetusA, float CathetusB)
float sqroot
;float sqr, n
float n


set n to ((Function.fParam1 * Function.fParam1) + (Function.fParam2 * Function.fParam2))
set f.n to ((f.fin1 * f.fin1) + (f.fin2 * f.fin2))
if (n <= 0)
if (f.n <= 0)
   set Function.fResult to 1
   set f.fout to 1
else
else
   set sqroot to n/2
   set f.sqr to f.n/2
   set sqroot to (sqroot + (n/sqroot))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set sqroot to (sqroot + (n/sqroot))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set sqroot to (sqroot + (n/sqroot))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set sqroot to (sqroot + (n/sqroot))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set sqroot to (sqroot + (n/sqroot))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set Function.fResult to (n * sqroot)
   set f.fout to f.sqr
endif</pre>
endif</pre>


Okay, the function setup is complete. now to use it in a script just type:
Okay, the function setup is complete. now to use it in a script just type:
<pre>; float Hypotenuse(float CathetusA, float CathetusB)
<pre>; float Hypotenuse(float CathetusA, float CathetusB)
set Function.fParam1 to SomeValue
set f.fin1 to SomeValue
set Function.fParam2 to AnotherValue
set f.fin2 to AnotherValue
setStage Function 10
setStage f 10
set YetAnotherValue to Function.fResult</pre>
set YetAnotherValue to f.fout</pre>


the Function executes immediately, it is easy to use and you can put one function for each stage into this quest framework! This is especially handy when you've to work with complex mathematical functions like those [http://www.elderscrolls.com/forums/index.php?showtopic=374963 HERE].
the Function executes immediately, it is easy to use and you can put one function for each stage into this quest framework! This is especially handy when you've to work with complex mathematical functions like those [http://www.elderscrolls.com/forums/index.php?showtopic=374963 HERE].
Line 238: Line 237:
And you can easily share this functions with others since you'll only have to share the code you've put into this single stage!
And you can easily share this functions with others since you'll only have to share the code you've put into this single stage!


(I haven't tested the functions here but they should work. Thanks to Galerion for the Square Root function.)
(This is tested and works. Thanks to Galerion for the Square Root function.)
--[[User:JustTim|JustTim]] 18:13, 6 May 2006 (EDT)
--[[User:JustTim|JustTim]] 18:13, 6 May 2006 (EDT)
Another cool function:
<pre>; FUNCTION float,float SinCos(float Angle)
float AngleA
float X
float X2
float X3
float X4
float X5
float X6
float X7
float X8
float X9
short xcoeff
short ycoeff
set AngleA to Function.fParam1
If AngleA >= 270
  Set xcoeff to 1
  Set ycoeff to -1
  Set AngleA to AngleA - 270
ElseIf AngleA >= 180
  Set xcoeff to -1
  Set ycoeff to -1
  Set AngleA to AngleA - 180
ElseIf AngleA >= 90
  Set xcoeff to -1
  Set ycoeff to 1
  Set AngleA to AngleA - 90
Else
  Set xcoeff to 1
  Set ycoeff to 1
EndIf
set X to AngleA / 57.2957792
set X2 to X*X
set X3 to X2*X
set X4 to X2*X2
set X5 to X4*X
set X6 to X3*X3
set X7 to X5*X2
set X8 to X4*X4
set X9 to X7*X2
set AngleA to X - X3/6 + X5/120 - X7/5040 + X9/362880 - X9*X2/39916800
set Function.fResult to AngleA * ycoeff ;Sin
set AngleA to 1 - X2/2 + X4/24 - X6/720 + X8/40320 - X8*X2/3628800
set Function.fResult2 to AngleA * xcoeff ;Cos</pre>
Haven't tested this too. I hope it works. Many thanks to mrflippy and Grundulum for this function! --[[User:JustTim|JustTim]] 19:35, 6 May 2006 (EDT)


Damn, I've once again found a great limitation in scripting: It is obviously impossible to define new variables in a quest stage. It compiles, but they'll always have a value of 0. To get the above sampes to work you'll have to define all vars in the function quest script instead of the stage. i'm testing it further. --[[User:JustTim|JustTim]] 21:55, 6 May 2006 (EDT)
Damn, I've once again found a great limitation in scripting: It is obviously impossible to define new variables in a quest stage. It compiles, but they'll always have a value of 0. To get the above sampes to work you'll have to define all vars in the function quest script instead of the stage. i'm testing it further. --[[User:JustTim|JustTim]] 21:55, 6 May 2006 (EDT)
Anonymous user

Navigation menu