Does writing a custom function speed up scripts?

Post » Wed Jun 20, 2012 10:50 am

If I do:

Function FlyUp()    Ref.PushActorAway(Ref, 0)    Ref.ApplyHavokImpulse(0.0, 0.0, 1.0, 20000)EndFunctionEvent OnEffectStart(Actor Target, Actor Caster)     Target.FlyUp()EndEvent

faster than just writing out it all?
User avatar
emily grieve
 
Posts: 3408
Joined: Thu Jun 22, 2006 11:55 pm

Post » Wed Jun 20, 2012 7:31 am

No, it's slower because of the added overhead of a function call.

The language doesn't allow functions to be declared hidden, this prevents the compiler from doing inline optimization's.
User avatar
Stu Clarke
 
Posts: 3326
Joined: Fri Jun 22, 2007 1:45 pm

Post » Wed Jun 20, 2012 3:33 pm

If I had something like this:

Event OnEffectStart()    if (Utility.RandomInt(0, 99) > 0)        ;do something    else        ;extremely large amount of code    endif    ;some more code so can't use a Return earlierEndEvent

would having a function make any difference?
User avatar
Travis
 
Posts: 3456
Joined: Wed Oct 24, 2007 1:57 am

Post » Wed Jun 20, 2012 5:27 pm

Functions won't make anything faster, though that isn't an issue -- they weren't meant to do so.

Instead functions ...
  • allow code-reuse -- no buggy copy-&-paste.
  • are callable from external sources -- scripts can call other scripts (that's how you can access properties).
  • are named -- improves code readability.
User avatar
nath
 
Posts: 3463
Joined: Mon Jan 22, 2007 5:34 am

Post » Wed Jun 20, 2012 9:07 pm

Not much. Functions are best used where you need to use the same bit of code in multiple places, particularly if you need to access the code from outside the script.
User avatar
Ashley Hill
 
Posts: 3516
Joined: Tue Jul 04, 2006 5:27 am


Return to V - Skyrim