Remote Droid that detonates on impact
Posted: Wed Apr 14, 2021 1:44 pm
As the title says I am trying to create a remote droid that detonates upon impact with an enemy AI. Unfortunately ExplosionImpact doesn't work on Droid classes and I couldn't figure out any other way, so I tried to achieve it with Lua scripting. That's the code I created:
Explanation
Once the player uses the remote droid, a timer is started. if the remote droid is alive, the function fetches its X,Y,Z coordinates and the X,Y,Z coordinates of every enemy AI. Then it checks for every enemy if the distance between the remote droid and that enemy is below a certain number. If this is true, the remote droid explodes. If this is false, the timer starts anew.
Problem
Everything works fine (as far as I can monitor it with the print commands) up until the point where the remote droid is near an enemy. The math statement is not being executed and I don't get a print entry in the BF2Log. And once the droid gets destroyed through time or an enemy shot, the game crashes.
Does somebody know how to prevent the CTD and make sure the math statement is being checked/executed correctly?
Code: Select all
nikitaUnit = nil
print("nikitaUnit:", nikitaUnit)
CreateTimer("NikitaPosUpdate")
SetTimerValue("NikitaPosUpdate", 1)
OnCharacterDispenseControllable(
function(player, controllable)
if GetEntityClass(controllable) == FindEntityClass("rep_weap_inf_remotedroid_ord") then
nikitaUnit = controllable
print("nikitaUnit:", nikitaUnit)
StartTimer("NikitaPosUpdate")
end
end
)
OnTimerElapse(
function(timer)
if nikitaUnit then
print("nikitaUnit:", nikitaUnit)
nikitaX,nikitaY,nikitaZ = GetWorldPosition(nikitaUnit)
print("Nikita X: ", nikitaX)
print("Nikita Y: ", nikitaY)
print("Nikita Z: ", nikitaZ)
local teamSize = GetTeamSize(2)
for i = 0, teamSize - 1 do
enemyIndex = GetTeamMember(2,i)
print("Getting Position of Enemy ", enemyIndex)
enemyX,enemyY,enemyZ = GetWorldPosition(GetCharacterUnit(enemyIndex))
print("Enemy X: ", enemyX)
print("Enemy Y: ", enemyY)
print("Enemy Z: ", enemyZ)
if (math.abs(enemyX - nikitaX) <= 32) and (math.abs(enemyY - nikitaZ) <= 32) then
print("Collide with Enemy ", enemyIndex)
KillObject(nikitaUnit)
end
end
end
SetTimerValue("NikitaPosUpdate", 1)
StartTimer("NikitaPosUpdate")
end,
"NikitaPosUpdate"
)
Once the player uses the remote droid, a timer is started. if the remote droid is alive, the function fetches its X,Y,Z coordinates and the X,Y,Z coordinates of every enemy AI. Then it checks for every enemy if the distance between the remote droid and that enemy is below a certain number. If this is true, the remote droid explodes. If this is false, the timer starts anew.
Problem
Everything works fine (as far as I can monitor it with the print commands) up until the point where the remote droid is near an enemy. The math statement is not being executed and I don't get a print entry in the BF2Log. And once the droid gets destroyed through time or an enemy shot, the game crashes.
Does somebody know how to prevent the CTD and make sure the math statement is being checked/executed correctly?