Page 1 of 1

Auto spawner Not Working

PostPosted: Wed Mar 06, 2013 04:38
by jojoa1997
I cant figure out why the npc spawners wont spawn mobs. For some reason I never noticed when this stopped working.
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
use_mesecons = false

function npc_spawner(pos)
    local MAX_NPC = 5
    local found = table.getn(minetest.env:get_objects_inside_radius(pos, 20))
    if found == nil then
    found = 0

    if found <= MAX_NPC then
        offsetx = math.random(-3,3)
        offsety = math.random(2,4)
        offsetz = math.random(-3,3)
            minetest.env:add_entity({ x=pos.x+offsetx, y=pos.y+offsety, z=pos.z+offsetz }, ("peaceful_npc:npc"))
        end
    end
end

if use_mesecons == true then
    minetest.register_node("peaceful_npc:npc_spawner", {
        description = "NPC Portal",
        drawtype = "glasslike",
        groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3,wood=1},
        sounds = default.node_sound_glass_defaults(),
        tiles = {"peaceful_npc_spawner.png"},
        sunlight_propagates = true,
        paramtype = "light",
        mesecons = {effector = {
            action_on = npc_spawner
        }}
    })
end

if use_mesecons == false then
    minetest.register_node("peaceful_npc:npc_spawner", {
        description = "NPC Portal",
        drawtype = "glasslike",
        groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3,wood=1},
        sounds = default.node_sound_glass_defaults(),
        tiles = {"peaceful_npc_spawner.png"},
        sunlight_propagates = true,
        paramtype = "light",
    })
    minetest.register_abm({
        nodenames = {"peaceful_npc:npc_spawner"},
        interval = 10.0,
        chance = 1,
        action = function(pos)
            npc_spawner(pos)
        end,
    })
end


PostPosted: Wed Mar 06, 2013 05:07
by prestidigitator
Your indentation is a little misleading. I'm not completely sure this is the problem, but its a good chance. Your "npc_spawner()" function won't do anything unless "found" is nil, because the first "if" statement encloses the whole second "if" statement. I'm not sure what "table.getn" is either (unless minetest has added this as a custom API function, but I don't see anything about it on the wiki...), so I'm not sure if and when it will actually return nil. If you meant table.maxn, its documentation doesn't say anything about returning nil, so I wouldn't depend on this behavior.

PostPosted: Wed Mar 06, 2013 13:00
by jojoa1997
Table.getn means it gives me the number of entities around.also each if statement is a seperate one.

PostPosted: Wed Mar 06, 2013 15:55
by PilzAdam
This should be in "Modding general".

PostPosted: Wed Mar 06, 2013 16:00
by jojoa1997
This is a bug but if a moderator thinks that this should be iin modding general then they can move it.

PostPosted: Wed Mar 06, 2013 16:30
by sfan5
moved

PostPosted: Wed Mar 06, 2013 19:24
by prestidigitator
jojoa1997 wrote:Table.getn means it gives me the number of entities around.also each if statement is a seperate one.


I understand that may have been the intent, but I don't think there's actually a "table.getn()" function, unless Minetest has added it somewhere. It is certainly not in the Lua 5.1 reference manual.

As for the if statements:

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)
    local MAX_NPC = 5
    local found = table.getn(minetest.env:get_objects_inside_radius(pos, 20))
    if found == nil then
    found = 0
        -- <<<<---- You may have intended to end the first if here, but there's no "end" token

    if found <= MAX_NPC then
        offsetx = math.random(-3,3)
        offsety = math.random(2,4)
        offsetz = math.random(-3,3)
            minetest.env:add_entity({ x=pos.x+offsetx, y=pos.y+offsety, z=pos.z+offsetz }, ("peaceful_npc:npc"))
        end  -- <<<<---- The innermost if statement ends here
    end  -- <<<<---- That first if statement actually ends here, so NOTHING gets done unless found == nil
end