Page 1 of 2

Optimization of a script

Posted: Sat Feb 18, 2017 7:57 pm
by vieuxchat
In my mod, several players said that one of my level was slowed down a lot.
Here is probably the culprit :

Code: Select all

function MAJ()
	local r,g,b = castle_arena_sky_1.light:getColor()
	for i =1,100 do
		local entity = findEntity("lumieres_fenetre_minimal_save_state_"..i)
		if entity ~= nil then entity.light:setColor(r,g,b) end
	end
end
So, the question is : what can I do to speed the process ?
Is the findEntity function efficient ? Or should I copy/paste

Code: Select all

lumieres_fenetre_minimal_save_state_1.light:setColor(r,g,b)
a hundred times ?

Re: Optimization of a script

Posted: Sat Feb 18, 2017 8:11 pm
by zimberzimber
Don't know if its more efficient, but you can use this:

Code: Select all

local map = Dungeon.getMap([level number])
local r,g,b = [color values...]
for e in map:allEntities() do
 if e.name == [object name] then
  e.light:setColor([color])
 end
end

Re: Optimization of a script

Posted: Sat Feb 18, 2017 9:17 pm
by minmay
vieuxchat wrote:

Code: Select all

lumieres_fenetre_minimal_save_state_1.light:setColor(r,g,b)
This code literally calls findEntity(). It's how the entity id as variable identifier functionality works.

I doubt the culprit is this lua code. I think the culprit is that your level has 100 lights on it in the first place.

Re: Optimization of a script

Posted: Sat Feb 18, 2017 9:41 pm
by vieuxchat
I feared that.
Those 100 lights add a lot to the mood of the level, I'll probably have to reduce the frequency of that script.

But is there a way to reduce the memory footprint of those lights ?

@zimberzimber : I don't understand why your solution would be faster than my script.

Re: Optimization of a script

Posted: Sat Feb 18, 2017 10:08 pm
by zimberzimber
I did say that I'm not sure ¯\_(ツ)_/¯

Re: Optimization of a script

Posted: Sat Feb 18, 2017 10:16 pm
by minmay
vieuxchat wrote:I feared that.
Those 100 lights add a lot to the mood of the level, I'll probably have to reduce the frequency of that script.

But is there a way to reduce the memory footprint of those lights ?
Memory isn't the issue. I'm sure the script isn't the issue, unless you're accidentally running it 1000 times per frame or something (running it once per frame would be fine). You and zimberzimber are underestimating the speed of the LuaJIT interpreter. Have you tested it by simply commenting out the body of the function and comparing the level's performance?

The issue is probably that lights are expensive to render and you have 100 of them (but obviously, I can't be sure without seeing your level, and it depends on the range, shadow casting, shadow map size, etc. of the lights).
vieuxchat wrote:@zimberzimber : I don't understand why your solution would be faster than my script.
It wouldn't.

Re: Optimization of a script

Posted: Sat Feb 18, 2017 11:12 pm
by vieuxchat
The lights are used to illuminate the interior of a castle with "natural" light (the exact light of the day)
So, when it is midnight, the lights are dimmed, and when it's midday they are "high".

I put those lights on the verge of the map (where there's windows). Sometimes, to get the best effect, I put several of them.

Here is the object :

Code: Select all

defineObject{
	name = "lumieres_fenetre_minimal_save_state",
	components = {
		{
			class = "Light",
			offset = vec(0, 2, 0),
			range = 24,
			color = vec(2.0, 2.15, 2.55),
			brightness = 0.2,
			castShadow = true,
			staticShadows = true,
			--fillLight = true,
		},	
	},
	placement = "floor",
	editorIcon = 88,
	tags = { "custom_vieuxchat" },
	minimalSaveState = true,
}

Re: Optimization of a script

Posted: Sat Feb 18, 2017 11:30 pm
by Isaac
If there are any places in your map ~visible, but inaccessible to the player, that show lights, it might improve the speed by impostering those lights with shadeless materials and/or non-opaque blending modes.

Re: Optimization of a script

Posted: Sun Feb 19, 2017 5:28 am
by minmay
vieuxchat wrote:The lights are used to illuminate the interior of a castle with "natural" light (the exact light of the day)
So, when it is midnight, the lights are dimmed, and when it's midday they are "high".

I put those lights on the verge of the map (where there's windows). Sometimes, to get the best effect, I put several of them.

Here is the object :

Code: Select all

defineObject{
	name = "lumieres_fenetre_minimal_save_state",
	components = {
		{
			class = "Light",
			offset = vec(0, 2, 0),
			range = 24,
			color = vec(2.0, 2.15, 2.55),
			brightness = 0.2,
			castShadow = true,
			staticShadows = true,
			--fillLight = true,
		},	
	},
	placement = "floor",
	editorIcon = 88,
	tags = { "custom_vieuxchat" },
	minimalSaveState = true,
}
100 lights with 24m range and full shadow casting is incredibly demanding. This is your problem. Not a chunk of Lua that runs in a few microseconds.
If you're imitating sunlight, why not use a single directional light instead of 100 point lights? You could even still use 100 short range, shadowless lights to produce a soft glow around the windows.

Re: Optimization of a script

Posted: Sun Feb 19, 2017 10:00 am
by vieuxchat
So the range and the shadows are the culprit.
What represents 24 m ingame ? 24 cells ? 12 ? 8 ?

All lights are in reachable places in the castle, so impostering wouldn't give good results.

In fact I'm not sure I really understand what is the difference between a directionnal light and a point one.
Would a directionnal light always follow the direction of the party ?

I thought static shadows = true would be faster than static shadows = false, is it like that or am I wrong ?