Attacker Information

Attacker Information

Discuss game development here, from a distinct game project to an accessible third-party mutator, down to the interaction and design of individual units if you like.

Moderator: Moderators

Post Reply
Von66341
Posts: 111
Joined: 10 Feb 2011, 03:00

Attacker Information

Post 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.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Attacker Information

Post by knorke »

you are missing half the parameters:
UnitDamaged() --> "unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam"
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Attacker Information

Post 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!
Von66341
Posts: 111
Joined: 10 Feb 2011, 03:00

Re: Attacker Information

Post 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?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Attacker Information

Post 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!
Von66341
Posts: 111
Joined: 10 Feb 2011, 03:00

Re: Attacker Information

Post by Von66341 »

It works like magic! Thanks lot! =)
Post Reply

Return to “Game Development”