Aller au contenu

Photo

Expensive Functions


  • Veuillez vous connecter pour répondre
1 réponse à ce sujet

#1
Lugaid of the Red Stripes

Lugaid of the Red Stripes
  • Members
  • 955 messages
When scripting, it seems really simple and efficient just to use one of the standard functions, or some cool function you found in an include, than to script your own function.  But some of those standard functions might not be so simple and effiecient. 

For example, I like to use the function "GetNearestObjectByTag".  For a human, the concept seems really simple, just grab the nearest guy named "Bob" or whatever.  But how does the computer do it?  I imagine that it has to look through some list of objects, one by one, check the tag, and then pull out the Pythagorean theorem to calculate the distance to each one.  If the game keeps a list of every object in an area, this wouldn't be so bad.  But what if it just has one big list of all the objects in the module?  Then every time you use this function, the engine is cycling through every goblin, door, barrel, and waypoint out there. So it's not so simple and efficient after all.  So maybe you can use a local object instead of always looking for that tag.

So I was hoping that we could put together a list of the most expensive functions in the game, maybe even with some kind of point system to show exactly how expensive they are.  This shouldn't be taken as a 'black list'-never use these functions kinda thing, though maybe a black list is warranted in some cases, just a guide to help scripters keep things simple and efficient.

#2
painofdungeoneternal

painofdungeoneternal
  • Members
  • 1 799 messages
They have comments in the official nwscript.nss to the effect of

PLEASE NOTE: This is an expensive function and may degrade performance if used frequently.


Going to http://70.179.46.242:8000/csl/ and entering expensive in the search box, i find CalcSafeLocation, LineOfSightVector, LineOfSightObject. These are probably the worst as it's doing a lot of work you don't see.

I'd add in anything which loops inside it's function. Also any official functions which were not rewritten, most of the AI. Loops can of course be nothing, but inventory iterating, and loops that loop each creature in turn can be nasty and as a scripter you just won't know. I would suggest preferring the main functions inside nwscript.nss which are core to the game whenever possible.

GetLocalString is ok IF you only have a few vars on the creature. It is actually iterating all the variable names on the target each time you do it, which is not what you assume. If you have over 5000 variables on an object, it can be bad, but it can handle a lot.

Anything related to the bioware database, it's a mess. ( storing object and retrieving them is nasty )

Creating objects - worse if from a blueprint so if you are making a lot of the same creature, put one in memory and create copies. ( the spawning systems do this if done well )

xp_profiler ( with NWNx ), or using some of the new things skywing is adding in can be helpful, but you have to test your module using the server program. Well worth the testing though, generally you make a mess, and figure out after wards where you need to clean it up which tends to be inside the loops.