Page 1 of 2

FX Translator [SCRIPT]

Posted: Mon Oct 14, 2013 7:07 am
by Diarmuid
Hello fellow community!

A request from pandaFox in the torch thread led me to design a quick script that can translate fxs, lights and particles around.

Video: Image

Script:
SpoilerShow

Code: Select all

--[[
Diarmuid's FX Translator script
V 1.11

put this in a script entity named fxTranslator
put a call to fxTranslator.update() from the onDrawGui.

Call fxTranslator.translateFx(fxId, direction, distance, speed)
- distance is expressed in meters (1 tile = 3 meters)
- speed is expressed in meters/sec (3 speed = 1s per tile. Fireball is 7.5, Lightning Bolt is 10)
- direction can be a facing number (0-3) or a {x, y, z} vector table, each value going from 0-1.

]]

fxTable = {};

lastFrameTime = 0;
dt = 0;

function updateDt()
	local t = getStatistic("play_time");
	dt = t - lastFrameTime;
	lastFrameTime = t;
end

function update()

	updateDt();

	for i = #fxTable, 1, -1 do
		local fxData  = fxTable[i];
		if fxData then
			local fxEntity = findEntity(fxData[1]);
			if fxEntity then
				local translate = dt * 3 / fxData[2];
				fxEntity:translate(fxData[3] * translate, fxData[4] * translate, fxData[5] * translate);
				fxData[7] = fxData[7] + translate;
				if fxData[7] > fxData[6] then
					table.remove(fxTable, i);
				end
			else
				table.remove(fxTable, i);
			end
		end
	end

end

function translateFx(fxId, direction, distance, speed)
	local dx, dy, dz;
	if type(direction) == "table" then
		dx, dy, dz = unpack(direction);
	else
		dx, dz = getForward(direction);
		dy = 0;
		dz = -dz;
	end
	local interval = 1/speed*3;
	fxTable[#fxTable+1] = {fxId, interval, dx, dy, dz, distance, 0};
end
- Setup instructions in the script header
- It is framerate independent, the effect will always move at the correct speed as long as framerate stays above 10fps.
- Don't worry if you don't understand the code, it has been tweaked for performance, not readability. Just spawn your fx and call the translateFx function the script takes care of the rest.

Enjoy!

Re: FX Translator [SCRIPT]

Posted: Mon Oct 14, 2013 11:09 am
by DesperateGames
Hello Diarmuid,

this is brilliant! In needed something like this for my mod and I already tried to experiment with timers and :translate() but your solution works like a charm and wa sjust what I needed! Thank you very much for posting this!

Re: FX Translator [SCRIPT]

Posted: Mon Oct 14, 2013 4:50 pm
by Pandafox
thanks a lot for this !
I will try it as soon as possible ! :P

Re: FX Translator [SCRIPT]

Posted: Mon Oct 14, 2013 6:22 pm
by Pandafox
mmmmm.....
I am trying to use it but I don't really understand how to... (I am not a pure coder :mrgreen: )

Can you explain a little more please ?

I added a LUA script into my level, I copied the script you made.

In my party.lua I added :
onDrawGui = function ()
fxTranslator.update()
print"drawGUI"
end,

But I don't know how to call the function to create a light (torch for exemple..) at the party place....?

Re: FX Translator [SCRIPT]

Posted: Mon Oct 14, 2013 7:01 pm
by Diarmuid
Ok, I've added a quick update to this to v1.1. Now, direction can be either a number 0-3 for facing, or a {x, y, z} vector table.

This allows stuff like vertical fireballs for example:

Image

@ Panda fox:

Here's the code that spawns that fireball, for example, and the moveUp function called from the plate:

Code: Select all

spawn("fx", 1, 24, 13, 1, "fxTest")
		:setParticleSystem("fireball")
		:setLight(1, 0.8, 0.7, 1, 7, 10000, true)
		:translate(0,-7,0);

function moveUp()
	translateFx("fxTest", {0, 1, 0}, 20, 5);
end
I also did a quick test for party movement:
Image

Here i used that to spawn the fireball:

Code: Select all

spawn("fx", 1, 24, 15, 1, "fxTest2")
		:setParticleSystem("fireball_greater")
		:setLight(1, 0.8, 0.7, 1, 7, 10000, true)
		:translate(0,1.5,0);
and that to move it from onMove:

Code: Select all

onMove = function(party, direction)
fxTranslator.translateFx("fxTest2", direction, 3, 3);
end

Re: FX Translator [SCRIPT]

Posted: Mon Oct 14, 2013 7:22 pm
by Isaac
The script gets better and better. 8-)

Re: FX Translator [SCRIPT]

Posted: Mon Oct 14, 2013 9:31 pm
by Pandafox
it works perfectly to make what I wanted to make !!
You are great ! thanks a lot :)

there are some details to fix for the party movement but I will find solutions ;)
Do you know that the distance the party move is 2.9m by step ? :lol:
First I put for "direction" the facing of the party but when backstep or left/right step it don't works !
same problem for walls, if the party walk on a wall, the fx translation move ! :lol:

Re: FX Translator [SCRIPT]

Posted: Sat Oct 19, 2013 1:53 am
by melierax
Bonjour :D
Vu que j'ai beaucoup de difficulté avec la langue :? et aussi que je n'y comprends rien avec le language lua :cry: , est-ce qu'il y aurait une âme charitable (rien qui presse) qui pourrait
me faire un petit donjon avec ces exemples. Je comprends mieux avec des modèles.
J'ai beaucoup de patience (beaucoup de test mais rien qui fonctionne) et si je continue, ca va me prendre une bouteille complète de Tylenol.
Merci

Google translation
Hello: D
Because I have great difficulty with the language? and also that I do not understand the language lua: cry: Is there a kind soul would (no rush) could
give me a small dungeon with these examples. I understand better with models.
I have a lot of patience (lots of test but nothing works) and if I go, it'll take me a full bottle of Tylenol.
Thank you

Re: Possible Bug?

Posted: Sat Oct 19, 2013 1:05 pm
by DesperateGames
Hello Diarmuid,

I added the script to my mod as mentioned above and it worked perfectly. I tested the scene where it is used at least 20 times, without any problems. Now after the mod has been released, I have one user that apparently has encountered a possible bug: The game crashes with the following error message:

Code: Select all

#fxTranslator:31: attempt to index local 'fxData' (a nil value)
stack traceback:
#fxTranslator:31: in function 'update'
mod_assets/scripts/init.lua:74: in function 'onDrawGui'
[string "Party.lua"]: in main chunk
[string "Map.lua"]: in function 'sendMessage'
[string "GameMode.lua"]: in function 'update'
[string "Grimrock.lua"]: in function 'display'
[string "Grimrock.lua"]: in main chunk
Could you imagine a scenario where fxData would be nil? This makes no sense to me, I would assume that he iterates through the fxTable in the update function, and as long as there is something found in the fxTable, fxData should never be nil.....

I will try to "correct" this (if there is anything that needs to be corrected, that is) by changing two checks for nil in the update function:

Code: Select all

function update()

   updateDt();

   for i = 1, #fxTable do
	  local fxData  = fxTable[i];
	  if fxData then
	     if fxData[1] then
	       local fxEntity = findEntity(fxData[1]);
	       if fxEntity then
	         local translate = dt * 3 / fxData[2];
	         fxEntity:translate(fxData[3] * translate, 0, -fxData[4] * translate);
	         fxData[6] = fxData[6] + translate;
	         if fxData[6] > fxData[5] then
	            table.remove(fxTable, i);
	            i = i - 1;
	         end
	       else
	         table.remove(fxTable, i);
	         i = i - 1;
	      end
		end
	  end
   end
end
I am not sure that this is required but it might help others that encounter the same problem.

Re: FX Translator [SCRIPT]

Posted: Sat Oct 19, 2013 2:04 pm
by Diarmuid
I guess that should do it... I haven't tested in depth, but I guess it has to do with the fact I'm removing elements from the table while iterating and "rewinding" the for loop with i = i - 1;(Which isn't recommended, usually, I should have been wary). Maybe it got back to an element that was removed somehow. But iterating backwards should maybe fix the problem: I've uploaded a fixed version 1.11 above.

@Melierax: Je suis pas mal occupé ces temps-ci, si j'ai deux secondes je te préparerai un exemple.