The most ignorant script you will ever see

Post » Wed Sep 01, 2010 11:08 pm

Consider the following portion of a script:

Spoiler
SCN MyScript

Ref SelfEA
Ref CriticalHealthEA

Begin GameMode

If ( SelfEA.GetLevel >= 8 )
Set CriticalHealthEA to 5
Elseif ( SelfEA.GetLevel >= 4 )
Set CriticalHealthEA to 4.5
Elseif ( SelfEA.GetLevel >= 1 )
Set CriticalHealthEA to 4
EndIf

End


You can see that, depending on the level of SelfEA a number will be set for CriticalHealthEA. So if SelfEA is at or above level 8 then CriticalHealthEA will be set to 5, but since it is GameMode it will be set to whatever number EVERY frame, hogging resources. Right? Are you laughing at me yet? Pal, we've only just started. My OCD is begging me to change the whole block to the following:

Spoiler

If ( SelfEA.GetLevel >= 40 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 10
Elseif ( SelfEA.GetLevel >= 36 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 9
Elseif ( SelfEA.GetLevel >= 32 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 8
Elseif ( SelfEA.GetLevel >= 28 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 7.5
Elseif ( SelfEA.GetLevel >= 24 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 7
Elseif ( SelfEA.GetLevel >= 20 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 6.5
Elseif ( SelfEA.GetLevel >= 16 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 6
Elseif ( SelfEA.GetLevel >= 12 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 5.5
Elseif ( SelfEA.GetLevel >= 8 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 5
Elseif ( SelfEA.GetLevel >= 4 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 4.5
Elseif ( SelfEA.GetLevel >= 1 ) && ( CriticalHealthEA == 0 )
Set CriticalHealthEA to 4
EndIf


There. So the script can now see that, for example, level 8 that CriticalHealthEA is at 0, so that it can then change it to 5. And after the fact, since one of the requirements for that change was for CriticalHealth to start at 0 then it will no longer process that line again and now the computer WON'T catch on fire and explode. Tell me straight, is this an unnecessary step ventured only to address my anxiety and stomp an irrational fear?
User avatar
Jennifer Munroe
 
Posts: 3411
Joined: Sun Aug 26, 2007 12:57 am

Post » Wed Sep 01, 2010 11:24 am

No, you are right...the first way wouldnt work very well.....but the second way is also less effient then just using nested ifs:

Begin GameModeif (CriticalHealthEA == 0 )	If ( SelfEA.GetLevel >= 8 )		Set CriticalHealthEA to 5	Elseif ( SelfEA.GetLevel >= 4 )		Set CriticalHealthEA to 4.5	Elseif ( SelfEA.GetLevel >= 1 )		Set CriticalHealthEA to 4	EndIfendifEnd

User avatar
cutiecute
 
Posts: 3432
Joined: Wed Sep 27, 2006 9:51 am

Post » Wed Sep 01, 2010 2:37 pm

Ah, thank you. The way you showed it means that the whole block is contingent on the 0, and so will not process again, uses less characters, and is likely much quicker. Scripting is water.
User avatar
Claire
 
Posts: 3329
Joined: Tue Oct 24, 2006 4:01 pm

Post » Wed Sep 01, 2010 1:30 pm

The second script is considerably less efficient than the first, because you're running an extra dozen conditions versus a single unnecessary assignment. da mage has the right of it, for best practices.

I would, however, point out that all three scripts could run literally hundreds of times per frame before affecting performance. Best practice is always preferred and it's good to not waste resources, but don't pull your hair out worrying about it. Especially with that second script, where it becomes ugly and hard to read - even if it was more efficient, I would argue the decrease in readability is not worth the performance increase.
User avatar
casey macmillan
 
Posts: 3474
Joined: Fri Feb 09, 2007 7:37 pm


Return to IV - Oblivion