Jump to content

Recommended Posts

Posted

Below is a script I wrote for a mod I am working on. This script and variations thereof are attached to a number of lights in a new area I have created.

The purpose of this script is to achieve a strobe light effect. I am new to scripting and am worried about my use of 'while'

 

 

Scriptname DepravedLightOnOff extends ObjectReference

 

 

ObjectReference Property ClubLight Auto

 

int LightCheck

 

Event OnCellAttach()

LightCheck = 1

While LightCheck == 1

ClubLight.disable()

Utility.Wait(0.06)

ClubLight.enable()

Utility.Wait(0.06)

EndWhile

EndEvent

 

Event onCellDetach()

LightCheck = 0

EndEvent

 

My first question is...

 

Will this script impact on memory overtime? e.g. each time the user loads the cell the lights using this script are in. etc

 

If it will have an impact, does anyone know of an alternative way to achieve the same 'strobe light' effect without impacting on memory/performance overtime.

 

If it won't have an impact , does anyone know of a better way to achieve the same 'strobe light effect'

 

My second question is does anyone have any suggestions on how to go about writing a Strobe Light toggle script? -The idea of this script is to give the users of my mod the option of toggling the strobe lights on or off permanently.

 

Any assistance with these questions would be greatly appreciated.

 

Cheers,

FatalisticZero

Posted

I might be completely off my rocker, but that code looks suspiciously like one that would just run ad infinitum.

 

Also, if shadows are ever cast by a light changing that frequently, that would cause some slowdown since it'd keep redrawing the shadows over and over.

 

I'm not going off my experience with Papyrus so much as I am coding in general, so take that for what it's worth.

Posted

mmm thanks for that DaedalusMachina, the infinite loop thing is my concern, you are the second person to mention that. I really need to redo this script in a way that will not go on infinite loops before a full release of my mod but I'm not sure how...oh well I just have to learn more I guess..

As for the shadow redraw thing, the lights haven't been causing me any lag in game and none of the beta users of my mod have complained thus far...hopefully it stays that way haha.

Thanks again DaedalusMachina.

Posted (edited)

I've modified the script a little.

1) Starts when the cell's 3D is loaded. Stops when the cell's 3D is unloaded or whenever the cell detaches.

2) Added a safety check within the while loop to abort the loop immediately whenever the cell unloads (because OnUnload can be a bit slow to execute due to the way code gets threaded).

 

Performance hit will be negligible. I've tested dozens of scripts with a while loop like this, all running simultaneously, and the performance hit was surprisingly small (provided the code is carefully written).

 

Even more interesting is that while loops are MUCH more game-friendly than RegisterForUpdate is! Updates can pile up and cause ugly stack dumps in the debug logs.

 

Hint: enable your Papyrus logs, and check them occasionally to make sure that your script doesn't spam the log with errors, like many of Bethesda's scripts do. You probably will get a couple of errors occasionally if the cell unloads during the while loop, BUT the loop will exit almost immediately due to the catch that I put in. On the otherhand, Bethesda has neglected to put such catches in their scripts, which is why you will find so many errors in the logs due to the 3D or cell unloading while the vanilla scripts are running :cool:

 

Scriptname DepravedLightOnOff extends ObjectReference 

ObjectReference Property ClubLight Auto 

int LightCheck

Event OnLoad()
LightCheck = 1

While LightCheck == 1
	; Quit whenever our 3D gets unloaded while the loop is running
	; this should help prevent errors in the trace log
	If !Is3DLoaded() || !GetParentCell()
		LightCheck = 0
		Return
	EndIf

	ClubLight.disable()
	Utility.Wait(0.06)
	ClubLight.enable()
	Utility.Wait(0.06)
EndWhile
EndEvent

Event onCellDetach()
LightCheck = 0
EndEvent

Event OnUnload()
LightCheck = 0
EndEvent

Edited by steve40
Posted (edited)

Thanks steve40

 

...I added your modifications to the script but it didn't work out...

 

 

Performance hit will be negligible. I've tested dozens of scripts with a while loop like this, all running simultaneously, and the performance hit was surprisingly small (provided the code is carefully written).

 

Even more interesting is that while loops are MUCH more game-friendly than RegisterForUpdate is! Updates can pile up and cause ugly stack dumps in the debug logs.

 

 

If it is true that while loops are more game friendly than RegisterForUpdate, I think for now I will keep my script as it is and if users start having issues I will try and rewrite/modify it.

Edited by FatalisticZero
Posted
Btw, I didn't test the script, no CK on this computer :whistling: Will test it out tonight :tongue: . Might have to omit the GetParentCell() part maybe.
Posted

Well anything that will make the script more efficient/ more likely to disable etc when it isn't needed is most welcome and appreciated :D haha.

 

Don't suppose you have any ideas for a script to allow users to toggle the strobe lights on or off? xD

Posted (edited)

Try this script (again, I haven't tried compiling it). Just add a switch to the room and make it the activate parent of the object that the script is attached to.

 

Scriptname DepravedLightOnOff extends ObjectReference 

ObjectReference Property ClubLight Auto 

bool LightCheck = false

Event onCellDetach()
       LightCheck = false
EndEvent

Event OnUnload()
       LightCheck = false
EndEvent

Event OnActivate(ObjectReference akActionRef)
LightCheck = !LightCheck
If LightCheck
	Strobe()
EndIf
EndEvent

Function Strobe()
       While LightCheck == true
               ; Quit whenever our 3D gets unloaded while the loop is running
               ; this should help prevent errors in the trace log
               If !Is3DLoaded()
                       LightCheck = false
                       Return
               EndIf

               ClubLight.disable()
               Utility.Wait(0.06)
               ClubLight.enable()
               Utility.Wait(0.06)
       EndWhile
EndFunction

 

Edit: it compiles OK.

Edited by steve40
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...