Complete Example Scripts: Difference between revisions

Jump to navigation Jump to search
→‎Good Usage of Properties to Make Scripts Compact: ' Good Usage of Properties to Make Scripts Compact' compacted.
imported>JustinOther
m (→‎Show Gift Window and Identify Items Given: 'Self' unnecessary. RemoveItem called implicitly)
imported>JustinOther
(→‎Good Usage of Properties to Make Scripts Compact: ' Good Usage of Properties to Make Scripts Compact' compacted.)
Line 317: Line 317:




=Good Usage of Properties to Make Scripts Compact=
=Usage of Arrays to Make Scripts Compact=
 
In this example you can see how to use a simple script property to reduce the number of scripts you need to write and thus the amount of properties you need to set, which can be time-consuming. Below I was having to do things to things to about 30 different draugrs:
In this example you can see how to use a simple script property to reduce the number of scripts you need to write and thus the amount of properties you need to set, which can be time-consuming.
 
 
In the example below I was having to do two different things to about 30 different draugrs,
 
* turn their AI on and make them visible
* turn their AI on and make them visible
* turn their AI off and make them invisible
* turn their AI off and make them invisible
I have different conditions within my level for when each of these two actions should occur. I could divide this into two scripts, but instead I wrote one script and passed a property to the script with each instance of the script to determine which aspect of the script would run.
<source lang="papyrus">Actor[] Property DraugrArray Auto ; Contains 30 elements filled in CK with the 30 Draugr
Bool Property bEnableBeasts Auto ; Can be set from another script and/or initialized true or false


I have different conditions within my level for when each of these two actions should occur.
Event OnTriggerEnter(ObjectReference akActionRef)
 
If akActionRef == Game.GetPlayer()
 
Int iIndex = DraugrArray.Length ; Will return 30
I could divide this into two scripts, but instead I wrote one script and passed a property to the script with each instance of the script to determine which aspect of the script would run.
While iIndex > 0
 
iIndex -= 1 ; 29'th element is 30'th Draugr
 
Actor rDraugr = DraugrArray[iIndex]
In this example I only show 3 of the Draugrs I was modifying. ''Imagine it was 30 Draugrs and you can see why only setting the properties once and writing only one script would be a time-saver :)''
rDraugr.EnableAI(bEnableBeasts)
 
rDraugr.SetAlpha(bEnableBeasts As Float)
<source lang="papyrus">
EndWhile
Scriptname RamaJRDisableAndMakeInvis extends ObjectReference
EndIf
 
EndEvent</source>
ObjectReference property RamaJRFadeRockArcher001 auto
Note that bEnableBeasts is a property so one can decide with each instance which way the script should work. One could use a FormList rather than an array or simply set one Draurg as the enable parent of the other 29 and enable/disable him, thus all the children refs simultaneously.
ObjectReference property RamaJRFadeRockArcher002 auto
ObjectReference property RamaJRFadeRockArcher003 auto
 
float alpha
 
 
;--- The Most Important Line ---
bool property enableBeasts auto
;-------------------------------
 
ObjectReference curBeast
 
Event onTriggerEnter(ObjectReference ObjRef)
 
if(ObjRef == game.getplayer())
 
  if (enableBeasts) ;if enableBeasts is true
                        ;make them visible
                        alpha = 1
                  else
                        ;make them invisible
                        alpha = 0
                  endif
 
  curBeast = RamaJRFadeRockArcher001
  (curBeast as Actor).setAlpha(alpha)
  (curBeast as Actor).enableAI(enableBeasts)
 
  curBeast = RamaJRFadeRockArcher002
  (curBeast as Actor).setAlpha(alpha)
  (curBeast as Actor).enableAI(enableBeasts)
 
  curBeast = RamaJRFadeRockArcher003
  (curBeast as Actor).setAlpha(alpha)
  (curBeast as Actor).enableAI(enableBeast)
endif
endEvent
 
 
 
;------------------------------------------------------------------
; By Kahmul: Added extra script for more readable and shorter code.
 
Scriptname EnableDisableFast extends ObjectReference
 
ObjectReference[] Property objAr auto  ; all needed ObjectReferences have to be added here. The number of added objects will define the array's size at run time.
ObjectReference curObj
 
int index
 
float Property Alpha auto  ; wanted alpha.
bool Property AI auto  ; wanted AI.
 
Event OnTriggerEnter(ObjectReference player)
 
index = 0
if player == game.getplayer()
while index < objAr.length ; note that you use ".length" not ".getlength" to get the size of arrays
curObj = objAr[index]
curObj.setAlpha(alpha)
curObj.enableAI(AI)
index += 1
endwhile
endif
EndEvent
 
;//End new version.
;------------------------------------------------------------------
 
 
</source>
 
So notice in the script above, I set the bool as a property value so I can decide with each instance where I run the script which way it should work.
 
 
* Incidentally this script is also a example of casting a variable to a certain type when needed, I cast the ObjectReference to a Actor so I can access the Actor-type functions
 
<source lang="papyrus">
ObjectReference curBeast
(curBeast as Actor).enableAI(true)
</source>
 
 
* I use curBeast for easy copy-pasting. You could use FormLists to do this process as well
 


'''How to Make This Script Work'''
'''How to Make This Script Work'''
 
* You will need to fill the array elements in the Creation Kit with the chosen creatures.
* You will need to replace all names for the Draugr Archers (RamaJRFadeRockArcher001 for example) with the exact ID values of each of your chosen creatures.
* Then you will need to chose for each instance of the script whether the boolean property should be false or true, this setting process shows up a simple checkmark box in the editor.
 
* Then you will need to chose for each instance of the script whether the boolean property should be false or true, this setting process shows up a simple checkmark box in the editor


= A helper script with functions to get the current moonphase, sync between the two moons and day of the week =
= A helper script with functions to get the current moonphase, sync between the two moons and day of the week =
Anonymous user

Navigation menu