What´s wrong with this character designer?

Post Reply
User avatar
Drachenbauer32
Member
Posts: 105
Joined: Wed Aug 28, 2019 18:11

What´s wrong with this character designer?

by Drachenbauer32 » Post

Hello

i modifyed a character-creator:

first lua-file

Code: Select all

character_creator = {}
character_creator.skins = dofile(minetest.get_modpath("character_creator") .. "/skins.lua")

local skinsdb
if minetest.get_modpath("skinsdb") and minetest.global_exists("skins") then
    skinsdb = skins
end

local skin_default = {
    gender = "Male",
    height = 4,
    width = 4,
    skin = "skinwhite.png",
    eyes = "eyeswhite.png^[multiply:#3f3f3f^eyesoverlay.png",
    hair = "hairwhite.png^[multiply:#3f3f3f^hairoverlay.png",
    shirt = "shirtwhite.png^[multiply:#ff0000",
    pants = "pantswhite.png^[multiply:#0000ff",
}

local skins = character_creator.skins
local skins_array = {}

minetest.after(0, function()
    local function associative_to_array(associative)
        local array = {}
        for key in pairs(associative) do
            table.insert(array, key)
        end
        return array
    end

    skins_array = {
        skin  = associative_to_array(skins.skin),
        hair  = associative_to_array(skins.hair),
        eyes  = associative_to_array(skins.eyes),
        shirt = associative_to_array(skins.shirt),
        pants = associative_to_array(skins.pants),
    }
end)

-- Saved skins_array indexes in this
local skin_indexes = {}

local function show_formspec(player)
    local indexes = skin_indexes[player]

    local formspec = "size[8,7.5]"

            .. default.gui_bg
            .. default.gui_bg_img
            .. ""

            -- Skin

            .. "image_button[3.5,0.5;1,1;" .. skins_array.skin[indexes.skin] .. ";skin;]"
            .. "image_button[1.5,0.5;1,1;gauche.png;skin_back;]"
            .. "image_button[5.5,0.5;1,1;droite.png;skin_next;]"

            -- Hair

            .. "image_button[3.5,1.5;1,1;" .. skins_array.hair[indexes.hair] .. ";hair;]"
            .. "image_button[1.5,1.5;1,1;gauche.png;hair_back;]"
            .. "image_button[5.5,1.5;1,1;droite.png;hair_next;]"

            -- Eyes

            .. "image_button[3.5,2.5;1,1;" .. skins_array.eyes[indexes.eyes] .. ";eyes;]"
            .. "image_button[1.5,2.5;1,1;gauche.png;eyes_back;]"
            .. "image_button[5.5,2.5;1,1;droite.png;eyes_next;]"

            -- Shirt

            .. "image_button[3.5,3.5;1,1;" .. skins_array.shirt[indexes.shirt] .. ";shirt;]"
            .. "image_button[1.5,3.5;1,1;gauche.png;shirt_back;]"
            .. "image_button[5.5,3.5;1,1;droite.png;shirt_next;]"

            -- Pants

            .. "image_button[3.5,4.5;1,1;" .. skins_array.pants[indexes.pants] .. ";pants;]"
            .. "image_button[1.5,4.5;1,1;gauche.png;pants_back;]"
            .. "image_button[5.5,4.5;1,1;droite.png;pants_next;]"

            -- Done

            .. "image_button_exit[1.0,6.5;2,1;;main;Back to Game]"

    minetest.show_formspec(player:get_player_name(), "character_creator", formspec)
end

local function load_skin(player)
    skin_indexes[player] = {}

    player:set_attribute("character_creator:gender", skin_default.gender)
    player:set_attribute("character_creator:width", skin_default.width)
    player:set_attribute("character_creator:height", skin_default.height)

    local function load_data(data_name)

        local key = player:get_attribute("character_creator:" .. data_name)
        local index = table.indexof(skins_array[data_name], key)
        if index == -1 then
            index = table.indexof(skins_array[data_name], skin_default[data_name])
        end

        local indexes = skin_indexes[player]
        indexes[data_name] = index
    end

    load_data("skin")
    load_data("hair")
    load_data("eyes")
    load_data("shirt")
    load_data("pants")

end

local function save_skin(player)
    local function save_data(data_name)
        local indexes = skin_indexes[player]
        local index = indexes[data_name]
        local key = skins_array[data_name][index]
        player:set_attribute("character_creator:" .. data_name, key)
    end

    save_data("skin")
    save_data("hair")
    save_data("eyes")
    save_data("shirt")
    save_data("pants")

end

local function get_texture(player)
    local indexes = skin_indexes[player]
    local texture = ""
    local gender = player:get_attribute("character_creator:gender")

    local skin_key = skins_array.skin[indexes.skin]
    local skin = skins.skin[skin_key]
    texture = skin

    local hair_key = skins_array.hair[indexes.hair]
    local hair = skins.hair[hair_key]
    texture = texture .. "^(" .. hair

    local eyes_key = skins_array.eyes[indexes.eyes]
    local eyes = skins.eyes[eyes_key]
    texture = texture .. "^(" .. eyes

    local shirt_key = skins_array.shirt[indexes.shirt]
    local shirt = skins.shirt[shirt_key]
    texture = texture .. "^(" .. shirt

    local pants_key = skins_array.pants[indexes.pants]
    local pants = skins.pants[pants_key]
    texture = texture .. "^(" .. pants .."))))"

    return texture
end

local function change_skin(player)
    local texture = get_texture(player)

    local width = tonumber(player:get_attribute("character_creator:width"))
    local height = tonumber(player:get_attribute("character_creator:height"))

    player:set_properties({
        visual_size = {
            x = width,
            y = height
        }
    })

    local name = player:get_player_name()

    if minetest.get_modpath("multiskin") then
        multiskin.layers[name].skin = texture
        armor:set_player_armor(player)
        multiskin:set_player_textures(player, { textures = { texture } })
    elseif minetest.get_modpath("3d_armor") then
        armor.textures[name].skin = texture
        armor:set_player_armor(player)
    else
        player:set_properties({ textures = { texture } })
    end

    save_skin(player)
end

if skinsdb then
    --change skin redefinition for skinsdb
    function change_skin(player)
        local playername = player:get_player_name()
        local skinname = "character_creator:" .. playername
        local skin_obj = skinsdb.get(skinname) or skinsdb.new(skinname)
        skin_obj:set_meta("format", "1.0")
        skin_obj:set_meta("visual_size_x", tonumber(player:get_attribute("character_creator:width")))
        skin_obj:set_meta("visual_size_y", tonumber(player:get_attribute("character_creator:height")))
        skin_obj:apply_skin_to_player(player)
        skinsdb.assign_player_skin(player, "character_creator:" .. playername)
        save_skin(player)
    end
end

minetest.register_on_joinplayer(function(player)
    load_skin(player)
    if skinsdb then
        local playername = player:get_player_name()
        local skinname = "character_creator:" .. playername
        local skin_obj = skinsdb.get(skinname) or skinsdb.new(skinname)
        -- redefinitions
        function skin_obj:set_skin(player)
            if not player or not skin_indexes[player] then
                return -- not loaded or disconnected
            end
            change_skin(player)
            show_formspec(player)
        end

        function skin_obj:get_texture()
            return get_texture(minetest.get_player_by_name(self:get_meta("playername")))
        end

        -- set data
        skin_obj:set_preview("inventory_plus_character_creator.png")
        skin_obj:set_meta("name", "Character Creator")
        --skin_obj:set_meta("author", "???")
        skin_obj:set_meta("license", "MIT / CC-BY-SA 3.0 Unported")
        skin_obj:set_meta("playername", playername)
        --check if active and start the update (avoid race condition for both register_on_joinplayer)
        if skinsdb.get_player_skin(player):get_key() == skinname then
            minetest.after(0, change_skin, player)
        end
    else
        minetest.after(0, change_skin, player)
    end
end)

minetest.register_on_leaveplayer(function(player)
    if skinsdb then
        local skinname = "character_creator:" .. player:get_player_name()
        skinsdb.meta[skinname] = nil
    end
    skin_indexes[player] = nil
end)

local skin_temp = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
    if formname ~= "character_creator" then
        return
    end

    local indexes = skin_indexes[player]
    if not skin_temp[player] then
        skin_temp[player] = {
            gender = player:get_attribute("character_creator:gender"),
            width = player:get_attribute("character_creator:width"),
            height = player:get_attribute("character_creator:height"),
            indexes = table.copy(indexes)
        }
    end

    -- Gender
    do
        if fields.male then
            player:set_attribute("character_creator:gender", "Male")
            player:set_attribute("character_creator:width", 4)
            player:set_attribute("character_creator:height", 4)
        end

        if fields.female then
            player:set_attribute("character_creator:gender", "Female")
            player:set_attribute("character_creator:width", 4)
            player:set_attribute("character_creator:height", 4)
        end
    end

    -- Height
    do
        local height = tonumber(player:get_attribute("character_creator:height"))

        if fields.taller and height < 1.25 then
            player:set_attribute("character_creator:height", height + 0.05)
        end

        if fields.shorter and height > 0.75 then
            player:set_attribute("character_creator:height", height - 0.05)
        end
    end

    -- Width
    do
        local width = tonumber(player:get_attribute("character_creator:width"))

        if fields.wider and width < 1.25 then
            player:set_attribute("character_creator:width", width + 0.05)
        end

        if fields.thinner and width > 0.75 then
            player:set_attribute("character_creator:width", width - 0.05)
        end
    end

    -- Switch skin
    do
        local function switch_skin(data_name, next_index)
            local index = indexes[data_name] + next_index
            local max = #skins_array[data_name]

            if index == 0 then
                index = max
            elseif index == (max + 1) then
                index = 1
            end

            indexes[data_name] = index
        end

        for field in pairs(fields) do
            if field:find("_back$") then
                local data_name = field:match("(.+)_back$")
                switch_skin(data_name, -1)
            elseif field:find("_next$") then
                local data_name = field:match("(.+)_next$")
                switch_skin(data_name, 1)
            end
        end
    end

    -- Close or update
    do
        local quit = false

        if fields.cancel then
            local temp = skin_temp[player]
            player:set_attribute("character_creator:gender", temp.gender)
            player:set_attribute("character_creator:width", temp.width)
            player:set_attribute("character_creator:height", temp.height)
            skin_indexes[player] = table.copy(temp.indexes)
            skin_temp[player] = nil
            quit = true
        elseif fields.quit then
            skin_temp[player] = nil
            quit = true
        end

        if not quit then
            show_formspec(player)
        end
    end
    change_skin(player)
end)

minetest.register_chatcommand("skin", {
    func = function(name)
        minetest.after(0.5, function()
            local player = minetest.get_player_by_name(name)
            if player then
                show_formspec(player)
            end
        end)
    end
})

minetest.register_chatcommand("say", {
    params = "<text>",
    description = "Send text to chat",
    privs = { talk = true },
    func = function(_, text)
        minetest.chat_send_all(text)
        return true, "Text was sent successfully"
    end,
})

if minetest.global_exists("unified_inventory") then
    unified_inventory.register_button("character_creator", {
        type = "image",
        image = "inventory_plus_character_creator.png",
        action = show_formspec
    })
elseif minetest.global_exists("inventory_plus") then
    minetest.register_on_joinplayer(function(player)
        inventory_plus.register_button(player, "character_creator", "Skin")
    end)
    minetest.register_on_player_receive_fields(function(player, _, fields)
        if fields.player then
            show_formspec(player)
        end
    end)
end
seccond lua file

Code: Select all

return {
    skin = {
        ["skinwhite.png"] = "skin_white.png",
        ["skinblack.png"] = "skin_black.png",
    },

    hair = {
        ["hairwhite.png"] = "hair_white.png",
        ["hairwhite.png^[multiply:#3f3f3f^hairoverlay.png"] = "hair_white.png^[multiply:#3f3f3f",
        ["hairwhite.png^[multiply:#ff0000^hairoverlay.png"] = "hair_white.png^[multiply:#ff0000",
        ["hairwhite.png^[multiply:#ff7f00^hairoverlay.png"] = "hair_white.png^[multiply:#ff7f00",
        ["hairwhite.png^[multiply:#ffff00^hairoverlay.png"] = "hair_white.png^[multiply:#ffff00",
        ["hairwhite.png^[multiply:#00ff00^hairoverlay.png"] = "hair_white.png^[multiply:#00ff00",
        ["hairwhite.png^[multiply:#0000ff^hairoverlay.png"] = "hair_white.png^[multiply:#0000ff",
        ["hairwhite.png^[multiply:#9f00ff^hairoverlay.png"] = "hair_white.png^[multiply:#9f00ff",
    },

    eyes = {
        ["eyeswhite.png"] = "eyes_white.png",
        ["eyeswhite.png^[multiply:#3f3f3f^eyesoverlay.png"] = "eyes_white.png^[multiply:#3f3f3f",
        ["eyeswhite.png^[multiply:#ff0000^eyesoverlay.png"] = "eyes_white.png^[multiply:#ff0000",
        ["eyeswhite.png^[multiply:#ff7f00^eyesoverlay.png"] = "eyes_white.png^[multiply:#ff7f00",
        ["eyeswhite.png^[multiply:#ffff00^eyesoverlay.png"] = "eyes_white.png^[multiply:#ffff00",
        ["eyeswhite.png^[multiply:#00ff00^eyesoverlay.png"] = "eyes_white.png^[multiply:#00ff00",
        ["eyeswhite.png^[multiply:#0000ff^eyesoverlay.png"] = "eyes_white.png^[multiply:#0000ff",
        ["eyeswhite.png^[multiply:#9f00ff^eyesoverlay.png"] = "eyes_white.png^[multiply:#9f00ff",
    },

    shirt = {
        ["shirtwhite.png"] = "shirt_white.png",
        ["shirtwhite.png^[multiply:#3f3f3f"] = "shirt_white.png^[multiply:#3f3f3f",
        ["shirtwhite.png^[multiply:#ff0000"] = "shirt_white.png^[multiply:#ff0000",
        ["shirtwhite.png^[multiply:#ff7f00"] = "shirt_white.png^[multiply:#ff7f00",
        ["shirtwhite.png^[multiply:#ffff00"] = "shirt_white.png^[multiply:#ffff00",
        ["shirtwhite.png^[multiply:#00ff00"] = "shirt_white.png^[multiply:#00ff00",
        ["shirtwhite.png^[multiply:#0000ff"] = "shirt_white.png^[multiply:#0000ff",
        ["shirtwhite.png^[multiply:#9f00ff"] = "shirt_white.png^[multiply:#9f00ff",
    },

    pants = {
        ["pantswhite.png"] = "pants_white.png",
        ["pantswhite.png^[multiply:#3f3f3f"] = "pants_white.png^[multiply:#3f3f3f",
        ["pantswhite.png^[multiply:#ff0000"] = "pants_white.png^[multiply:#ff0000",
        ["pantswhite.png^[multiply:#ff7f00"] = "pants_white.png^[multiply:#ff7f00",
        ["pantswhite.png^[multiply:#ffff00"] = "pants_white.png^[multiply:#ffff00",
        ["pantswhite.png^[multiply:#00ff00"] = "pants_white.png^[multiply:#00ff00",
        ["pantswhite.png^[multiply:#0000ff"] = "pants_white.png^[multiply:#0000ff",
        ["pantswhite.png^[multiply:#9f00ff"] = "pants_white.png^[multiply:#9f00ff",
    },
}
If i open the window of this tool ingame, it shows the arrow buttons to change all five character options, but only the icon for the skin-color
The places, where the other four icons should be, are empty.

Image

What´s wrong with it?

are menu-buttons unable to use textures with modifyers like "shirtwhite.png^[multiply:#ff0000"?
Attachments
Character-Designer.png
Character-Designer.png (11.22 KiB) Viewed 436 times
Last edited by Drachenbauer32 on Mon Oct 21, 2019 19:22, edited 1 time in total.

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: What´s wrong with this character designer?

by Krock » Post

Escape `[` in formspec elements.

Output the generated formspec definition to your terminal/console using `print(formspec)`. You'll see that some elements are not parsed correctly due to mixed use of `[`.
Example of the fixed code:

Code: Select all

 .. "image_button[3.5,2.5;1,1;" .. minetest.formspec_escape(skins_array.eyes[indexes.eyes]) .. ";eyes;]"
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Drachenbauer32
Member
Posts: 105
Joined: Wed Aug 28, 2019 18:11

Re: What´s wrong with this character designer?

by Drachenbauer32 » Post

thanks, that works.

another question:
How can i make the colors appear in the order of the list in the seccond code (white, black, red, orange, yellow, green, blue, purple) by clicking the right arrow?

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: What´s wrong with this character designer?

by Krock » Post

String-indexed arrays/tables in Lua are not sorted alphabetically. Use integer indexing (starting from 1) to get a persistent order.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Drachenbauer32
Member
Posts: 105
Joined: Wed Aug 28, 2019 18:11

Re: What´s wrong with this character designer?

by Drachenbauer32 » Post

Now i added tables, wich are indexed with numbers and have the indexes of the old tables as their content.

Code: Select all

return {
    skin = {
        ["skinwhite.png"] = "skin_white.png",
        ["skinblack.png"] = "skin_black.png",
    },

    hair = {
        ["hairwhite.png"] = "hair_white.png",
        ["hairwhite.png^[multiply:#3f3f3f^hairoverlay.png"] = "hair_white.png^[multiply:#3f3f3f",
        ["hairwhite.png^[multiply:#ff0000^hairoverlay.png"] = "hair_white.png^[multiply:#ff0000",
        ["hairwhite.png^[multiply:#ff7f00^hairoverlay.png"] = "hair_white.png^[multiply:#ff7f00",
        ["hairwhite.png^[multiply:#ffff00^hairoverlay.png"] = "hair_white.png^[multiply:#ffff00",
        ["hairwhite.png^[multiply:#00ff00^hairoverlay.png"] = "hair_white.png^[multiply:#00ff00",
        ["hairwhite.png^[multiply:#0000ff^hairoverlay.png"] = "hair_white.png^[multiply:#0000ff",
        ["hairwhite.png^[multiply:#9f00ff^hairoverlay.png"] = "hair_white.png^[multiply:#9f00ff",
    },

    haircount = {
        [1] = "hairwhite.png",
        [2] = "hairwhite.png^[multiply:#3f3f3f^hairoverlay.png",
        [3] = "hairwhite.png^[multiply:#ff0000^hairoverlay.png",
        [4] = "hairwhite.png^[multiply:#ff7f00^hairoverlay.png",
        [5] = "hairwhite.png^[multiply:#ffff00^hairoverlay.png",
        [6] = "hairwhite.png^[multiply:#00ff00^hairoverlay.png",
        [7] = "hairwhite.png^[multiply:#0000ff^hairoverlay.png",
        [8] = "hairwhite.png^[multiply:#9f00ff^hairoverlay.png",
    },

    eyes = {
        ["eyeswhite.png"] = "eyes_white.png",
        ["eyeswhite.png^[multiply:#3f3f3f^eyesoverlay.png"] = "eyes_white.png^[multiply:#3f3f3f",
        ["eyeswhite.png^[multiply:#ff0000^eyesoverlay.png"] = "eyes_white.png^[multiply:#ff0000",
        ["eyeswhite.png^[multiply:#ff7f00^eyesoverlay.png"] = "eyes_white.png^[multiply:#ff7f00",
        ["eyeswhite.png^[multiply:#ffff00^eyesoverlay.png"] = "eyes_white.png^[multiply:#ffff00",
        ["eyeswhite.png^[multiply:#00ff00^eyesoverlay.png"] = "eyes_white.png^[multiply:#00ff00",
        ["eyeswhite.png^[multiply:#0000ff^eyesoverlay.png"] = "eyes_white.png^[multiply:#0000ff",
        ["eyeswhite.png^[multiply:#9f00ff^eyesoverlay.png"] = "eyes_white.png^[multiply:#9f00ff",
    },

    eyescount = {
        [1] = "eyeswhite.png",
        [2] = "eyeswhite.png^[multiply:#3f3f3f^hairoverlay.png",
        [3] = "eyeswhite.png^[multiply:#ff0000^hairoverlay.png",
        [4] = "eyeswhite.png^[multiply:#ff7f00^hairoverlay.png",
        [5] = "eyeswhite.png^[multiply:#ffff00^hairoverlay.png",
        [6] = "eyeswhite.png^[multiply:#00ff00^hairoverlay.png",
        [7] = "eyeswhite.png^[multiply:#0000ff^hairoverlay.png",
        [8] = "eyeswhite.png^[multiply:#9f00ff^hairoverlay.png",
    },

    shirt = {
        ["shirtwhite.png"] = "shirt_white.png",
        ["shirtwhite.png^[multiply:#3f3f3f"] = "shirt_white.png^[multiply:#3f3f3f",
        ["shirtwhite.png^[multiply:#ff0000"] = "shirt_white.png^[multiply:#ff0000",
        ["shirtwhite.png^[multiply:#ff7f00"] = "shirt_white.png^[multiply:#ff7f00",
        ["shirtwhite.png^[multiply:#ffff00"] = "shirt_white.png^[multiply:#ffff00",
        ["shirtwhite.png^[multiply:#00ff00"] = "shirt_white.png^[multiply:#00ff00",
        ["shirtwhite.png^[multiply:#0000ff"] = "shirt_white.png^[multiply:#0000ff",
        ["shirtwhite.png^[multiply:#9f00ff"] = "shirt_white.png^[multiply:#9f00ff",
    },

    shirtcount = {
        [1] = "shirtwhite.png",
        [2] = "shirtwhite.png^[multiply:#3f3f3f",
        [3] = "shirtwhite.png^[multiply:#ff0000",
        [4] = "shirtwhite.png^[multiply:#ff7f00",
        [5] = "shirtwhite.png^[multiply:#ffff00",
        [6] = "shirtwhite.png^[multiply:#00ff00",
        [7] = "shirtwhite.png^[multiply:#0000ff",
        [8] = "shirtwhite.png^[multiply:#9f00ff",
    },

    pants = {
        ["pantswhite.png"] = "pants_white.png",
        ["pantswhite.png^[multiply:#3f3f3f"] = "pants_white.png^[multiply:#3f3f3f",
        ["pantswhite.png^[multiply:#ff0000"] = "pants_white.png^[multiply:#ff0000",
        ["pantswhite.png^[multiply:#ff7f00"] = "pants_white.png^[multiply:#ff7f00",
        ["pantswhite.png^[multiply:#ffff00"] = "pants_white.png^[multiply:#ffff00",
        ["pantswhite.png^[multiply:#00ff00"] = "pants_white.png^[multiply:#00ff00",
        ["pantswhite.png^[multiply:#0000ff"] = "pants_white.png^[multiply:#0000ff",
        ["pantswhite.png^[multiply:#9f00ff"] = "pants_white.png^[multiply:#9f00ff",
    },

    pantscount = {
        [1] = "pantswhite.png",
        [2] = "pantswhite.png^[multiply:#3f3f3f",
        [3] = "pantswhite.png^[multiply:#ff0000",
        [4] = "pantswhite.png^[multiply:#ff7f00",
        [5] = "pantswhite.png^[multiply:#ffff00",
        [6] = "pantswhite.png^[multiply:#00ff00",
        [7] = "pantswhite.png^[multiply:#0000ff",
        [8] = "pantswhite.png^[multiply:#9f00ff",
    },
}
But how can i use them in the main-script of the character-creator?

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: What´s wrong with this character designer?

by Krock » Post

untested

Code: Select all

local skins_array = {} -- Definition code above
local skin_indexes = {} -- Player data


-- Examples:
--  skins_table = "eyes"
--  value = "pantswhite.png^[multiply:#3f3f3f"
--  modifier = any number
function nextIndex(skins_table, value, modifier)
	local index_table = skins_array[skins_table .. "count")
	local i_skin = table.indexof(index_table, value)
	assert(i_skin, "Skin not found: " .. value .. " in table " .. skins_table .. "count")
	-- Keep within array length
	i_skin = (i_skin + modifier - 1) % #index_table + 1

	-- Next string value
	return skins_array[skins_table][index_table[i_skin]]
end

-- Example for player
local indexes = skin_indexes[player_name]
indexes.eyes = nextIndex("eyes", indexes.eyes, -1) -- Previous eye
indexes.shirt = nextIndex("shirt", indexes.shirt,  1) -- Next shirt
-- Current hair:
skins_array.eyes[indexes.eyes]
Maybe it would be helpful to find a way to remove the duplicated code, and to combine it. For example: change the string-indexes table values to a table like {"eyes_white.png^[multiply:#9f00ff", INDEX_HERE} so that you can iterate through all possible values, and find the next index.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Drachenbauer32
Member
Posts: 105
Joined: Wed Aug 28, 2019 18:11

Re: What´s wrong with this character designer?

by Drachenbauer32 » Post

I´m looking for a variant that fit´s here:

Code: Select all

-- Switch skin
    do
        local function switch_skin(data_name, next_index)
            local index = indexes[data_name] + next_index
            local max = #skins_array[data_name]

            if index == 0 then
                index = max
            elseif index == (max + 1) then
                index = 1
            end

            indexes[data_name] = index
        end

        for field in pairs(fields) do
            if field:find("_back$") then
                local data_name = field:match("(.+)_back$")
                switch_skin(data_name, -1)
            elseif field:find("_next$") then
                local data_name = field:match("(.+)_next$")
                switch_skin(data_name, 1)
            end
        end
    end
This code-snippet is the function for the arrow-buttons.
So it should use placeholders ("data_name") for the body-parts everywhere (including the function-call), because it get´s them directly from the fields (buttons) in the formspec

Or here:

Code: Select all

character_creator = {}
character_creator.skins = dofile(minetest.get_modpath("character_creator") .. "/skins.lua")

local skins = character_creator.skins
local skins_array = {}

minetest.after(0, function()
    local function associative_to_array(associative)
        local array = {}
        for key in pairs(associative) do
            table.insert(array, key)
        end
        return array
    end

    skins_array = {
        skin = associative_to_array(skins.skin),
        hair = associative_to_array(skins.hair),
        eyes = associative_to_array(skins.eyes),
        shirt = associative_to_array(skins.shirt),
        pants = associative_to_array(skins.pants),
    }
end)
I´m not sure, what this code-snippet makes with the tables.

User avatar
Drachenbauer32
Member
Posts: 105
Joined: Wed Aug 28, 2019 18:11

Re: What´s wrong with this character designer?

by Drachenbauer32 » Post

Now i have this at the beginning:

Code: Select all

character_creator = {}
character_creator.skins = dofile(minetest.get_modpath("character_creator") .. "/skins.lua")

local skins = character_creator.skins
local skins_array = {}

minetest.after(0, function()

    local function associative_to_array(associative)
        local array = {}
        for key in pairs(associative) do
            array[key] = associative[key]
        end
        return array
    end

    skins_array = {
        skin = associative_to_array(skins.skincount),
        hair = associative_to_array(skins.haircount),
        eyes = associative_to_array(skins.eyescount),
        shirt = associative_to_array(skins.shirtcount),
        pants = associative_to_array(skins.pantscount),
    }

end)
but if i start the game, it loggs in (fills whoole statusbar), but then it gives this error:
2019-10-25 21:19:55: ERROR[Main]: ServerError: AsyncErr: environment_Step: Runtime error from mod 'character_creator' in callback environment_Step(): ...ames\block_colors\mods\Player\character_creator\init.lua:147: attempt to concatenate local 'eyes' (a nil value)
2019-10-25 21:19:55: ERROR[Main]: stack traceback:
2019-10-25 21:19:55: ERROR[Main]: ...ames\block_colors\mods\Player\character_creator\init.lua:147: in function 'get_texture'
2019-10-25 21:19:55: ERROR[Main]: ...ames\block_colors\mods\Player\character_creator\init.lua:161: in function 'func'
2019-10-25 21:19:55: ERROR[Main]: ...n64\minetest-5.0.1-win64\bin\..\builtin\common\after.lua:18: in function <...n64\minetest-5.0.1-win64\bin\..\builtin\common\after.lua:4>
2019-10-25 21:19:55: ERROR[Main]: ...64\minetest-5.0.1-win64\bin\..\builtin\game\register.lua:419: in function <...64\minetest-5.0.1-win64\bin\..\builtin\game\register.lua:399>
2019-10-25 21:19:55: ERROR[Main]: stack traceback:
the only change, i made in this code since the last successfull run of the game, was to change the seccond code-snippet in the last post into the one here.
i checked the output of both with a lttle test-application, that runs directly in intellij idea.
both print an array into the console, that is ordered by numbers.
only the old one has the graphic-file-strings assignet randomly to the numbers, while my new one has them assigned in the right order.

Then i thaught, maybe the "players.sqlite" file in the folder of my world is broken from all theese tests, and deleted it, because it does not exist in a fresh created world, where i never logged in.
But the error still appears.

Edit:

I found the mistake:
The code-snippet here ia alright.
The error was in one of my new skin-arrays in the skins.lua.
I had "hair_overlay.png" instead of "eyes_overlay.png" in the "eyescount"-section.
afier fixing this, now it works again and scrolls through the colors in the right order.

So now all problems here are solved.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests