Auto spawner Not Working

User avatar
jojoa1997
Member
 
Posts: 2890
Joined: Thu Dec 13, 2012 05:11

Auto spawner Not Working

by jojoa1997 » Wed Mar 06, 2013 04:38

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

Last edited by jojoa1997 on Wed Mar 06, 2013 10:33, edited 1 time in total.
Coding;
1X coding
3X debugging
12X tweaking to be just right
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

by prestidigitator » Wed Mar 06, 2013 05:07

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.
 

User avatar
jojoa1997
Member
 
Posts: 2890
Joined: Thu Dec 13, 2012 05:11

by jojoa1997 » Wed Mar 06, 2013 13:00

Table.getn means it gives me the number of entities around.also each if statement is a seperate one.
Coding;
1X coding
3X debugging
12X tweaking to be just right
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Mar 06, 2013 15:55

This should be in "Modding general".
 

User avatar
jojoa1997
Member
 
Posts: 2890
Joined: Thu Dec 13, 2012 05:11

by jojoa1997 » Wed Mar 06, 2013 16:00

This is a bug but if a moderator thinks that this should be iin modding general then they can move it.
Coding;
1X coding
3X debugging
12X tweaking to be just right
 

User avatar
sfan5
Member
 
Posts: 3636
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5

by sfan5 » Wed Mar 06, 2013 16:30

moved
Mods: Mesecons | WorldEdit | Nuke
Minetest builds for Windows (32-bit & 64-bit)
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

by prestidigitator » Wed Mar 06, 2013 19:24

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
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 8 guests

cron