Page 1 of 2

Fireflies turning on and off

Posted: Wed Jan 07, 2015 7:51 pm
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

Re: Fireflies turning on and off

Posted: Wed Jan 07, 2015 8:13 pm
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

Re: Fireflies turning on and off

Posted: Thu Jan 08, 2015 2:57 pm
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?

Re: Fireflies turning on and off

Posted: Fri Jan 09, 2015 4:41 pm
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

Re: Fireflies turning on and off

Posted: Fri Jan 09, 2015 4:55 pm
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.

Re: Fireflies turning on and off

Posted: Fri Jan 09, 2015 7:05 pm
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...

Re: Fireflies turning on and off

Posted: Sat Jan 10, 2015 9:22 pm
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 ?

Re: Fireflies turning on and off

Posted: Sun Jan 11, 2015 10:50 pm
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.

Re: Fireflies turning on and off

Posted: Sun Jan 11, 2015 11:26 pm
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 ?

Re: Fireflies turning on and off

Posted: Mon Jan 12, 2015 12:09 am
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.