Skip to content

Dictionary ids: explicit vs implicit #59

@exectails

Description

@exectails

Here's the current function for Equipment Merchant Dunkel in Klaipeda:

-- Equipment Merchant Dunkel
function npc_equipmentmerchantdunkel()
    local selection,i = nselect("KLAPEDA_Akalabeth_basic28", "weapon:@dicID_^*$ETC_20150317_004443$*^", "armor:@dicID_^*$ETC_20150317_004444$*^", "!@#$Auto_JongLyo#@!")

    if selection == "weapon" then
        openshop("Klapeda_Weapon")
    elseif selection == "armor" then
        openshop("Klapeda_Armor")
    end
end

Generally I like what I'm seeing, nselect, the if/else, all looks fine. However, there's the dicIDs. For the client to recognize the id and convert it to a string from the localization dictionary this code is necessary: @dicID_^*$<id>$*^

This is the case for all dictionary ids, but for NPC names we hide that code, the key is wrapped in it behind the scenes.

addnpc(20111, "ETC_20150317_009196", "c_Klaipe", 394, -1, -475, 45, "npc_equipmentmerchantdunkel")

ETC_20150317_009196 becomes @dicID_^*$ETC_20150317_009196$*^ when sent to the client.

This is a lot of boilerplate that's hard to remember and basically not necessary. We can wrap the options of a select as well. The question is, should we actually hide that completely?

The dictionary ids aren't the only codes, as you can see above. !@#$Auto_JongLyo#@! is the code for a localized End or Cancel. (Since it's not a dictionary id I assume Auto does more than that.)

Hiding the wrap could result in confusing situations, unless you know every function and its implementation. You can't really be sure which functions convert the strings and which don't, does it convert every id in the string or only the entire string if it looks like an id, is there a shortcut for the other codes as well or not?

An alternative could be to add helper functions in a global script. For example:

local selection = select("Foobar?", dict("ETC_20150317_004443"), dict("ETC_20150317_004444"), endbtn())

This way it's much easier to remember, the implementation and what happens can be looked up without checking the core, and there's no ambiguity. One downside is that you have to concatenate strings if you need more than just the id.

local selection = nselect("Foobar?", "weapon:" .. dict("ETC_20150317_004443"), "armor:" .. dict("ETC_20150317_004444"), endbtn())

Or, there needs to be another help function.

local selection = nselect("Foobar?", ndict("weapon", "ETC_20150317_004443"), ndict("armor", "ETC_20150317_004444"), endbtn())

The advantages of this are explicitness and that it's less likely to make mistakes with all the codes. I can't really think of any actual disadvantages right now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions