Optimization: Single long script vs multiple small ones

Post » Tue Apr 27, 2010 1:35 pm

I'm working on a script at the moment that's supposed to run as a quest script at mininum delay time, so naturally im trying to optimize it as good as i can.

basically it consists of 3 or 4 main blocks, each of them could be sourced out to a different quest script, and i wonder if its worth it
+ easier to read
+ optimizable with early returns
- higher overhead?
- synchronization issues?

i forgot how concurrency was handled in the engine. can i assume that each script is atomic?

and finally, how is the performance of startQuest, isQuestRunning etc
would it be worth it to start/stop certain quests on demand or is it generally faster to let them run all the time and just make a well-placed return.

i know these questions are hard to answer, but maybe someone knows something :)
User avatar
Donatus Uwasomba
 
Posts: 3361
Joined: Sun May 27, 2007 7:22 pm

Post » Tue Apr 27, 2010 9:17 pm

What does minimum delay time mean?

if you're serious about that, do you really need it to go that fast? And for how long were you going to keep it at that speed?

To really get a sense of what your situation is, you'd need to explain a bit more about what you are using the quest scripts for.
User avatar
kirsty joanne hines
 
Posts: 3361
Joined: Fri Aug 18, 2006 10:06 am

Post » Tue Apr 27, 2010 9:01 pm

What does minimum delay time mean?

0.0150

if you're serious about that, do you really need it to go that fast? And for how long were you going to keep it at that speed?

1. It has to process input events from he player constantly so it has to react asap.
2. Forever? :D

To really get a sense of what your situation is, you'd need to explain a bit more about what you are using the quest scripts for.


Well, I could post it for a start:
scn ZRMainScriptshort initshort zoomTrap; 1 = no weapon; 2 = holstered weapon; 3 = weapon without scope; 4 = weapon with scopeshort modeshort lastModeshort zoomingshort reloadingshort holdingBreathfloat timershort holdTimeref weaponref lastWeaponfloat weaponFOVfloat weaponRange; Keysshort zoomKeyshort internalZoomKeyshort holdBreathKey;----------------------------------------------------------------------------------------------------------------------------------------------; Init blockBegin GameModeif init == 0	set init to 1	set zoomKey to 257		; left mouse button	set internalZoomKey to 38; L key	set holdBreathKey to 56	; left shift keyendifEnd;----------------------------------------------------------------------------------------------------------------------------------------------; Mode setting blockBegin GameModeset weapon to player.getEquippedObject 5if weapon == 0	set mode to 1elseif player.isWeaponOut == 0	set mode to 2elseif getWeaponHasScope weapon == 0	set mode to 3else	set mode to 4endifif lastMode != mode || weapon != lastWeapon; Remove zoom effect for scopes	if mode == 1 || mode == 2 || mode == 3		con_SetGameSetting "fIronSightsFOVTimeChange", "0.25"	else		con_SetGameSetting "fIronSightsFOVTimeChange", "0"		set weaponFOV to getWeaponSightFOV weapon		set weaponRange to getWeaponMaxRange weapon	; TODO: Zoom		if weaponFOV <= 25 && weaponRange >= 2000			PrintToConsole "3 StepZoom" 		elseif weaponFOV <= 35 && weaponRange >= 1000			PrintToConsole "2 StepZoom"		elseif weaponFOV <= 40			PrintToConsole "1 StepZoom"		endif	endif; Reset everything when mode changed while zooming	if zooming == 1		releaseKey internalZoomKey		set zoomTrap to 0		set zooming to 0	endifendifset lastMode to modeset lastWeapon to weaponEnd;----------------------------------------------------------------------------------------------------------------------------------------------; Hold breath blockBegin GameMode; Regenerate/Keep holdingif holdTime > 0	if timer > 0		set timer to timer - getSecondsPassed	else		set timer to 1	; Regenerate		if holdingBreath == 0			if holdTime == 1				PrintToConsole "1.0"				con_SetGameSetting "fGunWobbleMultScope", "1.0"			elseif holdTime == 2				PrintToConsole "2.0"				con_SetGameSetting "fGunWobbleMultScope", "2.0"			elseif holdTime >= 3				PrintToConsole "3.0"				con_SetGameSetting "fGunWobbleMultScope", "3.0"			elseif holdTime >= 4				PrintToConsole "4.0"				con_SetGameSetting "fGunWobbleMultScope", "4.0"			endif			set holdTime to holdTime - 1	; Keep Holding		else			if holdTime >= 4				set holdingBreath to 0				PrintToConsole "4.0"				con_SetGameSetting "fGunWobbleMultScope", "4.0"				playSound ZRReleaseBreathLong			else				set timer to 1				set holdTime to holdTime + 1				playSound ZRHeartBeat			endif		endif	endifendif; Scoped weapon, zooming, hold breath key pressed and not firing a weaponif mode == 4 && zooming == 1 && isKeyPressed holdBreathKey == 1 && player.getAnimAction != 2			if holdingBreath == 0		if holdTime == 0			set holdingBreath to 1			PrintToConsole "0.33"			con_SetGameSetting "fGunWobbleMultScope", "0.33"			set timer to 1			set holdTime to 1			playSound ZRHoldBreath		endif	endif; No scoped weapon or not zooming or not pressing hold breath key or firing a weaponelse	if holdingBreath == 1		set holdingBreath to 0					if holdTime >= 3			PrintToConsole "3.0"			con_SetGameSetting "fGunWobbleMultScope", "3.0"			playSound ZRReleaseBreathMedium		else			PrintToConsole "2.0"			con_SetGameSetting "fGunWobbleMultScope", "2.0"			playSound ZRReleaseBreathShort		endif	endifendifEnd;----------------------------------------------------------------------------------------------------------------------------------------------; Main blockBegin GameMode;---------------------------------------------------------------; Default  zooming behaviourif mode == 1 || mode == 2 || mode == 3	if isKeyPressed zoomKey == 1		if zoomTrap == 0			holdKey internalZoomKey			set zooming to 1			set zoomTrap to 1		endif	else		if zoomTrap == 1			releaseKey internalZoomKey			set zooming to 0			set zoomTrap to 0		endif	endif	return;---------------------------------------------------------------; Toggle zoomingelseif mode == 4; Handle reload	if player.getAnimAction == 9		if zooming == 1 && reloading == 0			releaseKey 38			set zooming to 0			set reloading to 1		endif	else		if reloading == 1			holdKey 38			set zooming to 1			set reloading to 0			return		endif	endif			if isKeyPressed zoomKey == 0		set zoomTrap to 0		return	endif	if zoomTrap == 1		return	endif	set zoomTrap to 1; Zoom in	if zooming== 0					holdKey 38		set zooming to 1; Zoom out	else		releaseKey 38		set zooming to 0	endif	returnendifEnd


What it does, in a nutshell:
- Make the zoom for scoped weapons toggleable and instant
- Add the ability to hold your breath while zooming.
User avatar
sam
 
Posts: 3386
Joined: Sat Jan 27, 2007 2:44 pm

Post » Tue Apr 27, 2010 10:10 am

0.0150


1. It has to process input events from he player constantly so it has to react asap.
2. Forever? :D



Well, I could post it for a start:
[code]scn ZRMainScript

short init
short zoomTrap

; 1 = no weapon
; 2 = holstered weapon
; 3 = weapon without scope
; 4 = weapon with scope

What it does, in a nutshell:
- Make the zoom for scoped weapons toggleable and instant
- Add the ability to hold your breath while zooming.


IMO - find a way to make this script not be at this speed for this long a time. Remember that you can change the script's speed itself using setquestdelay. Try like hell to only speed it up when it really needs the speed. Also, remember that if you're watching for an event, the average responsiveness you get is half of your script processing time. Like, if you set your quest delay for 1 second, the average response time that a player will notice will be half a second, because they will do their input-event at any time during that 1 second script timing, be it .2 of a second from the quest's execution, .5, or .7.

My mod Phalanx is probably the chief abuser of quest scripts out of all mods on this forum, I'm constantly running over 15 quest scripts. The fastest one I run is .2193 speed or something, and this quest turns itself off if the player enters combat. I have another one that runs at 3 seconds but auto-adjusts itself up to 1 second timing, and then turns itself back to 3 when the event it is waiting for happens. I can tell you that you want to take it easy on quest script timing whenever possible, people's CPUs do a whole lot during busy combat, and so on.

Your script does not seem long to me, I would totally keep that as one.
User avatar
patricia kris
 
Posts: 3348
Joined: Tue Feb 13, 2007 5:49 am

Post » Tue Apr 27, 2010 11:08 am

My DUI config quest has two speeds: normal (1), which simply runs in the Start Menu, and fast (0.05) which runs when the DUI settings are visible. I find .05 to be quick enough for UI response, giving 20 fps for animations as well. I'm not sure about zoom toggle, though 66.66 times per second seems a lot to me.
User avatar
Trent Theriot
 
Posts: 3395
Joined: Sat Oct 13, 2007 3:37 am

Post » Tue Apr 27, 2010 9:18 pm

While of course what you're saying is reasonable, I just tried higher delays and to detect key presses and its not feasible. If i use a quest delay time of 0.1 for example to detect mouse wheel up, i have to move it up 3-5 "clicks" until it finally fires. even with 0.05, every other rightclick is missed. In this case its not about response time, but about the fact that the script must process the getKeyPress exactly when the button is held down or else the event is missed. Maybe i should move event detection to an extra script with mininum delay to queue events for other scripts.
User avatar
Steven Nicholson
 
Posts: 3468
Joined: Mon Jun 18, 2007 1:24 pm

Post » Tue Apr 27, 2010 1:06 pm

While of course what you're saying is reasonable, I just tried higher delays and to detect key presses and its not feasible. If i use a quest delay time of 0.1 for example to detect mouse wheel up, i have to move it up 3-5 "clicks" until it finally fires. even with 0.05, every other rightclick is missed. In this case its not about response time, but about the fact that the script must process the getKeyPress exactly when the button is held down or else the event is missed. Maybe i should move event detection to an extra script with mininum delay to queue events for other scripts.


Mousewheel? wow.. yeah that would be some fast events :/
User avatar
April D. F
 
Posts: 3346
Joined: Wed Mar 21, 2007 8:41 pm


Return to Fallout 3