(SOLVED)Problem with a function

Post Reply
User avatar
qwrwed
Member
Posts: 325
Joined: Sun Jul 22, 2012 20:56
In-game: qwrwed or Nitro

(SOLVED)Problem with a function

by qwrwed » Post

I've been trying to make my lightplus mod more efficient. I have replaced

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

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.
Last edited by qwrwed on Tue Nov 12, 2013 21:18, edited 1 time in total.

User avatar
jin_xi
Member
Posts: 165
Joined: Mon Jul 02, 2012 18:19

by jin_xi » Post

you probably need to add or change some code

User avatar
qwrwed
Member
Posts: 325
Joined: Sun Jul 22, 2012 20:56
In-game: qwrwed or Nitro

by qwrwed » Post

I know that, but I don't know what I need to add or change.

User avatar
general3214
Member
Posts: 122
Joined: Fri Oct 04, 2013 03:15

by general3214 » Post

Try this (I'm sorta new with Minetest modding):

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
Last edited by general3214 on Mon Nov 11, 2013 20:48, edited 1 time in total.
Avatar made by Annahstas

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

by kaeza » Post

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.
Last edited by kaeza on Mon Nov 11, 2013 21:01, edited 1 time in total.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

User avatar
qwrwed
Member
Posts: 325
Joined: Sun Jul 22, 2012 20:56
In-game: qwrwed or Nitro

by qwrwed » Post

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.

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

by kaeza » Post

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):

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.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

User avatar
qwrwed
Member
Posts: 325
Joined: Sun Jul 22, 2012 20:56
In-game: qwrwed or Nitro

by qwrwed » Post

Thanks kaeza, your code works. I have updated my mod with it.
Last edited by qwrwed on Tue Nov 12, 2013 21:19, edited 1 time in total.

Post Reply

Who is online

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