Optimization of a script

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Optimization of a script

Post 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 ?
User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Optimization of a script

Post 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
My asset pack [v1.10]
Features a bit of everything! :D
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Optimization of a script

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Re: Optimization of a script

Post 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.
User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Optimization of a script

Post by zimberzimber »

I did say that I'm not sure ¯\_(ツ)_/¯
My asset pack [v1.10]
Features a bit of everything! :D
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Optimization of a script

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Re: Optimization of a script

Post 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,
}
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Optimization of a script

Post 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.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Optimization of a script

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Re: Optimization of a script

Post 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 ?
Post Reply