Page 1 of 1

error in misc file

PostPosted: Sat Jun 15, 2013 23:38
by JBR
Ok i was testing my mod and I got this error minetest-0.4.7\bin\..\builtin/misc.lua:17: attempt to call field 'func' (a nil value)

The error came up when this was called or when the function game was called.
Code: Select all
    minetest.after(4,game)

function game:
Code: Select all
function game()
    players = minetest.get_connected_players()
    if #players < 2 then
        reset()
        return
    end
    for i = 1, #catchers do
        local num = math.random(1, #catcherspawns[lvlnum])
        catchers[i]:setpos(catcherspawns[lvlnum])
    end
    for i = 1, #runners do
        local num = math.random(1, #runnerspawns[lvlnum])
        runners[i]:setpos(runnerpawns[lvlnum])
    end
    GameOn = true
    loading = false
end


Please help.

Also it didn't wait before it called the function.

edit: I'm using minetest version 4.7

PostPosted: Sat Jun 15, 2013 23:46
by PilzAdam
Try making the game function local, to be sure that other mods dont screw it up.

PostPosted: Sat Jun 15, 2013 23:50
by JBR
PilzAdam wrote:Try making the game function local, to be sure that other mods dont screw it up.


wait your saying that if your don't make functions/variables are not local they can be accessed by other mods? Whats the point of using dofile then?

My though is that maybe because on 4.7 they have changed minetest.env to just minetest maybe they forgot to change one of the functions to minetest. Do you think I should try using minetest.env instead?

edit: I just tried making the game function local and it didn't work, still got the same error.

PostPosted: Sat Jun 15, 2013 23:54
by PilzAdam
All mods run in the same namespace. This is extremly useful for mods like mesecons (other mods can add mesecon functionality and so on).

Can you please paste your whole code? I guess game is just not defined when running minetest.after.

PostPosted: Sun Jun 16, 2013 00:03
by JBR
PilzAdam wrote:All mods run in the same namespace. This is extremly useful for mods like mesecons (other mods can add mesecon functionality and so on).

Can you please paste your whole code? I guess game is just not defined when running minetest.after.


Here's the relevant part of the code:

Code: Select all
function count(num)
    minetest.chat_send_all(num)
end

function game()
    players = minetest.get_connected_players()
    if #players < 2 then
        reset()
        return
    end
    for i = 1, #catchers do
        local num = math.random(1, #catcherspawns[lvlnum])
        catchers[i]:setpos(catcherspawns[lvlnum])
    end
    for i = 1, #runners do
        local num = math.random(1, #runnerspawns[lvlnum])
        runners[i]:setpos(runnerpawns[lvlnum])
    end
    GameOn = true
    loading = false
end


function chooseplayers()
    players = minetest.get_connected_players()
    if #players < 2 then
        reset()
        return
    end
    numcatchers = #players/2
    numcatchers = math.floor(numcatchers)
    chosen = {}
    peepstochoose = players
    for i = 1, numcatchers do
        local num = math.random(1,#peepstochoose)
        table.insert(chosen, peepstochoose[num])
        table.remove(peepstochoose, num)
    end
    cmsg = ""
    for i = 1, #chosen do
        cmsg = cmsg..chosen[i]:get_player_name()
        if i ~= #chosen then
            cmsg = cmsg..", "
        end
    end
    minetest.chat_send_all("Catchers chosen! They are: "..cmsg)
    for i = 1, #chosen do
        table.insert(catchers,chosen[i])
    end
    for i = 1, #peepstochoose do
        table.insert(runners,peepstochoose[i])
    end
    minetest.chat_send_all("Game starting in..")
    minetest.after(1,count(3))
    minetest.after(2,count(2))
    minetest.after(3,count(1))
    minetest.after(4,game)
end


function main(dtime)
    timer = timer+dtime
    players = minetest.get_connected_players()
    if #players > 1 then
        if not GameOn then
            if not loading then
                loading = true
                local num = math.random(1,#levels)
                lvlname = levels[num]
                lvlnum = num
                minetest.chat_send_all("Level chosen:"..lvlname)
                minetest.chat_send_all("Selecteting catchers..")
                minetest.after(2.0, chooseplayers)
            end
        end
    end
end

PostPosted: Sun Jun 16, 2013 00:10
by PilzAdam
The problem is
Code: Select all
minetest.after(1,count(3))

Change it to
Code: Select all
minetest.after(1, count, 3)
You need to pass a function to minetest.after(), parameters for this function can be added as additional parameters to minetest.after().

PostPosted: Sun Jun 16, 2013 00:14
by JBR
PilzAdam wrote:The problem is
Code: Select all
minetest.after(1,count(3))

Change it to
Code: Select all
minetest.after(1, count, 3)
You need to pass a function to minetest.after(), parameters for this function can be added as additional parameters to minetest.after().


That worked thanks! :)