r1731 (Efficiency in LUA)

Post Reply
Zedingo
Developer
Posts: 85
Joined: Tue Aug 14, 2012 12:24 pm

r1731 (Efficiency in LUA)

Post by Zedingo » Wed Sep 12, 2012 11:58 pm

Since diatanato posts his changelog in Russian, and there are some people who are new to LUA and/or new to the project but don't know how to read C++ source about, let me translate what he's asking you to do:

Code: Select all

if(target:getStatusEffect(EFFECT_ACCURACY_DOWN) ~= nil) or (caster:getStatusEffect(EFFECT_ACCURACY_BOOST) ~=nil) then
replaced with:

Code: Select all

if(target:hasStatusEffect(EFFECT_ACCURACY_DOWN) or caster:hasStatusEffect(EFFECT_ACCURACY_BOOST)) then
There's already a lot of stuff handled in the source code, and C++ is MUCH faster than parsed LUA scripts. If you use target:hasStatusEffect(), then you will get a boolean (true/false) returned. But if you use target:getStatusEffect(), you'll get an object (I believe in this case a number), which you must compare to nil/null to get your boolean. For efficiency's sake, use the already coded functions instead of trying to handle it yourself in the LUA script.


Code: Select all

if(target:getStatusEffect(EFFECT_BLINDNESS) ~= nil) then
target:delStatusEffect(EFFECT_BLINDNESS);
replaced with:

Code: Select all

if (target:delStatusEffect(EFFECT_BLINDNESS)) then
If you simply call delStatusEffect(), it will return true if the target had the effect and it was able to delete said effect. It's an unnecessary extra step to check first.


Code: Select all

	dINT = (caster:getStat(MOD_INT) - target:getStat(MOD_INT));
replaced with:

Code: Select all

	local dINT = (caster:getStat(MOD_INT) - target:getStat(MOD_INT));
DECLARE YOUR VARIABLES AS LOCAL IF YOU DON'T NEED THEM ELSEWHERE! This cannot be stressed enough. Local vars have a sizable performance improvement.


Code: Select all

if(target:getStatusEffect(EFFECT_BLINDNESS) ~= nil) then

	if((target:getStatusEffect(EFFECT_BLINDNESS)):getPower() < power) then
replaced with:

Code: Select all

			local blindness = target:getStatusEffect(EFFECT_BLINDNESS);
				if(blindness ~= nil) then
					if(blindness:getPower() < power) then
If you call a function more than once, put it's result in a LOCAL variable. Every time you call the function, the server has to look that function up, slowing everything down. For instance, if you need to check a player's neck slot, we would not use target/caster:getEquipID(9) every place we wanted to check. Instead we would do this:

Code: Select all

local sNeck = target:getEquipID(9);
if (sNeck == whatever) then
(I'm not commanding/asking you to use sNeck for neck. You may name the variable whatever you wish, but please make the name intuitive.)


The last part of dia's changes is just that you do not need to call updateEnmity for a spell. That is already handled by the C++ code.

User avatar
diatanato
Developer
Posts: 112
Joined: Thu Aug 30, 2012 9:59 pm

Re: r1731 (Efficiency in LUA)

Post by diatanato » Thu Sep 13, 2012 12:50 am

thanks for translation ^^

Post Reply