Page 1 of 1

Attacker Information

Posted: 06 Jun 2011, 18:23
by Von66341
Hey everyone!

I have this portion of the code that write who is being attack in the game.

Code: Select all

function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer)
	local txt = "TakingFire unitTeam ".. unitTeam.. " unit".. unitDefID.. " id"..unitID
	write(txt)
end
Anyone knows if I could find out through this widget which unit is attacking my unit?

Desire outcome will be Unit 123 from Team 0 is attacking Unit 456 from Team 1 at x y z location.
Instead of the current> TakingFire unitTeam 0 unit 123.

Re: Attacker Information

Posted: 06 Jun 2011, 20:46
by knorke
you are missing half the parameters:
UnitDamaged() --> "unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam"

Re: Attacker Information

Posted: 06 Jun 2011, 20:59
by zwzsg
What knorke said. However.... maybe that's gadget only!

As using widgets to know who's attacking through the fog of war could theorically be considered some sort of cheating, it might have been disabled in widgets. Try and see!

Re: Attacker Information

Posted: 07 Jun 2011, 09:53
by Von66341
Thanks knorke and zwzsg!

Yup, I confirm it only works in gadget!

I got this portion of the code in a gadget:

Code: Select all

function gadget:UnitDamaged(unitID,unitDefID,unitTeam,damage,paralyzer,weaponID,attackerID,attattackerDefID,attackerTeam)
local x,y,z=Spring.GetUnitPostion(unitID) 
local txt = "Unit "..unitID.." from Team"..unitTeam.." is being attacked by Unit "..attackerID.." from Team "..attackerTeam.."at"..x..y..z

Spring.SendLuaUIMsg(txt)
end

Thanks! =)
Which sends the information to a widget to write it in a txt.
The portion that contains of the widget that receive the information:

Code: Select all

function widget:RecLuaMsg(txt,playerID) 
write(txt) 
end
My questions here:
1. The information is successfully written in a txt file.
But I randomly I will have this error message:

Code: Select all

LuaRules::RunCallIn:error=2, unitDamged, [string "LuaRules/Gadgets/logger.lua"]:16: attemot to concatenate local 'attackerTeam'(a nil value)
How should I ensure this error is prevented?

Re: Attacker Information

Posted: 07 Jun 2011, 20:52
by FLOZi
you can nil-check the attackerTeam (it can be nil if e.g. a unit is hit by falling debris) or you could do something like

Code: Select all

" from Team ".. (attackerTeam or "no team") .."at".
but that's a bit ugly.

i'd probably just do:

Code: Select all

local txt = "Unit "..unitID.." from Team"..unitTeam.." is being attacked by Unit "..attackerID

if attackerTeam then
  txt .. " from Team "..attackerTeam
end
txt .."at"..x..y..z
of course it might be very sensible to nil-check other arguments too!

Re: Attacker Information

Posted: 08 Jun 2011, 03:53
by Von66341
It works like magic! Thanks lot! =)