Complete Example Scripts

From the CreationKit Wiki
Revision as of 07:33, 14 February 2012 by imported>Evernewjoy (→‎Script Instantiation)
Jump to navigation Jump to search

Complete Example Scripts

Script Instantiation

Please remember that few example scripts will be complete by themselves, you must attach a script to an object in the Creation Kit before it will run. The exception is for "library" scripts, such as the Utility Script, which contain definitions of global functions for use in other scripts.

Property Setting

And if the script has any properties, those properties must be set on the object to which the script is attached in the Creation Kit before the script will work correctly.

Script Name

If you use any of these example scripts, you can change the name to anything else, but if you're creating the file outside of the Creation Kit you must make sure it also matches the name of your file.


Scripts

A Trigger That Detects When The Player Enters

Scriptname ExampleTrigger extends ObjectReference

Event OnTriggerEnter(ObjectReference akActionRef)
	if(akActionRef == Game.GetPlayer()) ; This condition ensures that only the player will trigger this code
		Debug.MessageBox("Yippee!")
	endif
EndEvent

To Make This Script Work

Select an existing object in your cell

Click on the Trigger Volume button, the box with a T inside of it.

  • A orange box should appear around the object you selected.

One reason the Creation Kit is more awesome than any prior editor I've used is this feature!

If you know you need a large trigger volume, select a large object before pressing the T-box button!


  • Now edit the new orange box you created and add the script above.
  • Then go in-game and walk into the space where this invisible box is.

Uses:

  • Create events that depend on player positioning
  • You could eliminate the player if statement above and have other actors like draugr or companions activate things as well
  • Non-player triggers can be used by changing the == to a != in the script above, to do things to other actors like restoring their health or moving them around (but not affecting player if player walks through the trigger)


Move an Object to a Specific Location Without Fade

You can use this script to make an activator or actor move without fading out in any way (moveTo and setPosition cause fading to occur).

Use this to make objects fly around or make the player move continuously to a destination without fade.


This example moves the player, for your entertainment.


  • To move something other than player, add a property of type ObjectReference and set it to the actor or activator that you want to move (I have not succeeded in moving statics, only activators and actors)
  • Use different Events freely, this event is used for simplicity and easy demonstration.


Scriptname MoveObjExample extends ObjectReference  

ObjectReference property dest auto

Event onTriggerEnter(ObjectReference ObjRef) 

  if(ObjRef == game.getplayer())
    objRef.translateTo(
         dest.x,dest.y,dest.z,
         dest.getangleX(),dest.getangleY(),dest.getangleZ(),
         500)
  endif

;500 is the speed value, and it's pretty fast

endEvent

To Make This Script Work

  • Make an XMarkerHeading object (search *All with "xmarker" filter), place it where you want the player to be moved to.
  • Click on an object in your cell and select the Trigger Volume button, the T with a box around it
  • Move the orange box to where you want the player to start their controlled movement to the XMarkerHeading destination.
  • Add this script above to the orange box/trigger volume that you created.
  • SET THE PROPERTY of the script instance, dest, to be your XMarkerHeading that you just made.


This example will not work if you do not set the property as I mention just above, this is an easy thing to forget in this new and wonderful language.

Get used to these basics and you will find Papyrus much more powerful than prior scripting languages.

A Fully Functional Dwemer Button

Scriptname DwemerButtonExampleScript extends ObjectReference  

ObjectReference property objSelf auto hidden
Sound property QSTAstrolabeButtonPressX auto

;==================

;this event runs when cell is loaded containing
;this scripted object

Event OnCellAttach()
	PlayAnimation("Open")
EndEvent

;==================

Event OnActivate(ObjectReference akActionRef)

	if (akActionRef == Game.GetPlayer())

		;--- sound and animation

		PlayAnimationAndWait("Trigger01", "done")
	if (QSTAstrolabeButtonPressX)
		QSTAstrolabeButtonPressX.Play(Self)
	endif

	;actual code for what button should do
	Debug.MessageBox("Joy!")
	
	endif

EndEvent

To Make This Script Work

  • You must create this script on a Activator object, probably a dwemer button :)
  • You must then set the property for the QST sound effect property, the sound file has the same name as the property in the script above.


To make your own dwemer button and avoid messing with the original Skyrim scripts/activators:

  • Extract the dwemer button .nif file from the Meshes.bsa and create a new activator using that .nif file (picking the mesh to use is an option in the activator edit window).
  • You must put your extracted .nif file in the skyrim/Data/meshes directory structure if you want the Creation Kit to be able to find and use it.


To extract files from the Skyirm .BSA files you must download something like the Fallout3 Archive Utility, do a google search for this.

Set Quest Stage When Picked Up By Player

ScriptName SetStageOnPlayerPickUp extends ObjectReference

Quest property QuestToSet auto
int property StageToSet auto

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)

	if (akNewContainer == Game.GetPlayer())
		QuestToSet.SetStage(StageToSet)
		GoToState("Taken")
	endif

EndEvent

State Taken
	; Do nothing
EndState

To Make This Script Work

This script should be attached to an item that the player has to pick up as part of a quest. When the player picks up the item for the first time, the quest's stage it set to a value specified by a parameter.

  • Create an item for the player to pick up, and attach this script to that item
  • In the dialogue window for that item, right click on this script and select "Edit Properties". You need to specify:
    • The associated quest (QuestToSet)
    • The stage at which the quest should be set (StageToSet)
  • Then just place your item in the world. When the player picks it up, the quest you specified will be set to the stage you specified.