Jump to content

question about code optimization


Recommended Posts

As I understand it,

put a RETURN as soon as possible in your scripts that you don't need running

so the game engine don't read everything looking for an exit

for instance this code

I don't want the script to run so I set skiprun to 1

 

short skiprun

 

if skiprun

RETURN

endif

insert lots of other code here

 

 

question I have is....

does this only apply to gamemode scripts?

how bout user created functions (OBSE)

or other script blocks such as...

begin menu 1011

 

my mod has a ton of code in the menu 1011

and I have a bunch of user created functions

 

I can add the RETURN commands on everything

I was just making sure I wasn't doing something that was

pointless.

 

 

 

Link to comment
Share on other sites

Actually, to my own understanding at least, they did understand it quite fine. Let's wait for QQuix or others of the true gurus to show up first, but as far as I know there is indeed a noticeable/provable difference between:

 

if (condition1)
    ;some code
else
    ;lots of other code
endif

and

 

if (condition1)
    ;some code
    return
endif
;lots of other code

insofar as ObScript also evaluates all those lines inside the "else" inside the first version, even if its condition was not met, while in the second version it doesn't.

 

I'm not quite in on "all" the details anymore, I'm afraid, and right now can't even really say "why" that is, but it was rather common knowledge the second version was to be preferred and the first avoided.

The CS Wiki seems to copy this, too: https://cs.elderscrolls.com/index.php?title=Minimizing_your_Script Although it might be outdated in some points by now.

Link to comment
Share on other sites

An article being old doesn't make it wrong. What would make it incorrect is if either the info was wrong from the start or Oblivion was eventually patched to fix this quirk. If the former, then that begs the question of why this false info has been there for so long. As for the latter, Oblivion itself hasn't been updated in about a decade and I have seen no evidence of any official patch fixing this quirk. I'm not aware of any unofficial patches fixing it either (assuming the quirk exists, that is). In fact, I wouldn't be surprised if Fallout 3 and New Vegas have the same quirk.

 

As for the original question, this quirk applies to every script in the game. For every script, the game will run through all code until it finds an exit point. This only really matters for GameMode and, to a lesser extent, MenuMode scripts. This is because these script blocks run every frame whereas the other event based blocks only run once each time the specified event occurs. That said, it probably is good practice to get into the habit of writing the scripts for all your older Bethesda games this way. The more important advice is to move as much of your GameMode and MenuMode code as possible into event based blocks that only run the code when you need it to rather than all the time.

Link to comment
Share on other sites

The reason for the grain of salt is that, at the time, there were several 'common knowledge' that, later, were proved wrong. Wiki editors adjusted most of them, but some wrong text may have gone unnoticed.

 

In the case at hand, the Wiki text raises interesting questions. Biggest of all is WHY? (why would a (supposedly) competent Beth developer do that?).

 

It kind of implies that I should do this at the beginning of all my Gamemode scripts:

if 1 = 2
     RETURN
endif
<main code>
Link to comment
Share on other sites

It kind of implies that I should do this at the beginning of all my Gamemode scripts:

if 1 = 2
     RETURN
endif
<main code>

 

Ahh, yes. Sometimes we're still really surprised of how much wrong Bethesda could do, even in this older (ancient? ^^) game.

 

But I do think your (hopefully needless) safety-check there won't actually ever fire at all, as it's not a comparison but an assignment inside. And we all know you cannot "assign" 1 to be 2... or at least outside of a Bethesda game we cannot. :laugh:

Link to comment
Share on other sites

Oh... I am a little rusty on Oblivion syntax. I meant an if that is always false (should it be "if 1 == 2" ?)

 

Anyways . . .

I've just read the Discussion page for that article. As one can read at the end, Dragoon Wraith (one of the top Sheriffs of the WIKI at the time) considered removing the whole article as irrelevant to performance.

Link to comment
Share on other sites

I just decided to do some testing to try get to the bottom of this and I was honestly surprised at the results.
I created a plugin with a script that had ~3500 "GetDistance PlayerRef" calls in a Gamemode block. I then attached this script to a friendly Dremora and placed 30 copies of him in the Oak and Crosier's basement. I then saved my game in this basement and started my tests. I each case, I restarted the game, loaded my save from the main menu, waited for the game to settle down then opened the debug menu to get my FPS.

Test 1: GameMode script with ~3500 "GetDistance PlayerRef" calls: FPS was 23.

Begin GameMode

< Insert 3500 "GetDistance PlayerRef calls >

End


Test 2: Same script except I added "Return" above those 3500 calls, aborting the script before any distance calculations were made: FPS was 29.

Begin GameMode

Return
< Insert 3500 "GetDistance PlayerRef calls >

End

Test 3: Same script as 1 except I enclosed the entire block of GetDistance calls in a "If 1 == 2" conditional: FPS was 29.

Begin GameMode

If 1 == 2
< Insert 3500 "GetDistance PlayerRef calls >
EndIf

End

Unless I've done something wrong, my tests seem to indicate that the claim "the script engine would process all code inside of an If block (even if the condition was false) until the script engine could find an exit point" is completely wrong. To make things worse, that article links to a forum thread that apparently talks about this quirk in more detail but it's a dead link. I can't even search Bethesda's forum for this thread because it no longer allows me to login.

Edit: Ignore the above. I did some more tests and found that I did actually do something wrong.

That said, QQuix, with all due respect, your posts give me the impression that you don't know what the article is talking about. If this quirk is actually real, it means that every piece of code inside an If block is executed until it hits a Return regardless of whether the If check is true or false. The only difference would be that the code executed inside a false If block won't actually do anything. This means that putting "Return" in a "If 1 == 2" block as you proposed would not change anything at all; the script would simply proceed beyond this "always false If" and continue implementing this buggy behaviour on all the code below.

The main problem I see with the Talk page is that DragoonWraith didn't seem to take the big picture into account (and even admitted that he only cared about performance in so far as it affected his game). Yes, it is unlikely that a single script-heavy mod will cause problems in someone's game. However, when you have dozens of script-heavy mods combined with graphics enhancers that place further strain, that is when optimisation starts to matter. Especially with how ridiculously easy it is to make single-threaded CPU performance the bottleneck for Oblivion and how single-threaded performance in the last decade has not increased as much as one would think. His proposal to delete the article was in the context of re-writing it.

And on a side note, it's not a good idea to use "Beth developer" and "competent" in the same sentence, especially after Fallout 76. :wink:

Edited by XJDHDR
Link to comment
Share on other sites

  • Recently Browsing   0 members

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