[General] Trigger editor and percent sign

Status
Not open for further replies.
Level 9
Joined
Jul 20, 2018
Messages
177
I created a function to cast doubles to integers in Lua. Here is the function:
Lua:
function cast_number(n)
    local s_e_m1, m2 = string.unpack('>I4I4', string.pack('>d', n))  -- two times I4 because WC3 supports only 4-byte integers
    return s_e_m1 >> 31, string.format('%03x', s_e_m1 >> 20 & 0x7ff), string.format('%05x%08x', s_e_m1 & 0xfffff, m2)
end
In Lua console function works as expected. Although, in WC3 the function returns complete garbage. I exported script using the editor and got this:
Lua:
function cast_number(n)
    local s_e_m1, m2 = string.unpack('>I4I4', string.pack('>d', n))
    return s_e_m1 >> 31, string.format('00a', s_e_m1 >> 20 & 0x7ff), string.format('b1c26af000000020', s_e_m1 & 0xfffff, m2)
end
Why my formatted strings got replaced with some garbage?
 
Level 9
Joined
Jul 20, 2018
Messages
177
I read similar threads, there are some problems with % in trigger editor. You need to duplicate it (in string literals at least).

The only special meaning of % connected with tooltips is <rawcode,filedcode,%>. Such "code" in tooltips takes value of field fieldcode from rawcode and multiplies it by 100. But I do not think that these somehow influence on Trigger Editor.
 
Level 20
Joined
Jan 3, 2022
Messages
364
I've literally just encountered this myself. This makes any meaningful usage of string.format in Lua impossible, because it crashes WorldEdit upon map save:
Lua:
function dummy()
    print("%s") -- crashes on save
end
Workaround:
Lua:
function dummy()
    print("\37s") -- works fine
end
 
Level 9
Joined
Jul 20, 2018
Messages
177
@Luashine when you save map, Trigger Editor converts all double percents to a single one. Type inside it print(string.format("%%s", "Hello")) then open terrain editor File -> Export Script -> select where to save the file. Open it, and you will see that all %% are %.

And that's why it is better to use external code editors and some builders. When you add .lua file to map manually, you do not need to double persents, this is required only for code inside Trigger Editor.
 
Status
Not open for further replies.
Top