scp-fs2open / fs2open.github.com

Origin Repository for SCP FreeSpace 2 Open
https://www.hard-light.net/
Other
402 stars 161 forks source link

SwarmInfo does not retrieve swarm count when referenced in Lua #6358

Closed WoolieWool closed 1 week ago

WoolieWool commented 2 weeks ago

I was working on modifications to SCPUI for a future pull request, and found while working on the code to calculate the size of a weapon's volley, that pulling the value from the weapon class' SwarmInfodoes not retrieve any value and thus attempts to perform operations on or concatenate the value will crash. This small script demonstrates the problem. It iterates through tb.WeaponClasses[] one at a time, reporting the name of each weapon with ba.warn() as it goes. When it reaches a swarm weapon, it will use SwarmInfo to set three variables, and print out the second variable, swarmcount, with another ba.warn(). However, the operation to set the variable fails and it will throw an error instead as it will attempt to concatenate a nil value with the surrounding text while composing the warning.

#Conditional Hooks

$Application: FS2_Open

$On Game Init:
[
     function iterateWeapons()
            for j = 1, #tb.WeaponClasses do
                local weapontype = tb.WeaponClasses[j].Name
                ba.warning("Weapon type reported as " .. weapontype)
                if tb.WeaponClasses[j].SwarmInfo then
                    local isSwarmer, swarmcount, swarmwait = tb.WeaponClasses[j].SwarmInfo
                    ba.warning ("Detected swarm weapon " .. weapontype .. " with swarmcount " .. swarmcount)
                end
            end
     end
    iterateWeapons()
]

#End

According to Asteroth in the HLP discord, the issue is caused by the swarm count being a short, which apparently causes a problem when it's being stored into an int variable by the lua system.

Goober5000 commented 1 week ago

Fixed by #6357. On further investigation, the cause was ADE_VIRTVAR not supporting more than one return value.