serialize node position
I'm trying to store the position of a node so that it can be destroyed when a "parent" node is destroyed. The metadata appears to come back as null but I have no idea why.
Here's the code
Here's the error code.
ServerError: LuaError: error: ...poon/minetest-master/bin/../mods/schematics/init.lua:77: bad argument #1 to 'remove_node' (table expected, got nil)
This is the line it's having difficulty with.
pos1 = minetest.deserialize(meta:get_string("pos1"))
Here's the code
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
minetest.register_node("schematics:p1", {
description = "Schematic point 1",
tiles = {"p1_top.png", "p1_bottom.png", "p1_left.png", "p1_right.png", "p1_front.png", "p1_back.png", },
paramtype2 = "facedir",
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
})
minetest.register_node("schematics:p2", {
description = "Schematic point 2",
tiles = {"p2_top.png", "p2_bottom.png", "p2_left.png", "p2_right.png", "p2_front.png", "p2_back.png", },
paramtype2 = "facedir",
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
})
minetest.register_node("schematics:writer", {
description = "Schematic writer",
tiles = {"save_block.png"},
paramtype2 = "facedir",
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
on_construct = function(pos)
--local n = minetest.get_node(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "field[text;;${text}]")
meta:set_string("infotext", """")
end,
on_receive_fields = function(pos, formname, fields, sender)
--print("Sign at "..minetest.pos_to_string(pos).." got "..dump(fields))
local meta = minetest.get_meta(pos)
fields.text = fields.text or ""
print((sender:get_player_name() or "").." wrote ""..fields.text.."" to sign at "..minetest.pos_to_string(pos))
meta:set_string("text", fields.text)
meta:set_string("infotext", '"'..fields.text..'"')
end
})
local on_place_writer = function(pos, node, placer)
if node.name == "schematics:writer"then
local lookDir = placer:get_look_dir()
local pos1, pos2
if math.abs(lookDir.x) < math.abs(lookDir.z) then
pos1 = { x = pos.x+1, y = pos.y, z = pos.z }
pos2 = { x = pos.x-1, y = pos.y, z = pos.z }
else
pos1 = { x = pos.x, y = pos.y, z = pos.z+1 }
pos2 = { x = pos.x, y = pos.y, z = pos.z-1 }
end
minetest.env:set_node(pos1, {name = "schematics:p1"})
minetest.env:set_node(pos2, {name = "schematics:p2"})
local meta = minetest.get_meta(pos)
meta:set_string("pos1", minetest.serialize(pos1))
meta:set_string("pos2", minetest.serialize(pos2))
end
end
local on_place_point = function(pos, node, placer)
if node.name == "schematics:p1" or node.name == "schematics:p1" then
local lookDir = placer:get_look_dir()
local pos1, pos2
if math.abs(lookDir.x) < math.abs(lookDir.z) then
pos1 = { x = pos.x+1, y = pos.y, z = pos.z }
pos2 = { x = pos.x-1, y = pos.y, z = pos.z }
else
pos1 = { x = pos.x, y = pos.y, z = pos.z+1 }
pos2 = { x = pos.x, y = pos.y, z = pos.z-1 }
end
end
end
local on_dig_writer = function(pos, node, placer)
if node.name == "schematics:writer"then
local meta = minetest.get_meta(pos)
local pos1, pos2
pos1 = minetest.deserialize(meta:get_string("pos1"))
pos2 = minetest.deserialize(meta:get_string("pos2"))
minetest.remove_node(pos1)
minetest.remove_node(pos2)
end
end
minetest.register_on_placenode(on_place_writer)
minetest.register_on_dignode(on_dig_writer)
minetest.register_on_placenode(on_place_point)
Here's the error code.
ServerError: LuaError: error: ...poon/minetest-master/bin/../mods/schematics/init.lua:77: bad argument #1 to 'remove_node' (table expected, got nil)
This is the line it's having difficulty with.
pos1 = minetest.deserialize(meta:get_string("pos1"))