Activation Functions

Revision as of 20:15, 11 September 2006 by imported>Elder

IN-GAME FUNCTIONS by Guidobot


This is an alternative to using stage functions. It relies on the fact that the Activator method can be called on a persistant object at any time and operates immediately.

In-game functions are easier to create. They also do not have the restrictions that Stage functions have (number of lines, refernce object calls, variable definiton, etc.) and allow for looping code through clever use of the additional GameMode block (using flags, counters, etc.). Additionally, they are an alternative way to add global variables to the game.


How to set up an in-game function:


1) Copy any object that takes a script (I use Gold001). Rename to, e.g., TargFunction.

2) Place a copy somewhere suitable in the world - preferably where you already have something or in your own cell.

3) Mark the object persistant. Mark it Initially Disabled. Give it a suiable name, e.g. TargFunc01.

4) Place a script on the object to do what you want. (See below for a neat example.)

5) Call this function by simply doing TargFunc01.Activate player 1.


Here is an example of a method that you can call to get the position of the target cross hairs:


scn TargFunctionScript

; initial position vector
float xPos
float yPos
float zPos

; fly vector
float dx
float dy
float dz

; private
float ang
float x
float x2
float sin
float cos

Begin OnActivate

; horizontal fly vector
set ang to player.GetAngle z
if ang > 180
  set ang to (ang)-360
endif
set x to ang*0.0174532925
set x2 to x*x
set sin to x*(1-(x2/6)*(1-(x2/20)*(1-(x2/42)*(1-(x2/72)*(1-x2/110)))))
set cos to 1-0.5*x2*(1-(x2/12)*(1-(x2/30)*(1-(x2/56)*(1-x2/90))))
set dx to 10*sin
set dy to 10*cos

; vertical fly vector
set ang to player.GetAngle x
set x to -ang*0.0174532925
set x2 to x*x
set sin to x*(1-(x2/6)*(1-(x2/20)*(1-(x2/42)*(1-(x2/72)*(1-x2/110)))))
set cos to 1-0.5*x2*(1-(x2/12)*(1-(x2/30)*(1-(x2/56)*(1-x2/90))))
set dz to 10*sin

; initial position vector (to add to player coords)
set xPos to 5*dx*cos
set yPos to 5*dy*cos
set zPos to 118-25*(player.IsSneaking)+5*dz

End

To use this function:

; some script
;...
TargFunc01.Activate player 1
set xPos to TargFunc01.xPos+player.GetPos x
set yPos to TargFunc01.yPos+player.GetPos y
set zPos to TargFunc01.zPos+player.GetPos z

set dx to TargFunc01.dx
set dy to TargFunc01.dy
set dz to TargFunc01.dz


IMPORTANT NOTES

It turns out there is a bug that can occur if an in-game function is actively processing when the player enters a new cell. The effect is that the item bearing the script suddenly appears in your inventory as if 'IGFRef.Activate player' was called rather than 'IGFRef.Activate player 1'.

One way to fix this is to pass another actor ref instead of player. However, it depends on what you wish to achieve.

If you really want a static function then your best option is to use a remote persistent object - i.e. a true world activator object (e.g. a switch). This has no such issues and has the advantage of accepting slightly neater call syntax:

IGFActRef.Activate player

(Additionally you do not have to mark the placed IGFRef as initially disabled unless you need it to activate as a sensor for when the player is nearby.)

The disadvantage of this is that the GameMode part of the script will never be that useful since the activator item is remote. If you use items, as described above, you can always teleport them to the player location so they trigger every frame. However, in this case you may be better off using an unplayable item (aka token) on the player and using the OnActivate method or switch it on using quest variables.


Another thing to watch for is how many times you call the same IGF activator within a single frame. Like Stage functions, they can 'time-out' if too many calls are made. Additionally, as with (recursive) PlaceAtMe calls, there appears to be a limit of only 4 or 5 activation calls you are allowed to make in one frame - probably due to a general hard-coded recursion call trap that the interpretor makes.


One extra note on Stage functions. Apart from being useful in their own right, they have a very nice goto # / call # functionality.

set x to 10; any value between 0-255
SetStage MyQuest x

This property can be used to create small linked-lists, stacks, arrays, etc.