Skip to content

Instantly share code, notes, and snippets.

local parent = {}
parent[1] = {}
mutate_table(parent, function(v)
v.prop = "asdf"
end)
parent[1].prop -- has been set to "asdf"
local function dangerous_mutate_table(t, f)
for i, v in ipairs(t) do t[i] = f(v, i) end
end
parent = {}
parent[1] = {}
dangerous_mutate_table(parent, function(v)
v.prop = "asdf"
-- oops, the programmer forgot to return anything here
end)
local weathers = {"clear", "drought", "rain", "snowfall"}
local roll = wesnoth.random(4)
weather_id = weathers[roll]
function weighted_random(odds)
local target = wesnoth.random()
local sum = 0.0
local i = 1
sum += odds[1]
while sum <= target and i <= #odds do
sum += odds[i]
i += 1
end
return i
local weather_names = {"clear", "snowfall"}
local weather_probabilities = {5, 1}
local function good(t)
table.sort(t) -- OK, does what you expect
end
local function bad(table)
table.sort(table) -- error, the table doesn't have a field called "sort"
end
local function ordered_map_of_keys(t)
local ordered_map = {}
for k, v in pairs(t)
table.insert(ordered_map, k)
end
table.sort(ordered_map)
return ordered_map
end
From 5d8b4f7987c8fca0e042021a61ce5c8086da3544 Mon Sep 17 00:00:00 2001
From: Jyrki Vesterinen <[email protected]>
Date: Sat, 7 Jul 2018 09:50:49 +0300
Subject: [PATCH] Experimental fixes for the crash when a modeless dialog
destroys itself
This commit fixes the crashes, but I'm not convinced that one of the fixes
is correct.
Setting `handled` and `halt` to true in
local function split_to_array(s)
local a = {}
for m in utils.split(s) do table.insert(a, m) end
return a
end
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff2a922f1 in __GI_abort () at abort.c:79
#2 0x00007ffff2cbe943 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff2cc4896 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff2cc48d1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff2cc4b04 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff2cc082b in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00000000004a41fb in std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, chatroom_log, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, chatroom_log> > >::at (
this=0x1886c68 <default_chat_log[abi:cxx11]>, __k=...)