Page 1 of 1

Help nonconsistant recurring error

PostPosted: Sun Mar 10, 2013 15:33
by jojoa1997
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
function npc_spawner(pos, SPAWN_TYPE)
    local MAX_NPC = 15
    local count = table.getn(minetest.env:get_objects_inside_radius(pos, 50))
    if count == nil then
        count = 0
    end
   
    if count <= MAX_NPC then
        minetest.env:add_entity({x=pos.x+math.random(-1,1),y=pos.y+math.random(2,3),z=pos.z+math.random(-1,1)}, SPAWN_TYPE)
    end
end


Error
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
11:27:03: ERROR[main]: ========== ERROR FROM LUA ===========
11:27:03: ERROR[main]: ...minetest\bin\..\mods\mod_test\peaceful_npc/items.lua:4: attempt to index field 'env' (a nil value)
11:27:03: ERROR[main]: stack traceback:
11:27:03: ERROR[main]:     ...minetest\bin\..\mods\mod_test\peaceful_npc/items.lua:4: in function 'npc_spawner'
11:27:03: ERROR[main]:     ...minetest\bin\..\mods\mod_test\peaceful_npc/items.lua:53: in main chunk
11:27:03: ERROR[main]:     [C]: in function 'dofile'
11:27:03: ERROR[main]:     ...\minetest\bin\..\mods\mod_test\peaceful_npc\init.lua:6: in main chunk
11:27:03: ERROR[main]: =======END OF ERROR FROM LUA ========

PostPosted: Sun Mar 10, 2013 16:00
by PilzAdam
If you call this function when the code is executed the first time (for registration of node, entities, etc.) minetest.env isnt defined. You can only call this callbacks (like nodedef.on_punch).

PostPosted: Sun Mar 10, 2013 16:45
by jojoa1997
PilzAdam wrote:If you call this function when the code is executed the first time (for registration of node, entities, etc.) minetest.env isnt defined. You can only call this callbacks (like nodedef.on_punch).
fixed in irc. please close this