Optimise my clunky code

Post » Wed Jun 20, 2012 4:51 am

Hi guys, i'm teaching myself papyrus (very gradually) I haven't coded since high school and that was in turbo pascal (yes that long ago) so it's slow going. I've written a script for my player house: When the player enters a trigger zone (directly in front of the entrance) the script checks to see if any of the fast travel markers to the standing stones (ie guardian stones mage, thief and warrior) are enabled, if they are it enables clones of the standing stones within the house. (this way you don't have to fast travel everywhere when you want to change star signs. But the code is very basic and as I look at it, I wonder if theres not a more elegant way to do this. If you're good at this sort of thing, please take a look and advise on any improvements:

Spoiler


Scriptname FFM_Doomstone_Enable extends ObjectReference;Check to see if player has discovered doomstones and if so enables them for use in the houseObjectReference Property DStoneMarker01  AutoObjectReference Property DStoneMarker02  AutoObjectReference Property DStoneMarker03  AutoObjectReference Property DStoneMarker04  AutoObjectReference Property DStoneMarker05  AutoObjectReference Property DStoneMarker06  AutoObjectReference Property DStoneMarker07  AutoObjectReference Property DStoneMarker08  AutoObjectReference Property DStoneMarker09  AutoObjectReference Property DStoneMarker10  AutoObjectReference Property DStoneMarker11  AutoObjectReference Property FTMarker01 AutoObjectReference Property FTMarker02 AutoObjectReference Property FTMarker03 AutoObjectReference Property FTMarker04 AutoObjectReference Property FTMarker05 AutoObjectReference Property FTMarker06 AutoObjectReference Property FTMarker07 AutoObjectReference Property FTMarker08 AutoObjectReference Property FTMarker09 AutoObjectReference Property FTMarker10 AutoObjectReference Property FTMarker11 AutoEvent OnTriggerEnter(ObjectReference akActionRef);When player enters trigger zone, checks to see if any of the stones are disabled and if they are and the player can fast travel to them, it enables in-house stones.	bool canFastTravel = FTMarker01.CanFastTravelToMarker()bool canFastTravel01 = FTMarker01.CanFastTravelToMarker()bool canFastTravel02 = FTMarker02.CanFastTravelToMarker()bool canFastTravel03 = FTMarker03.CanFastTravelToMarker()bool canFastTravel04 = FTMarker04.CanFastTravelToMarker()bool canFastTravel05 = FTMarker05.CanFastTravelToMarker()bool canFastTravel06 = FTMarker06.CanFastTravelToMarker()bool canFastTravel07 = FTMarker07.CanFastTravelToMarker()bool canFastTravel08 = FTMarker08.CanFastTravelToMarker()bool canFastTravel09 = FTMarker09.CanFastTravelToMarker()bool canFastTravel10 = FTMarker10.CanFastTravelToMarker()bool canFastTravel11 = FTMarker11.CanFastTravelToMarker()			If (DStoneMarker01.IsDisabled())   if canFastTravel01 == true	DStoneMarker01.Enable()   endif			endif			If (DStoneMarker02.IsDisabled())   if canFastTravel02 == true	DStoneMarker02.Enable()   endif			endif			If (DStoneMarker03.IsDisabled())   if canFastTravel03 == true	DStoneMarker03.Enable()   endif			endif		   If (DStoneMarker04.IsDisabled())   if canFastTravel04 == true	DStoneMarker04.Enable()   endif			endif			If (DStoneMarker05.IsDisabled())   if canFastTravel05 == true	DStoneMarker05.Enable()   endif			endif  If (DStoneMarker06.IsDisabled())   if canFastTravel06 == true	DStoneMarker06.Enable()   endif			endif			 If (DStoneMarker07.IsDisabled())   if canFastTravel07 == true	DStoneMarker07.Enable()   endif			endif			If (DStoneMarker08.IsDisabled())   if canFastTravel08 == true	DStoneMarker08.Enable()   endif			endif			 If (DStoneMarker09.IsDisabled())   if canFastTravel09 == true	DStoneMarker09.Enable()   endif			endif			If (DStoneMarker10.IsDisabled())   if canFastTravel10 == true	DStoneMarker10.Enable()   endif			endif			 If (DStoneMarker11.IsDisabled())   if canFastTravel11 == true	DStoneMarker11.Enable()   endif			endifEndEvent

User avatar
Star Dunkels Macmillan
 
Posts: 3421
Joined: Thu Aug 31, 2006 4:00 pm

Post » Wed Jun 20, 2012 5:53 am

Perhaps you should just link each of your clones to the originals, then in your script you just check:

if (DStoneMarker01.IsDisabled())	if (DStoneMarker01.GetLinkedRef().CanFastTravelToMarker())		DStoneMarker01.Enable()	endifendif;etc...

If you don't mind setting up an array, you could even simplify the whole script down to this:

Scriptname ExampleScript extends ObjectReferenceObjectReference[] property DStoneMarkers autoEvent OnTriggerEnter(ObjectReference akActionRef)	int index = 0	while (index < DStoneMarkers.length)		if (DStoneMarkers[index].IsDisabled())			if (DStoneMarkers[index].GetLinkedRef().CanFastTravelToMarker())				DStoneMarkers[index].Enable()			endif		endif		index += 1	endwhileEndEvent
User avatar
celebrity
 
Posts: 3522
Joined: Mon Jul 02, 2007 12:53 pm

Post » Wed Jun 20, 2012 3:22 am

Dude thank you so much, the Array works perfectly, (I tried to make on at first, but my layout of the While function was completely wrong) And that is so much simpler and easier to understand than what I had.
User avatar
Naazhe Perezz
 
Posts: 3393
Joined: Sat Aug 19, 2006 6:14 am


Return to V - Skyrim