Global functions unable to be called by anything else.
3 replies, posted
I'm currently writing a gamemode, and I've just run in to a problem while writing this function:
[lua]
function glife.AddCommand( command, func )
commandTable[ command ] = func
end
[/lua]
This is stored in gamemode/modules/commands/sv_commands.lua and is ran by the gamemode on load (I have checked this through print statements). glife is a table which is created in my gamemodes shared.lua file. The issue I'm having, is when called from any place that isn't sv_commands.lua I get this error (or similar):
[lua]
[ERROR] gamemodes/harp/gamemode/modules/money/sv_money.lua:15: attempt to call field 'AddCommand' (a nil value)
1. unknown - gamemodes/harp/gamemode/modules/money/sv_money.lua:15
2. include - [C]:-1
3. unknown - gamemodes/harp/gamemode/init.lua:15
[/lua]
Line 15 is just including sh_money.lua, and for those interested, this is what the function that uses it looks like:
[lua]
glife.AddCommand( "pay", function( ply, args )
local foundPlayers = { }
if args[ 1 ] == nil then ply:PrintMessage( HUD_PRINTTALK, "No player specified!" ) return "" end
if args[ 2 ] == nil then ply:PrintMessage( HUD_PRINTTALK, "No amount specified!" ) return "" end
for k,v in pairs( player.GetAll() ) do
if string.find( v:Name(), args[ 1 ], 0, true ) != nil then
table.insert( foundPlayers, v )
end
end
if table.Count( foundPlayers ) == 1 then
if foundPlayers[ 1 ] != ply then
if ply:CanAfford( args[ 2 ] ) then
ply:TakeMoney( args[ 2 ] )
ply:PrintMessage( HUD_PRINTTALK, "You paid $" .. args[ 2 ] .. " to " .. foundPlayers[ 1 ]:Name() .. "." )
foundPlayers[ 1 ]:PrintMessage( HUD_PRINTTALK, "You recieved $" .. args[ 2 ] .. " from " .. ply:Name() .. "." )
else
ply:PrintMessage( HUD_PRINTTALK, "You can't afford this!" )
end
else
ply:PrintMessage( HUD_PRINTTALK, "You can't pay yourself!" )
end
else
ply:PrintMessage( HUD_PRINTTALK, "Couldn't find a player to pay. Try being more specific." )
end
return ""
end )
[/lua]
I've tried renaming the function, removing the table, making the table serverside and moving code to shared, but still no luck. Any ideas?
[editline]28th October 2014[/editline]
PrintTable seems to find the function:
[lua]
] rcon lua_run PrintTable( glife )
> PrintTable ( glife )...
AddCommand = function: 0x21a624c8
[/lua]
The function does not exist in the table at the time of the function call.
what order are you including the files in?
Ah, turns out I was including sv_commands last, thanks for the help!
Sorry, you need to Log In to post a reply to this thread.