(SOLVED)Problem with a function

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

(SOLVED)Problem with a function

by qwrwed » Mon Nov 11, 2013 20:33

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

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

by jin_xi » Mon Nov 11, 2013 20:35

you probably need to add or change some code
 

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

by qwrwed » Mon Nov 11, 2013 20:36

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

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

by general3214 » Mon Nov 11, 2013 20:44

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
Last edited by general3214 on Mon Nov 11, 2013 20:48, edited 1 time in total.
March 6, 2014, 8:37 PM PST: I will no longer continue my mods.
Mods | GitHub | MCF
Avatar made by Annahstas
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

by kaeza » Mon Nov 11, 2013 20:59

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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

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

by qwrwed » Mon Nov 11, 2013 21:03

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
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

by kaeza » Mon Nov 11, 2013 21:36

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.
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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

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

by qwrwed » Tue Nov 12, 2013 21:19

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.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 10 guests

cron