Why your unit does not aim & fire...
Posted: 17 Jan 2021, 16:07
So you have a unit - and it does not fire
You have a weapon configured, you copy pasted it from some - known to work example, from some other game. Keep it simple, keep it clean, modify away from working software in baby-steps.
For this Torturial, i will use a turret weapon with the following unitdef:
And the following weaponDef:
So how do you find out why it does not fire? Lets get this done.
Step1: Check if the Lua - Aiming Calls.
If that one does produced results, your lua code is not returning true at the end. Congratulations you are almost done.
Step 2: Looking for clue through the API aka MorePrintf Debuggery
Print the weaponstable.
One typo might be silently swallowed and your weapon turned into something completely different.
Spring will best effort brows your weapons def, adhering to the Linux Mantra "Fail early, fail loud" only if there are syntactic errors.
Also check on the collissionmodel - for self intersections.
Yellow Dots are Points AimeFrom. Red/Orange Dots are FireEmitors,

Right model is working, left is not working.

Model is still not working

To be continued
You have a weapon configured, you copy pasted it from some - known to work example, from some other game. Keep it simple, keep it clean, modify away from working software in baby-steps.
For this Torturial, i will use a turret weapon with the following unitdef:
Code: Select all
local groundturretmg = Turret:New{
name = "Stationary Machinegun",
Description = "Pillbox Emplacement ",
objectName = "ground_turret_mg.dae",
customParams = {
normaltex = "unittextures/component_atlas_normal.dds",
},
script = "ground_turretscript.lua",
buildPic = "ground_turret_mg.png",
iconType = "ground_turret_mg",
--floater = true,
--cost
buildCostEnergy = 500,
buildCostMetal= 500,
buildTime = 35,
--Health
maxDamage = 500,
idleAutoHeal = 0,
--Movement
fireState=1,
FootprintX = 1,
FootprintZ = 1,
maxSlope = 50,
MaxWaterDepth = 0,
MovementClass = "Default2x2",
nanocolor=[[0.20 0.411 0.611]],
sightDistance = 300,
activateWhenBuilt = true,
cantBeTransported = false,
usepiececollisionvolumes = true,
commandFire = true,
canAttackGround = true,
CanAttack = true,
CanGuard = true,
CanMove = true,
CanPatrol = true,
Canstop = true,
onOffable = false,
LeaveTracks = false,
canCloak =false,
canManualFire = true,
Category = [[GROUND]],
customParams = {
baseclass = "turret"
},
sfxtypes = {
explosiongenerators = {
"custom:bigbulletimpact"
},
},
weapons = {
[1]={name = "machinegun",
onlyTargetCategory = [[GROUND BUILDING]],
turret = true
},
[2]={name = "aamachinegun",
onlyTargetCategory = [[AIR]],
turret = true
},
},
}Code: Select all
local weaponName = "machinegun"
local weaponDef = {
name = "M27-64",
weaponType = [[Cannon]],
--damage
damage = {
default = 4,
HeavyArmor = 1,
},
areaOfEffect = 8,
explosionGenerator = "custom:gunimpact",
cegTag = "gunprojectile",
texture1 = "gunshot",
--physics
weaponVelocity = 850,
reloadtime = 5,
range = 350,
sprayAngle = 300,
tolerance = 8000,
lineOfSight = true,
turret = true,
craterMult = 0,
burst = 15,
burstrate = 0.2,
soundStart = "weapons/machinegun/salvo2.ogg",
soundtrigger = 1,
SweepFire = 1,
interceptor=1,
--apperance
rgbColor = [[0.95 0.5 0.2]],
size = 1.2,
stages = 20,
separation = 0.2,
collideFirebase = false,
avoidFriendly= false,
}
return lowerkeys({ [weaponName] = weaponDef })
Step1: Check if the Lua - Aiming Calls.
Code: Select all
function script.AimWeapon1(Heading, pitch)
Spring.Echo("Aiming weapon 1")
return true
endStep 2: Looking for clue through the API aka MorePrintf Debuggery
Print the weaponstable.
Code: Select all
function printOutWeapon(weaponName)
for name,param in weaponDef:pairs() do
if weaponName == name then
echo(name,param)
end
end
end
Spring will best effort brows your weapons def, adhering to the Linux Mantra "Fail early, fail loud" only if there are syntactic errors.
Code: Select all
function debugAimLoop(weaponID, sleepMS)
restTime = sleepMS or 1
while true do
angleGood, loaded, reloadFrame, salvoLeft, numStockpiled =Spring.GetUnitWeaponState(unitID,weaponID)
if angleGood then
echo("Weapon: Anglegood->"..toString(angleGood).." Loaded->"..toString(loaded).." reloadFrame->"..toString(reloadFrame))
end
px,py,pz, dx,dy,dz =Spring.GetUnitWeaponVectors(unitID,weaponID)
if px then
echo("Weapon: Vector ->", {px,py,pz, dx,dy,dz})
end
commands = Spring.GetUnitCommands(unitID, weaponID)
if commands and type(commands) =="table" and commands[1] and commands[1].id and commands[1].id == CMD.ATTACK then
attackedID = commands[1].params[1]
boolWeaponCanFire = Spring.GetUnitWeaponCanFire(unitID, weaponID)
echo("Units Weapon can fire: "..toString(boolWeaponCanFire))
resultType, tID = Spring.GetUnitWeaponTarget(unitID, weaponID)
if resultType == 1 and uID then
echo("Target is Unit ->".. tID)
end
if attackedID then
bSucces = Spring.GetUnitWeaponHaveFreeLineOfFire(unitID, weaponID)
if bSucces then
echo("Raytrace reaches Goal:"..toString(bSucces))
end
boolTargetInRange = Spring.GetUnitWeaponTestRange(unitID, weaponID, attackedID)
if boolTargetInRange then
echo("Target is in Range: "..toString(boolTargetInRange))
end
end
end
Sleep(restTime)
end
endYellow Dots are Points AimeFrom. Red/Orange Dots are FireEmitors,
Right model is working, left is not working.
Model is still not working
To be continued