Fireflies turning on and off

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!
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Fireflies turning on and off

Post by Granamir »

Hi guys
i'm new here, and almost new in programming (absolutely new in Lua).
I have a problem with a script to make fireflies appear only during night. I started from skugg code and tried to make something suited me more.
Here is the code:

function lucciole()
-- in 10 passage
tempoInizio = 1.02 -- starting time of the turning lights on
tempoFine = 1.98 -- end time of turninglights of
lucePartenza = 4 -- light brightness when fully lightened (4 is standard)
numeroSciami = 1 -- how much fireflies swarms i have in map

-- turning on
if GameMode.getTimeOfDay() >= tempoInizio and GameMode.getTimeOfDay() < tempoInizio+0.01 then
for sciami=1,numeroSciami do
local nomeSciame="forest_fireflies_"..tostring(sciami)
if nomeSciame~=nil then
nomeSciame.particle:enable()
nomeSciame.light:setBrightness(lucePartenza-3.2)
else end
end
elseif
//
missing code, here i cut to be shorter, basically is the same repeated for 10 times increasing time and brightness nunmber
//
-- lightened during night
elseif GameMode.getTimeOfDay() >= tempoInizio+0.10 and GameMode.getTimeOfDay() < tempoFine-0.10 then
for sciami=1,numeroSciami do
local nomeSciame="forest_fireflies_"..tostring(sciami)
if nomeSciame~=nil then
nomeSciame.particle:enable()
nomeSciame.light:setBrightness(lucePartenza)
else end
end

-- turning off
elseif GameMode.getTimeOfDay() >= tempoFine-0.10 and GameMode.getTimeOfDay() < tempoFine-0.9 then
for sciami=1,numeroSciami do
local nomeSciame="forest_fireflies_"..tostring(sciami)
if nomeSciame~=nil then
nomeSciame.particle:enable()
nomeSciame.light:setBrightness(lucePartenza)
else end
end
elseif
//
missing code, here i cut to be shorter, basically is the same repeated for 10 times decreasing time and increasing brightness nunmber
//

-- fireflies during day, not visible
else
for sciami=1,numeroSciami do
local nomeSciame="forest_fireflies_"..tostring(sciami)
if nomeSciame~=nil then
nomeSciame.particle:disable()
nomeSciame.light:disable()
else end
end
end
end

but i have an issue, i guess with nomeSciame in nomeSciame.particle:anable()
i receive: attempt to index field 'particle' (a nil value)
can't imagine why i receive this error, i tryed to hudprint nomeSciame and it worked fine

can someone help me? ty

-----

Here u can find my last code:
https://onedrive.live.com/redir?resid=D ... 900F%21181
Last edited by Granamir on Tue Jan 13, 2015 10:20 pm, edited 1 time in total.
User avatar
Drakkan
Posts: 1318
Joined: Mon Dec 31, 2012 12:25 am

Re: Fireflies turning on and off

Post by Drakkan »

Hi,

Personaly I think much better solution then just to disable / enable is to slowly fade it on / off, which looks much better ingmae. check following thread

viewtopic.php?f=22&t=8486&p=86071&hilit ... ies#p86071
Breath from the unpromising waters.
Eye of the Atlantis
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: Fireflies turning on and off

Post by Granamir »

Ty
i'm not a programmer so i'm trying to figure out how those things works, but yes from video seems cool.
...btw just to learn something, does anyone can figure out why i have that error message in my code?
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: Fireflies turning on and off

Post by Granamir »

Ok i'v used fade in and out and it works fine.
Btw i tried again to make a code more easy to use and manage more fireflyes, but i have the same problem i posted at begining. When i use a variable for fireflyes name to make something i receive nil value error.
Someone has a clue for me?
ty
User avatar
Phitt
Posts: 442
Joined: Tue Aug 14, 2012 9:43 am

Re: Fireflies turning on and off

Post by Phitt »

I did it like that:

Code: Select all

local i = 1;
local entity = findEntity("forest_fireflies_" .. i);

	while entity ~= nil do
	
		if entity.light ~= nil then
		local rand = math.random(3,10)
		entity.light:fadeIn(rand)
		entity.particle:fadeIn(rand)
		end
		
	i = i + 1;
	entity = findEntity("forest_fireflies_" .. i);
Add this code during nightfall (getTimeOfDay > 1) and the same thing, only with fadeOut instead of fadeIn when the night is over (like getTimeOfDay > 1.95). Then you can simply place the standard fireflies (forest_fireflies) and the script will recognize them automatically.
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: Fireflies turning on and off

Post by Granamir »

Thank you very much
I'v tried and it works great.
The random touch is a masterpiece, i really like it...now i have to think out of a map with a field full of fireflyes...
User avatar
Drakkan
Posts: 1318
Joined: Mon Dec 31, 2012 12:25 am

Re: Fireflies turning on and off

Post by Drakkan »

can you please share whole script ? I´d like to have this in my dungeon :)
Also what is the best way how to trigger it ? Some timer set for it ? or the script is somehow updating fireflies automaticaly ?
Breath from the unpromising waters.
Eye of the Atlantis
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: Fireflies turning on and off

Post by Granamir »

I'm working on an imroved version of firefliys.
Btw code i'm using now in my dungeon (...don't know if i can call it dungeon since it's outside for now...) is:

Code: Select all

function lucciole()
	local oraDelGiorno=GameMode.getTimeOfDay()
	local tempoInizio = 1.02 -- starting time of fade in
	local tempoFine = 1.98 -- endingi time of fade out
	
	-- showing during day
	if oraDelGiorno >= tempoInizio and oraDelGiorno < tempoFine then
		local contatoreSciami = 1
		local sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		while sciame ~= nil do
			if sciame.light ~= nil then
				local rand = math.random(3,10)
				sciame.light:fadeIn(rand)
				sciame.particle:fadeIn(rand)
			end
			contatoreSciami = contatoreSciami + 1
			sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		end
		
	-- hiding during night
	else 
		local contatoreSciami = 1
		local sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		while sciame ~= nil do
			if sciame.light ~= nil then
				local rand = math.random(3,10)
				sciame.light:fadeOut(rand)
				sciame.particle:fadeOut(rand)
			end
			contatoreSciami = contatoreSciami + 1
			sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		end
	end
end
I just put a script_entity in the map with lua file (containing the script above) as external source.
If someone has question feel free to ask, or if u have some better code feel free to share.
User avatar
Drakkan
Posts: 1318
Joined: Mon Dec 31, 2012 12:25 am

Re: Fireflies turning on and off

Post by Drakkan »

Granamir wrote:I'm working on an imroved version of firefliys.
Btw code i'm using now in my dungeon (...don't know if i can call it dungeon since it's outside for now...) is:

Code: Select all

function lucciole()
	local oraDelGiorno=GameMode.getTimeOfDay()
	local tempoInizio = 1.02 -- starting time of fade in
	local tempoFine = 1.98 -- endingi time of fade out
	
	-- showing during day
	if oraDelGiorno >= tempoInizio and oraDelGiorno < tempoFine then
		local contatoreSciami = 1
		local sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		while sciame ~= nil do
			if sciame.light ~= nil then
				local rand = math.random(3,10)
				sciame.light:fadeIn(rand)
				sciame.particle:fadeIn(rand)
			end
			contatoreSciami = contatoreSciami + 1
			sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		end
		
	-- hiding during night
	else 
		local contatoreSciami = 1
		local sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		while sciame ~= nil do
			if sciame.light ~= nil then
				local rand = math.random(3,10)
				sciame.light:fadeOut(rand)
				sciame.particle:fadeOut(rand)
			end
			contatoreSciami = contatoreSciami + 1
			sciame = findEntity("forest_fireflies_" .. contatoreSciami)
		end
	end
end
I just put a script_entity in the map with lua file (containing the script above) as external source.
If someone has question feel free to ask, or if u have some better code feel free to share.
thanks a lot !
how are you triggering the script ? I have for now timer which is running every second and checking script condition. not sure if there is some better solution ?
Breath from the unpromising waters.
Eye of the Atlantis
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: Fireflies turning on and off

Post by Granamir »

Yea sorry i forgot (just saw it) i also have a timer connected to script, calling fireflies function onActivate. 1 second time interval.
Post Reply