Difference between revisions of "Simulating new functions"

1,552 bytes added ,  09:06, 7 May 2006
no edit summary
imported>JustTim
imported>JustTim
Line 189: Line 189:
Create a Quest just called "f" (for "function") with the following script:
Create a Quest just called "f" (for "function") with the following script:
<pre>ScriptName FunctionQuestScript
<pre>ScriptName FunctionQuestScript
; Internals
short doOnce
; Constants
float rad
float pi


; Function In- and Output
; Function In- and Output
Line 203: Line 210:
ref rout2
ref rout2


;Specific Function Vars
;S5 FUNCTION sqrt
;Stage10 FUNCTION Hypotenuse
float sqr
float sqr
float n</pre>
 
;S10 FUNCTION Hypotenuse
;float sqr
float n
 
;S20 FUNCTION getAngle
;float sqr
float x
float y
float sin
float cos
float ang
 
; Set constants first
Begin Gamemode
if doOnce == 0
set rad to 57.2957792
set pi to 3.1415927
set doOnce to 1
endif
;StopQuest f
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.


And now make stage 10 for this quest with the following code:
And now make stage 5 for this quest with the following code:
<pre>; FUNCTION float Hypotenuse(float CathetusA, float CathetusB)
<pre>;FUNCTION float sqrt(float input)
;float sqr, n
if (f.fin1 <= 0)
 
   set f.fout to 0
set f.n to ((f.fin1 * f.fin1) + (f.fin2 * f.fin2))
if (f.n <= 0)
   set f.fout to 1
else
else
   set f.sqr to f.n/2
   set f.sqr to f.fin1/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
   set f.sqr to (f.sqr+(f.n/f.sqr))/2
   set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
   set f.fout to f.sqr
   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:
And stage 10:
<pre>; float Hypotenuse(float CathetusA, float CathetusB)
<pre>;FUNCTION float Hypotenuse(float CathetusA, float CathetusB)
set f.fin1 to SomeValue
set f.n to ((f.fin1 * f.fin1) + (f.fin2 * f.fin2))
set f.fin2 to AnotherValue
;CALL float sqrt(float input)
set f.fin1 to f.n
setStage f 5
;fout is already the result</pre>
 
And stage 20:
<pre>;FUNCTION float getAngle(float x, float y)
set f.x to f.fin1
set f.y to f.fin2
;CALL Hypotenuse using same fin
setStage f 10
setStage f 10
set YetAnotherValue to f.fout</pre>
set f.sqr to f.fout
 
set f.sin to (f.x/f.sqr)
set f.cos to (f.y/f.sqr)
 
if f.cos >= 0 && f.sin < 0 ;Q4
  set f.ang to (f.cos*f.rad)
  set f.ang to f.ang + 270
elseif f.cos < 0 && f.sin < 0; Q3
  set f.ang to ((0-f.sin)*f.rad)
  set f.ang to f.ang + 180
elseif f.cos < 0 && f.sin >= 0 ;Q2
  set f.ang to ((0-f.cos)*f.rad)
  set f.ang to f.ang + 90
else ;if f.cos >= 0 && f.sin >= 0 ;Q1
  set f.ang to (f.sin*f.rad)
endif
set f.fout to f.ang</pre>
 
Okay, the function setup is complete. Now to get the Angle between two objects in a script just type:
<pre>;CALL float getAngle(float x, float y)
set f.fin1 to ( Object2.getPos x - Object1.getPos x )
set f.fin2 to ( Object2.getPos y - Object1.getPos y )
setStage f 20
set ResultingAngle 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 243: Line 305:


Very cool: You are able to call a function (stage) from inside another function, even if both are part of the same quest! You'll just have to take care that they use diffrent veriables  or the same but not at the same time. This is very handy due to the very limited size of one stage. It is also helpful to call the quest just "f" instead of "function" since you have to write the quest name for every variable you access in the script. --[[User:JustTim|JustTim]] 22:59, 6 May 2006 (EDT)
Very cool: You are able to call a function (stage) from inside another function, even if both are part of the same quest! You'll just have to take care that they use diffrent veriables  or the same but not at the same time. This is very handy due to the very limited size of one stage. It is also helpful to call the quest just "f" instead of "function" since you have to write the quest name for every variable you access in the script. --[[User:JustTim|JustTim]] 22:59, 6 May 2006 (EDT)
I've updated the example with a more complex one. It is tested and working!! But it isn't very accurate due to the relatively unaccurate squareroot. If anyone knows how to improve this, please call me. --[[User:JustTim|JustTim]] 10:06, 7 May 2006 (EDT)


[[Category:Useful Code]]
[[Category:Useful Code]]
[[Category:Solutions]]
[[Category:Solutions]]
Anonymous user