Add to list: lists made there are lists, the result's text is the item, Allow repeats

Making a new value from an Add to list row made a number, which the list
picker then hid and which couldn't hold words. Now it makes a list. The
item starts as the result's own text. List cards get Allow repeats (add
every time, in order); off keeps adding only new items.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
RobinFiredrake 2026-11-14 13:40:20 -06:00
parent 9a0d97e924
commit 3aeb98c2fd
13 changed files with 83 additions and 13 deletions

View file

@ -20,17 +20,17 @@ const OPS := ["+=", "-=", "*=", "/=", "="]
## A comma-separated list with `item` added at the end (unless it's there
## already, ignoring capitals) or, with `remove`, taken out. Returns the new
## list text. Used by += / -= on lists and by saving a result into a list
## value (RollContext.save_result()).
static func list_with(list: Variant, item: String, remove: bool = false) -> String:
## already, ignoring capitals; with `repeats`, every time) or, with `remove`,
## taken out (the first match). Returns the new list text. Used by += / -= on
## lists and by saving a result into a list value (RollContext.save_result()).
static func list_with(list: Variant, item: String, remove: bool = false, repeats: bool = false) -> String:
var items := Expr.list_items(list if list != null else "")
var at := -1
for k in items.size():
if items[k].to_lower() == item.to_lower():
at = k
break
if not remove and at < 0 and item != "":
if not remove and (at < 0 or repeats) and item != "":
items.append(item)
elif remove and at >= 0:
items.remove_at(at)
@ -230,7 +230,7 @@ static func _assign(st: Dictionary, ctx: RollContext, source_id: String, facts:
"+=", "-=":
if as_list:
var item := Expr.format_value(rhs).strip_edges()
value = list_with(cur, item, op == "-=")
value = list_with(cur, item, op == "-=", ctx.declared(target) != null and ctx.declared(target).list_repeats)
line = "%s %s %s → %s" % [label, "+" if op == "+=" else "−", item, _shown(value)]
else:
var a: Variant = Expr.to_value(cur) if cur != null and str(cur).strip_edges() != "" else 0

View file

@ -128,7 +128,7 @@ func save_result(name: String, value: Variant, source_id: String = "") -> void:
return
var list: Variant = get_value(n)
for item in Expr.list_items(Formatter.strip_tags(Expr.format_value(value))):
list = Effects.list_with(list, item)
list = Effects.list_with(list, item, false, card.list_repeats)
save(n, list if list != null else "", source_id)

View file

@ -120,6 +120,9 @@ var var_type: String = "number"
var initial: String = ""
## Scale variables: the ordered steps, lowest first.
var steps: Array[String] = []
## List cards: true when the list keeps repeats (an item added every time, in
## order); false adds an item only if it isn't there yet.
var list_repeats := false
## Ledger cards: the entries, [{name, start}] (LedgerMembers).
var members: Array = []
## Check cards: the dice, the modifier and the target (expressions) and the

View file

@ -72,6 +72,8 @@ static func to_dict(t: RollTable) -> Dictionary:
d["initial"] = t.initial
if t.var_type == RollTable.VAR_SCALE and not t.steps.is_empty():
d["steps"] = t.steps.duplicate()
if t.var_type == "list" and t.list_repeats:
d["repeats"] = true
if t.is_collection() and not t.members.is_empty():
d["members"] = t.members.duplicate(true)
elif t.kind == RollTable.KIND_CHECK:
@ -138,6 +140,7 @@ static func from_dict(d: Dictionary) -> RollTable:
t.initial = RollTable.expr_text(d.get("initial", ""), "")
t.steps = Scales.clean(d.get("steps", []))
t.members = LedgerMembers.clean(d.get("members", []))
t.list_repeats = bool(d.get("repeats", false))
t.check_dice = RollTable.expr_text(d.get("check_dice", RollTable.CHECK_DEFAULT_DICE), RollTable.CHECK_DEFAULT_DICE).strip_edges()
if t.check_dice == "":
t.check_dice = RollTable.CHECK_DEFAULT_DICE

View file

@ -401,7 +401,7 @@ follow.
```
@str = 2d6 set
@terms += 1 add (number) · append ", item" if not present (text/list)
@terms += 1 add (number) · append ", item" if not present (text/list; a list card with `"repeats": true` appends every time)
@credits -= 1000 subtract (number) · remove the item (text/list)
@age *= 2 multiply (/= divides, rounding down)
@skill_pilot += 1 unset counts as 0, so this gives Pilot 1 or raises it

View file

@ -126,7 +126,7 @@ An option can have [b]Show only if[/b], so it's offered only when a condition is
# Values and changes
A spread can remember [b]values[/b] between rolls, such as Luck, Gold or the current town. In rules a value is written with @: `@luck`.
- Any result can have [b]changes[/b] that happen when it comes up: [b]Add 1 to Luck[/b], [b]Subtract 3 from Gold[/b], [b]Set Town to …[/b]. Edit them in the inspector as rows, or with Edit as text.
- Any result can have [b]changes[/b] that happen when it comes up: [b]Add 1 to Luck[/b], [b]Subtract 3 from Gold[/b], [b]Set Town to …[/b], [b]Add to list[/b] (the result's own text, unless you type another item). Edit them in the inspector as rows, or with Edit as text.
- A [b]Variable[/b] card gives a value a name, a type and a starting value, and shows what it is now. The sample's [b]Luck[/b] card starts at 0.
- A [b]Ledger[/b] card groups related values, such as Skills, so they can be listed and picked from together. In Build mode, add and rename its entries right on the card; each entry's number is where it starts.
- The tray's [b]Variables[/b] tab shows every value, and lets you clear or reset them. [b]Reset all…[/b] puts every value back to its start.
@ -134,7 +134,7 @@ A spread can remember [b]values[/b] between rolls, such as Luck, Gold or the cur
## Writing changes as text
- `@luck += 1` adds, `@luck -= 1` subtracts and `@gold = 2d6` sets.
- `reset @luck` goes back to the starting value. `reset @skill_*` resets every value starting with skill_.
- Text values work as lists: `@traits += "Brave"` adds an item.
- Text values work as lists: `@traits += "Brave"` adds an item, and `-=` takes one out. A [b]List[/b] variable adds an item only if it isn't there yet, unless you tick [b]Allow repeats[/b]; then it adds it every time, in order.
## Conditions
Conditions are used by Only if threads, branches, repeats and Show only if. In plain words they read "Luck is at least 2". As text: `@luck >= 2`, `@town = "Saltmere"`, `@gold > 0 and @luck < 3`.

View file

@ -129,7 +129,7 @@ static func _canon_table(td: Dictionary) -> Dictionary:
for e in src_entries:
entries.append(_canon_entry(e))
t["entries"] = entries
_keep(td, t, ["color", "w", "list_h", "roll_floor", "roll_ceiling", "deck_empty_text", "columns", "column_rolls", "steps", "members"])
_keep(td, t, ["color", "w", "list_h", "roll_floor", "roll_ceiling", "deck_empty_text", "columns", "column_rolls", "steps", "members", "repeats"])
# A separator may be just a space, so it isn't trimmed.
if td.get("column_join") is String and td["column_join"] != "":
t["column_join"] = td["column_join"]

49
tests/test_lists.gd Normal file
View file

@ -0,0 +1,49 @@
extends TestBase
## List values: the "Add to list" change row (picking a list, the result's own
## text as the item, a new value made there is a list), and "Allow repeats".
## Run: godot --headless --path . -s res://tests/test_lists.gd -- --sample
func _init() -> void:
if not await start_main():
return
main.set_mode(Main.Mode.BUILD)
main.data.load_dict({"format_version": 2, "id": "list-test", "name": "List test", "tables": [
{"id": "groc", "name": "Groceries", "kind": "variable", "var_name": "groceries", "var_type": "list", "x": 40, "y": 40, "entries": []},
{"id": "shop", "name": "Shop", "x": 340, "y": 40, "entries": [{"id": "e1", "text": "[b]Eggs[/b]"}]},
]})
main.persistent_vars.clear()
main._rebuild_cards()
await _frames(4)
await _test_add_to_list_row()
_test_repeats()
finish()
func _test_add_to_list_row() -> void:
main.select_result("shop", "e1")
await _frames(2)
var fx := main.inspector.effects
fx.add_row()
fx.set_row_op(0, "append")
_eq(fx.rows[0]["amount"], "Eggs", "Add to list offers the result's own text")
fx.set_row_var(0, "@groceries")
_eq(main.data.get_table("shop").find_entry("e1").effects, "@groceries += \"Eggs\"", "and writes it as the item")
var picker := ValuePicker.new(main)
picker.kinds = ["list", "text"]
var made := picker.create_value("Inventory", true)
var card := main.variable_card(made.trim_prefix("@"))
_check(card != null and card.var_type == "list", "a new value made from a list picker is a list")
picker.free()
func _test_repeats() -> void:
_eq(Effects.list_with("Eggs", "eggs"), "Eggs", "without repeats an item already there isn't added again")
_eq(Effects.list_with("Eggs", "Eggs", false, true), "Eggs, Eggs", "with repeats it's added every time, in order")
_eq(Effects.list_with("Eggs, Milk, Eggs", "Eggs", true), "Milk, Eggs", "removing takes out the first match")
var groc := main.data.get_table("groc")
groc.list_repeats = true
_check(RollTable.from_dict(groc.to_dict()).list_repeats, "Allow repeats is saved with the card")
var ctx := Roller.new_context(main.data, main.rng, {}, {})
Effects.run("@groceries += \"Eggs\"\n@groceries += \"Eggs\"", ctx, "shop")
_eq(ctx.get_value("@groceries"), "Eggs, Eggs", "a list that allows repeats collects each one")

1
tests/test_lists.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://dct2cpq3ic6dx

View file

@ -304,6 +304,14 @@ static func _variable(insp: Inspector, box: VBoxContainer, t: RollTable) -> void
InspectorScale.build(insp, box, t, err)
else:
box.add_child(InspectorKit.field("Starting value", setting_line(insp, "initial", t.initial, "0" if t.var_type == "number" else "nothing", err)))
if t.var_type == "list":
var repeats := InspectorKit.check_box("Allow repeats", t.list_repeats)
repeats.tooltip_text = "On: an item is added every time, in order. Off: only if it isn't on the list yet."
InspectorKit.tag(repeats, "card.repeats")
repeats.toggled.connect(func(on: bool) -> void:
t.list_repeats = on
insp.wrote(t))
box.add_child(repeats)
box.add_child(err)

View file

@ -20,6 +20,8 @@ var rows: Array[Dictionary] = []
var text_mode := false
var _raw := ""
var _key := "fx"
## The result's own text: what "Add to list" adds unless you type something else.
var item_hint := ""
func setup(p_main: Main, text: String, p_table: RollTable, as_text: bool, key: String = "fx") -> void:
@ -114,7 +116,8 @@ func set_row_op(i: int, op: String) -> void:
if op == "reset":
m["amount"] = ""
elif listy and not was_listy:
m["amount"] = Builder.unquote(amt)
# A fresh row's "1" isn't an item anyone means: offer the result itself.
m["amount"] = item_hint if item_hint != "" and (amt == "" or amt == "1") else Builder.unquote(amt)
elif not listy and was_listy:
m["amount"] = InspectorKit.amount_expr(amt, _known_names()) if amt != "" else ""
elif amt == "" and was == "reset":

View file

@ -94,6 +94,7 @@ static func section(insp: Inspector, t: RollTable, e: TableEntry) -> void:
insp._content.add_child(InspectorCard.case_block(insp, t, e))
insp.effects = EffectsBuilder.new()
insp.effects.item_hint = Formatter.strip_tags(e.text).strip_edges()
insp.effects.setup(insp.main, e.effects, t, not plain, "res.fx")
insp.effects.changed.connect(insp.set_entry_effects)
insp._content.add_child(InspectorKit.field("When this comes up", insp.effects))

View file

@ -309,7 +309,9 @@ func _ask_new(initial: String) -> void:
func create_value(label_text: String, on_board: bool) -> String:
var nm := JsonIO.slug(label_text.strip_edges(), "value")
if on_board and main:
var t := main.add_variable_card(label_text.strip_edges(), nm)
# A value made from a picker that only takes lists (Add to list) is a list.
var type := str(kinds[0]) if not kinds.is_empty() and RollTable.VAR_TYPES.has(str(kinds[0])) else "number"
var t := main.add_variable_card(label_text.strip_edges(), nm, type)
nm = t.var_name
var full := "@" + nm
set_value(full)