I was trying to use this function in our shiny new Lua script.
Lua:
print(BlzGetAbilityTooltip('AHfs', 1))
It gave no output and crashed the rest of the calling function.
I found these lines in the common.j file. The AbilityId function always gives 0, whatever the input.
vJASS:
// Not currently working correctly...
constant native AbilityId takes string abilityIdString returns integer
constant native AbilityId2String takes integer abilityId returns string
So I decided to do it myself. I thought this would work, but it just gives me "Tooltip missing!"
Lua:
function GetAbilityID (code)
local length = #code
if length == 4 then
local id = 0
for i = 1, length do
id = id + (256 ^ (length - i)) * code:byte(i)
end
return id | 0
else
return 0, 'Error: ' .. code .. ' is not an ability!'
end
end
print(BlzGetAbilityTooltip(GetAbilityID('AHfs'), 1))
After some debugging, I found the issue. In the standalone Lua interpreter, the function call gives the expected value 1095263859. But in game, it gives 1095263744.
Lua:
print(GetAbilityID('AHfs'))
I suspect the error is related to the floating point numbers. It is. The following version works correctly.
Lua:
function GetAbilityID (code)
local length = #code
if length == 4 then
local id = 0
for i = 1, length do
-- Must convert floats to integers, or there will be severe rounding errors
id = id + (256 ^ (length - i) | 0) * code:byte(i)
end
return id
else
return 0, 'Error: ' .. code .. ' is not an ability!'
end
end
My conclusion:
- Automatic conversion from ability ID string to integer does not work in Lua scripts
- There are some subtle bugs in the new Lua VM related to floating point numbers
- Integer and Real types are not indistinguishable even in Lua scripts