Script request - simple onoff switch

Post » Thu Jun 21, 2012 6:34 am

Please could someone write a very quick script for me? It's a very simple switch script:

When button A is pressed, object B is disabled. When button A is pressed again, object B reappears.

In Morrowind, it would have been something like (forgive me, I'm terribly rusty)

Begin MyScriptshort SwitchedOnif ( OnActivate == 1 )  set SwitchedOn to 1endifif (SwitchedOn == 1 )   if (getDisabled->ObjectB == 0 )	  ObjectB->disable	  setSwitchedOn to 0   elseif (getDisabled->ObjectB == 1 )	  ObjectB->enable	  setSwitchedOn to 0   endifendifend

And now you see how bad my coding skills are, which is why I haven't even begun to look at Papyrus. I'll take a look at some point but I'm working on something else right now and time is very limited. I'd be grateful if someone could just post the entire script I need (including begin/end equivalents).

Much appreciated
User avatar
Claire Mclaughlin
 
Posts: 3361
Joined: Mon Jul 31, 2006 6:55 am

Post » Thu Jun 21, 2012 9:24 pm

Hey princess_stomper :)

Here's something that I think should do what you need:
ScriptName ToggleSwitchScript extends ObjectReferenceObjectReference Property LinkedReference autoEvent OnActivate(ObjectReference akActionRef) If (LinkedReference.IsDisabled())  LinkedReference.Enable() Else  LinkedReference.Disable() EndIfEndEvent
This script should be attached to the switch, and you'll need to select the reference you want to be switched on and off in the properties section.

Cipscis
User avatar
Kahli St Dennis
 
Posts: 3517
Joined: Tue Jun 13, 2006 1:57 am

Post » Thu Jun 21, 2012 2:01 pm

Hey princess_stomper :smile:

Here's something that I think should do what you need:
Or one could use your patented bool switch:
Bool bOnEvent OnActivate(ObjectReference akActivator)	bOn = !bOn	If bOn		GetLinkedRef().Enable() ; Ref "Initially Disabled"	Else		GetLinkedRef().Disable()	EndIfEndEvent
User avatar
Emma Louise Adams
 
Posts: 3527
Joined: Wed Jun 28, 2006 4:15 pm

Post » Thu Jun 21, 2012 5:50 pm

Why would you want to use a variable to store a state for something that already has a state you can easily check? In fact, don't write a new script for this. There's already one supplied with the game!

It's defaultActivateToggleLinkedRef and uses the GetLinkedRef feature instead of making you set the property directly.

So in the CK, just double click on the switch object you've placed in the world then:
On the "Linked Ref" tab add the object you want to get disabled and enabled (you don't need a keyword).
On the "Scripts" tab (it's always last) and add the "defaultactivatetogglelinkedref" script.

That's it. Apparently they had a use for the light switch script too even if they gave it a really bad name.
User avatar
Bethany Watkin
 
Posts: 3445
Joined: Sun Jul 23, 2006 4:13 pm

Post » Thu Jun 21, 2012 3:41 pm

Why would you want to use a variable to store a state for something that already has a state you can easily check? In fact, don't write a new script for this. There's already one supplied with the game!
Sure, there are plenty of ways to skin this cat, using a vanilla script being the least ...elastic given it's generally *a bad idea to edit 'em. Albeit resource consumption isn't really a factor for this script, checking a variable/property is 'cheaper' than using any function.

*if they want to add a dimmer switch, multicolored mood lighting, or whatever
User avatar
Lew.p
 
Posts: 3430
Joined: Thu Jun 07, 2007 5:31 pm

Post » Thu Jun 21, 2012 1:26 pm

I never mentioned editing the script because that would be a really bad idea. The request wasn't for a fancy dimmer and multicolored mood lighting switch, just a simple on-off switch. Now that you mention it, that's rather out of character for princess_stomper though. :)

But Papyrus was designed for scripts to be reused and the resource consumption of having another unique script running for a standard action far outweighs any benefit you might get from checking a variable vs. calling the function. (And for an action that responds to an infrequent event like OnActivate that's even more true.) It seems that once a Papyrus script is ever attached to some item in your game at least some information about it will be in your savegame file forever, so it's a bad idea to use new scripts when an old one will get the job done.

I'm really starting to like Papyrus, but it's going to take the modding community a long time to unlearn bad habits we had to develop when working with the older scripting language. We really should start documenting more clearly these little gems of default scripts that have such horrible names.

defaultActivateToggleLinkedRef does have one optional property that can be set. Fade should make the other item fade in and out instead of just popping on and off.
User avatar
Chrissie Pillinger
 
Posts: 3464
Joined: Fri Jun 16, 2006 3:26 am

Post » Thu Jun 21, 2012 7:28 pm

Awesome! Thanks everyone - and yes, that should be ideal for my light switches! :)
User avatar
Jessica Phoenix
 
Posts: 3420
Joined: Sat Jun 24, 2006 8:49 am

Post » Thu Jun 21, 2012 9:28 pm

the names are awful so it's no surprise people re-invent the wheel at each point. Using a new language is difficult enough for people brand new to coding anything.

There's a gap between knowing what you want to happen and knowing if there's a built in function that could be used. Getting ones head around event tasks and the ability to code for different events is a big chore and while the construction set wiki does help a bit it leave a lot to be desired on the papyrus syntax structure it has elected to use.

question - is akbaseitem a default base reference id that can be used anywhere?

for example

Spoiler


Scriptname SidGemSafe extends ObjectReference
int NameList

int aicountGemAmethyst
int aicountGemAmethystFlawless
int aicountGemDiamond
int aicountGemDiamondFlawless
int aicountGemEmerald
int aicountGemEmeraldFlawless
int aicountGemGarnet
int aicountGemGarnetFlawless
int aicountGemRuby
int aicountGemRubyFlawless
int aicountGemSapphire
int aicountGemSapphireFlawless
int aicountIngotSilver
int aicountIngotGold

Formlist Property SidAmethystList Auto
Formlist Property SidAmethystFlawList Auto
Formlist Property SidDiamondList Auto
Formlist Property SidDiamondFlawList Auto
Formlist Property SidEmeraldList Auto
Formlist Property SidEmeraldFlawList Auto
Formlist Property SidGarnetList Auto
Formlist Property SidGarnetFlawList Auto
Formlist Property SidRubyList Auto
Formlist Property SidRubyFlawList Auto
Formlist Property SidSapphireList Auto
Formlist Property SidSapphireFlawList Auto
Formlist Property Silveritems Auto
Formlist Property golditems Auto


ObjectReference Property SidAmethyst Auto
ObjectReference Property SidAmethystFlaw Auto
ObjectReference Property SidDiamond Auto
ObjectReference Property SidDiamondFlaw Auto
ObjectReference Property SidEmerald Auto
ObjectReference Property SidEmeraldFlaw Auto
ObjectReference Property SidGarnet Auto
ObjectReference Property SidGarnetFlaw Auto
ObjectReference Property SidRuby Auto
ObjectReference Property SidRubyFlaw Auto
ObjectReference Property SidSapphire Auto
ObjectReference Property SidSapphireFlaw Auto
ObjectReference Property SidIngotSilver Auto
ObjectReference Property SidIngotGold Auto

FormList Property NameList Auto


event oncellattach()
debug.notification("Hello Sidrat, welcome Home")

; SidAmethystList
if self.getitemcount(SidAmethystList) >= 1
SidAmethyst.enable()
endif

; SidAmethystFlawList
if self.getitemcount(SidAmethystFlawList) >=1
SidAmethystFlaw.enable()
endif

; SidDiamondList
if self.getitemcount(SidDiamondList) >=1
SidDiamond.enable()
endif

; SidDiamondFlawList
if self.getitemcount(SidDiamondFlawList) >=1
SidDiamondFlaw.enable()
endif

; SidEmeraldList
if self.getitemcount(SidEmeraldList) >=1
SidEmerald.enable()
endif

; SidEmeraldFlawList
if self.getitemcount(SidEmeraldFlawList) >=1
SidEmeraldFlaw.enable()
endif

; SidGarnetList
if self.getitemcount(SidGarnetList) >=1
SidGarnet.enable()
endif

; SidGarnetFlawList
if self.getitemcount(SidGarnetFlawList) >=1
SidGarnetFlaw.enable()
endif

; SidRubyList
if self.getitemcount(SidRubyList) >=1
SidRuby.enable()
endif

; SidRubyFlawList
if self.getitemcount(SidRubyFlawList) >=1
SidRubyFlaw.enable()
endif

; SidSapphireList
if self.getitemcount(SidSapphireList) >=1
SidSapphire.enable()
endif

; SidSapphireFlawList
if self.getitemcount(SidSapphireFlawList) >=1
SidSapphireFlaw.enable()
endif

; Silveritems
if self.getitemcount(Silveritems) >=1
SidIngotSilver.enable()
endif

; golditems
if self.getitemcount(golditems) >=1
SidIngotGold.enable()
endif

endevent


Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
If akSourceContainer == Game.GetPlayer()
If !NameList.HasForm(akBaseItem)
RemoveItem(akBaseItem, aiItemCount, True, akSourceContainer)
Debug.notification("That Does Not Belong Here")
EndIf

; Checks to see which item was added to enable the relevant item in the display case

; SidAmethystList
if self.getitemcount(SidAmethystList) >= 1
SidAmethyst.enable()
endif

; SidAmethystFlawList
if self.getitemcount(SidAmethystFlawList) >=1
SidAmethystFlaw.enable()
endif

; SidDiamondList
if self.getitemcount(SidDiamondList) >=1
SidDiamond.enable()
endif

; SidDiamondFlawList
if self.getitemcount(SidDiamondFlawList) >=1
SidDiamondFlaw.enable()
endif

; SidEmeraldList
if self.getitemcount(SidEmeraldList) >=1
SidEmerald.enable()
endif

; SidEmeraldFlawList
if self.getitemcount(SidEmeraldFlawList) >=1
SidEmeraldFlaw.enable()
endif

; SidGarnetList
if self.getitemcount(SidGarnetList) >=1
SidGarnet.enable()
endif

; SidGarnetFlawList
if self.getitemcount(SidGarnetFlawList) >=1
SidGarnetFlaw.enable()
endif

; SidRubyList
if self.getitemcount(SidRubyList) >=1
SidRuby.enable()
endif

; SidRubyFlawList
if self.getitemcount(SidRubyFlawList) >=1
SidRubyFlaw.enable()
endif

; SidSapphireList
if self.getitemcount(SidSapphireList) >=1
SidSapphire.enable()
endif

; SidSapphireFlawList
if self.getitemcount(SidSapphireFlawList) >=1
SidSapphireFlaw.enable()
endif

; Silveritems
if self.getitemcount(Silveritems) >=1
SidIngotSilver.enable()
endif

; golditems
if self.getitemcount(golditems) >=1
SidIngotGold.enable()
endif
Endif

EndEvent


Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)


; SidAmethystList
if self.getitemcount(SidAmethystList) ==0
SidAmethyst.disable()
endif

; SidAmethystFlawList
if self.getitemcount(SidAmethystFlawList) ==0
SidAmethystFlaw.disable()
endif

; SidDiamondList
if self.getitemcount(SidDiamondList) ==0
SidDiamond.disable()
endif

; SidDiamondFlawList
if self.getitemcount(SidDiamondFlawList) ==0
SidDiamondFlaw.disable()
endif

; SidEmeraldList
if self.getitemcount(SidEmeraldList) ==0
SidEmerald.disable()
endif

; SidEmeraldFlawList
if self.getitemcount(SidEmeraldFlawList) ==0
SidEmeraldFlaw.disable()
endif

; SidGarnetList
if self.getitemcount(SidGarnetList) ==0
SidGarnet.disable()
endif

; SidGarnetFlawList
if self.getitemcount(SidGarnetFlawList) ==0
SidGarnetFlaw.disable()
endif

; SidRubyList
if self.getitemcount(SidRubyList) ==0
SidRuby.disable()
endif

; SidRubyFlawList
if self.getitemcount(SidRubyFlawList) ==0
SidRubyFlaw.disable()
endif

; SidSapphireList
if self.getitemcount(SidSapphireList) ==0
SidSapphire.disable()
endif

; SidSapphireFlawList
if self.getitemcount(SidSapphireFlawList) ==0
SidSapphireFlaw.disable()
endif

; Silveritems
if self.getitemcount(Silveritems) ==0
SidIngotSilver.disable()
endif

; golditems
if self.getitemcount(golditems) ==0
SidIngotGold.disable()
endif

EndEvent


I'm sure there's a much easier way of making static objects appear in a display case based on the inventory of a container.
User avatar
Lexy Dick
 
Posts: 3459
Joined: Mon Feb 12, 2007 12:15 pm

Post » Thu Jun 21, 2012 4:27 pm

I'm sure there's a much easier way of making static objects appear in a display case based on the inventory of a container.
Since there are 14 iCounts, FormLists, and ObjectReferences, this seems a perfect occasion to use array variables with corresponding indices.
Spoiler
ScriptName SidGemSafe extends ObjectReference Int[] Property CountArray Auto ; Contains integers to count each FLSTFormList[] Property FormListArray Auto ; Contains gem lists in alphabetical orderFormList Property NameList AutoObjectReference[] Property ObjectReferenceArray Auto ; Contains the gem references in alphabetical orderEvent OnCellAttach()	Debug.Notification("Hello Sidrat, welcome home!")	Maintain()EndEventEvent OnItemAdded(Form akBaseItem, Int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)	If !NameList.HasForm(akBaseItem) ; This has everything in all 14, I am assuming		RemoveItem(akBaseItem, aiItemCount, True, akSourceContainer)		Debug.Notification("That does not belong here.")	Else		Maintain()	EndIfEndEventEvent OnItemRemoved(Form akBaseItem, Int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)	Maintain()EndEventFunction Maintain(Int aiIndex = 15)	While aiIndex > 0		aiIndex -= 1		CountArray[aiIndex] = GetItemCount(FormListArray[aiIndex])		If ObjectReferenceArray[aiIndex].IsEnabled() != CountArray[aiIndex] As Bool			If CountArray[aiIndex] As Bool				ObjectReferenceArray[aiIndex].Enable()			Else				ObjectReferenceArray[aiIndex].Disable()			EndIf		EndIf	EndWhileEndFunction
You wouldn't need the below if they're plugged into the arrays.
Spoiler
;/ int aicountGemAmethystint aicountGemAmethystFlawlessint aicountGemDiamondint aicountGemDiamondFlawlessint aicountGemEmeraldint aicountGemEmeraldFlawlessint aicountGemGarnetint aicountGemGarnetFlawlessint aicountGemRubyint aicountGemRubyFlawlessint aicountGemSapphireint aicountGemSapphireFlawlessint aicountIngotSilverint aicountIngotGoldFormlist Property SidAmethystList AutoFormlist Property SidAmethystFlawList AutoFormlist Property SidDiamondList AutoFormlist Property SidDiamondFlawList AutoFormlist Property SidEmeraldList AutoFormlist Property SidEmeraldFlawList AutoFormlist Property SidGarnetList AutoFormlist Property SidGarnetFlawList AutoFormlist Property SidRubyList AutoFormlist Property SidRubyFlawList AutoFormlist Property SidSapphireList AutoFormlist Property SidSapphireFlawList AutoFormlist Property SilverItems AutoFormlist Property GoldItems AutoObjectReference Property SidAmethyst AutoObjectReference Property SidAmethystFlaw AutoObjectReference Property SidDiamond AutoObjectReference Property SidDiamondFlaw AutoObjectReference Property SidEmerald AutoObjectReference Property SidEmeraldFlaw AutoObjectReference Property SidGarnet AutoObjectReference Property SidGarnetFlaw AutoObjectReference Property SidRuby AutoObjectReference Property SidRubyFlaw AutoObjectReference Property SidSapphire AutoObjectReference Property SidSapphireFlaw AutoObjectReference Property SidIngotSilver AutoObjectReference Property SidIngotGold Auto /;

Just a few observations regarding optimization: In cases like the below...
Spoiler
if self.getitemcount(SidAmethystList) >= 1if self.getitemcount(SidSapphireFlawList) ==0
if getitemcount(SidAmethystList)if !getitemcount(SidSapphireFlawList)
...'Self', you'll find, is unnecessary as GetItemCount can be called implicitly and the '>= 1' and '== 0' could be removed given both conditions are bivalent.
User avatar
Miranda Taylor
 
Posts: 3406
Joined: Sat Feb 24, 2007 3:39 pm

Post » Thu Jun 21, 2012 6:16 pm

*if they want to add a dimmer switch, multicolored mood lighting, or whatever

Multi-colored mood lighting? Srsly Justin? Srsly?
User avatar
Rex Help
 
Posts: 3380
Joined: Mon Jun 18, 2007 6:52 pm


Return to V - Skyrim