[solved] set_node error

User avatar
octacian
Member
 
Posts: 408
Joined: Mon Dec 21, 2015 22:18
GitHub: octacian
IRC: octacian
In-game: octacian

[solved] set_node error

by octacian » Fri Jun 10, 2016 20:35

I am working on a command for my mod, that update a node in the location specified through a chatcommand to the block specified. This should be pretty simple, but I'm trying to write it in a way that players don't get all the weird errors and get confused (like me), but instead get simple error messages through the chat.

I'm having two issues. When I run /update default:dirt 125 1.5 -26.2, I get "attempt to index a nil value," at line 39.
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 item_string_type = minetest.registered_items[node].type -- item string type


This line gets the type (node, craftitem, tool) for an if statement that confirms the type is node.

When I run the command the other way around, /update 125 1.5 -26.2 default:dirt, I get and error in the chat saying:
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
2016-06-10 13:14:11: ERROR[Server]: Map::setNode(): Not allowing to place CONTENT_IGNORE while trying to replace "air" at (125,2,-26) (block (7,0,-2))


The /update functions accepts two values, name and param. player is then set from param, and node, p is set from param. Then I set item_string_type, for the if statement.
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
  -- variables
local player = minetest.get_player_by_name(name) -- player name
local node, p = string.match(param, "^([^ ]+) *(.*)$")
local p = minetest.string_to_pos(p)
local item_string_type = minetest.registered_items[node].type -- item string type


+ The full function called by /update:


You can browse the code of the whole mod here, though functions.lua is missing several small fixes that I have not committed yet because of the above issues.
Last edited by octacian on Sat Jun 11, 2016 00:49, edited 1 time in total.
God isn't dead!

My Coolest Mods:
MicroExpansion, Working Computers, Interchangeable Hands

Check out my YouTube channel! (octacian)
 

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

Re: Errors... help!

by kaeza » Fri Jun 10, 2016 22:39

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.set_node(p, {name = param})

You probably meant to use `node` instead of `param` here.

From your code, it seems you get (for example) `default:dirt 1,2,3` as param which you split into `node` ("[^ ]+" pattern), and `p` (".*" pattern).

A few more tips:

  • `string.match` may return nil if the string does not match the pattern, so you should take that into account before passing to `string_to_pos`.
  • As an extension to the above, `string_to_pos` may also return nil if it cannot parse the string as a position.
  • You seem to compare `registered_nodes[node]` to both nil and a string. IIRC, registered_nodes[node] always returns a table thanks to metatables, so you should use another check to see if the node exists. Probably `rawget(minetest.registered_nodes, node) ~= nil`.
  • You also seem to compare strings to numbers in `if p.x=="" ...`. This would not crash, but it will always return false.
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
octacian
Member
 
Posts: 408
Joined: Mon Dec 21, 2015 22:18
GitHub: octacian
IRC: octacian
In-game: octacian

Re: Errors... help!

by octacian » Sat Jun 11, 2016 00:48

Thanks!

The function now looks like this, and seems to work fine:
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
-- Update Block
function servertools.update_node(name, param)
  -- variables
  local player = minetest.get_player_by_name(name) -- player name
   local node, p = string.match(param, "^([^ ]+) *(.*)$")
   local p = minetest.string_to_pos(p)

   -- confirm node param
   if minetest.registered_items[node] == nil then
      minetest.chat_send_player(name, "Please enter a valid item string.") --print to chat
      minetest.log("action", "[ServerTools] "..name.." tried to update a node with an invalid item string.") --print to log
   else
      local item_string_type = minetest.registered_items[node].type -- item string type
      -- make sure item string is not a craftitem or tool
      if item_string_type == "tool" or item_string_type == "craft" then
         minetest.chat_send_player(name, "Item string is of invalid type "..item_string_type..".") --print to chat
         minetest.log("action", "[ServerTools] "..name.." tried to update to "..item_string_type..".") --print to log
      else
         if p == nil or p == "" then
            minetest.chat_send_player(name, "Please enter valid coordinates.") --print to chat
            minetest.log("action", "[ServerTools] "..name.." tried to update a node with invalid coordinates.") --print to log
            return
         end
         -- confirm position params
        if p.x == nil or p.y == nil or p.y == nil or p.z == nil then
          -- print error to chat and log
          minetest.chat_send_player(name, "Please enter valid coordinates.") -- print to chat
          minetest.log("action", "[ServerTools] "..name.." tried to update a node with invalid coordinates.") --print to log
        else
          -- update node
            minetest.set_node(p, {name = node})
        end
      end
   end
end


If I missed something, I always appreciate knowing. PS: Check out the mod that this is for, servertools (not quite ready to post on the forums yet).
God isn't dead!

My Coolest Mods:
MicroExpansion, Working Computers, Interchangeable Hands

Check out my YouTube channel! (octacian)
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron