Page 1 of 1

(SOLVED)Problem with a function

PostPosted: Mon Nov 11, 2013 20:33
by qwrwed
I've been trying to make my lightplus mod more efficient. I have replaced
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local on_light_puncher = function (pos, node, puncher)
  if node.name == 'lightsplus:light' then
    minetest.env:add_node(pos, {name="lightsplus:light_on"})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:light_on' then
    minetest.env:add_node(pos, {name="lightsplus:light",})
    nodeupdate(pos)
  end
  if node.name == 'lightsplus:slab_light' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_on"})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:slab_light_on' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light",})
    nodeupdate(pos)
  end
  if node.name == 'lightsplus:slab_light_inv' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_inv_on"})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:slab_light_inv_on' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_inv",})
    nodeupdate(pos)
  end
  if node.name == 'lightsplus:slab_light_wall' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_wall_on", param2=node.param2})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:slab_light_wall_on' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_wall", param2=node.param2})
    nodeupdate(pos)
  end
    if node.name == 'lightsplus:light_gold' then
    minetest.env:add_node(pos, {name="lightsplus:light_on_gold"})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:light_on_gold' then
    minetest.env:add_node(pos, {name="lightsplus:light_gold",})
    nodeupdate(pos)
  end
  if node.name == 'lightsplus:slab_light_gold' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_on_gold"})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:slab_light_on_gold' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_gold",})
    nodeupdate(pos)
  end
  if node.name == 'lightsplus:slab_light_inv_gold' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_inv_on_gold"})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:slab_light_inv_on_gold' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_inv_gold",})
    nodeupdate(pos)
  end
  if node.name == 'lightsplus:slab_light_wall_gold' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_wall_on_gold", param2=node.param2})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:slab_light_wall_on_gold' then
    minetest.env:add_node(pos, {name="lightsplus:slab_light_wall_gold", param2=node.param2})
    nodeupdate(pos)
  end
    if node.name == 'lightsplus:light_flat' then
    minetest.env:add_node(pos, {name="lightsplus:light_flat_on", param2=node.param2,})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:light_flat_on' then
    minetest.env:add_node(pos, {name="lightsplus:light_flat", param2=node.param2,})
    nodeupdate(pos)
  end
  if node.name == 'lightsplus:light_flat_gold' then
    minetest.env:add_node(pos, {name="lightsplus:light_flat_on_gold", param2=node.param2,})
    nodeupdate(pos)
  elseif node.name == 'lightsplus:light_flat_on_gold' then
    minetest.env:add_node(pos, {name="lightsplus:light_flat_gold", param2=node.param2,})
    nodeupdate(pos)
  end
end
minetest.register_on_punchnode(on_light_puncher)
with
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local lights = {
    {"lightsplus:light", "lightsplus:light_on"},
    {"lightsplus:gold_light", "lightsplus:gold_light_on"},
    {"lightsplus:slab_light", "lightsplus:slab_light_on"},
    {"lightsplus:gold_slab_light", "lightsplus:gold_slab_light_on"},
    {"lightsplus:flat_light", "lightsplus:flat_light_on"},
    {"lightsplus:gold_flat_light", "lightsplus:gold_flat_light_on"},
}

for i in ipairs(lights) do
    local off = lights[i][1]
    local on = lights[i][2]
   

local on_light_puncher = function (pos, node, puncher)
    if node.name == off then
        minetest.env:add_node(pos, {name=on})
        nodeupdate(pos)
    elseif node.name == on then
        minetest.env:add_node(pos, {name=off})
        nodeupdate(pos)
        end
    end
end

minetest.register_on_punchnode(on_light_puncher)
(and also changed the names to make more sense). However, when punching any of the lights, none turn on or off as intended. How do I make this mod work? There is probably something simple I missed.

PostPosted: Mon Nov 11, 2013 20:35
by jin_xi
you probably need to add or change some code

PostPosted: Mon Nov 11, 2013 20:36
by qwrwed
I know that, but I don't know what I need to add or change.

PostPosted: Mon Nov 11, 2013 20:44
by general3214
Try this (I'm sorta new with Minetest modding):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local lights = {
    {"lightsplus:light", "lightsplus:light_on"},
    {"lightsplus:gold_light", "lightsplus:gold_light_on"},
    {"lightsplus:slab_light", "lightsplus:slab_light_on"},
    {"lightsplus:gold_slab_light", "lightsplus:gold_slab_light_on"},
    {"lightsplus:flat_light", "lightsplus:flat_light_on"},
    {"lightsplus:gold_flat_light", "lightsplus:gold_flat_light_on"},
}

for _, row in ipairs(lights) do
    local off = row[1]
    local on = row[2]
   
    on_light_puncher = function (pos, node, puncher)
        if node.name == off then
            minetest.env:add_node(pos, {name=on})
            nodeupdate(pos)
        elseif node.name == on then
            minetest.env:add_node(pos, {name=off})
            nodeupdate(pos)
        end
    end
end

minetest.register_on_punchnode(on_light_puncher)


EDIT: Fixed reference to my code

PostPosted: Mon Nov 11, 2013 20:59
by kaeza
Probably doesn't work because you are redefining the on_light_puncher function on each iteration.

Tip: Use the on_punch field on the node definition.

PostPosted: Mon Nov 11, 2013 21:03
by qwrwed
general3214: Your code has the same effect as mine (none).
kaeza: So what do I need to do in order to fix this? I'm relatively new to doing things like this as opposed to copying, pasting and adapting code.

PostPosted: Mon Nov 11, 2013 21:36
by kaeza
qwrwed wrote:kaeza: So what do I need to do in order to fix this? I'm relatively new to doing things like this as opposed to copying, pasting and adapting code.


Something like this should work(untested):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local lights = {
    { "lights:light1_off", "lights:light1_on" },
    { "lights:light2_off", "lights:light2_on" },
    -- ...
}

for _, row in ipairs(lights) do
    local off = row[1]
    local on = row[2]
    minetest.register_node(off, {
        description = "Light off",
        -- Other stuff here.
        on_punch = function(pos, node, puncher)
            minetest.set_node(pos, {name=on})
        end,
    })
    minetest.register_node(on, {
        description = "Light on",
        drop = off,
        -- Other stuff here.
        on_punch = function(pos, node, puncher)
            minetest.set_node(pos, {name=off})
        end,
    })
end


Feel free to adapt for your purposes.

PostPosted: Tue Nov 12, 2013 21:19
by qwrwed
Thanks kaeza, your code works. I have updated my mod with it.