I'm encountering a problem in a Lua script that I'm learning from (i am new to Lua) this error got me heavily confused, when I run the code it gives me this following error:
attempt to index global "zoneName" (a nil value)
this is my code:
local zoneName = zoneName:gsub ( "'", "" )
if dbExec ( handler, "INSERT INTO `safeZones` (`rowID`, `zoneName`, `zoneX`, `zoneY`, `zoneWidth`, `zoneHeight`) VALUES (NULL, '".. tostring ( zoneName ) .."', '".. tostring ( zoneX ) .."', '".. tostring ( zoneY ) .."', '".. zoneWidth .."', '".. zoneHeight .."');" ) then
createSafeZone ( { [ "zoneName" ] = zoneName, [ "zoneX" ] = zoneX, [ "zoneY" ] = zoneY, [ "zoneWidth" ] = zoneWidth, [ "zoneHeight" ] = zoneHeight } )
outputDebugString ( "Safe Zones: Safe zone created name: ".. tostring ( zoneName ) )
return true
else
return false, "Unable to create the safe zone"
end
You reference zoneName already in it's definition, you code equals to
local zoneName = nil:gsub("'", "")
hence the error (zoneName is not yet defined when Lua tries to execute zoneName:gsub()).
Either define zoneName before the gsub() call or use string.gsub()
Related
For building a dynamic SELECT I am iterating over a field list and adding an alias.
However, that clears the list.
REPORT ZITER_TEST.
CONSTANTS lc_alias TYPE string VALUE 'ref'.
DATA lt_field TYPE string_table.
lt_field = VALUE string_table(
( CONV string( 'PERNR' ) )
( CONV string( 'SUBTY' ) )
).
" lt_field: ['PERNR','SUBTY']
lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field
( lc_alias && `~` && <lv_tmp> )
).
" lt_field: [] instead of: ['ref~PERNR','ref~SUBTY']
The ABAP documentation of VALUE says:
In assignments to a data object, the target variable is used directly and no temporary data object is created. This variable is initialized or overwritten in full before the assignment of the values specified in the parentheses. Its original value, however, is still available in an optional LET expression.
So, instead of:
lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field
( lc_alias && `~` && <lv_tmp> )
).
Use:
lt_field = VALUE string_table( LET lt_field_temp = lt_field IN
FOR <lv_tmp> IN lt_field_temp
( lc_alias && `~` && <lv_tmp> )
).
You just have to use some temporary table. It works in my case:
REPORT ZITER_TEST.
CONSTANTS lc_alias TYPE string VALUE 'ref'.
DATA: lt_field_temp TYPE string_table,
lt_field TYPE string_table.
lt_field_temp = VALUE string_table(
( CONV string( 'PERNR' ) )
( CONV string( 'SUBTY' ) )
).
lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field_temp
( lc_alias && '~' && <lv_tmp> )
).
Looping table with field-symbol will be significantly faster than using temporary table in either form (LET or direct assign):
LOOP AT lt_field ASSIGNING FIELD-SYMBOL(<field>)
<field> = lc_alias && `~` && <field>.
ENDLOOP.
I have a table like this
local ftable = {
getPinId = app.getPinId
}
ftable is passed to another function which exports it as a RPC interface.
This works but now I want to add function call tracing to a log file.
The simple approach is
local ftable = {
getPinId = function(...) print("getPinId") app.getPinId(...) end
}
But, this is not particularly nice.
I'd like to put something like:
local trace = function(func, ...)
return function(...) print(func) func(...) end
end
local ftable = {
getPinId = trace(app.getPinId)
}
But this doesn't produce quite the desired result. The parameters are not being passed through.
One other option is to use a metatable like this:
local ftable = {}
setmetatable(ftable, {
__index = function(_, k)
printf("Call: app.%s\n", k) return app[k] end
})
Which works. But I'd also like to be able to print the parameters that are passed if possible.
Any suggestions?
I'm exclusively using luajit if that makes any difference.
Wrapping a function call is easy in Lua:
local function wrap( f )
local function after( ... )
-- code to execute *after* function call to f
print( "return values:", ... )
return ...
end
return function( ... )
-- code to execute *before* function call to f
print( "arguments:", ... )
return after( f( ... ) )
end
end
local function f( a, b, c )
return a+b, c-a
end
local f_wrapped = wrap( f )
f_wrapped( 1, 2, 3 )
Output is:
arguments: 1 2 3
return values: 3 2
One problem for logging/tracing is that Lua values (including functions) don't have names themselves. The debug library tries to find suitable names for functions by inspecting how they are called or where they are stored, but if you want to make sure, you'll have to supply a name yourself. However, if your functions are stored in (nested) tables (as indicated in a comment), you could write a function that iterates the nested tables, and wraps all functions it finds using the table keys as names:
local function trace( name, value )
local t = type( value )
if t == "function" then -- do the wrapping
local function after( ... )
print( name.." returns:", ... )
return ...
end
return function( ... )
print( "calling "..name..":", ... )
return after( value( ... ) )
end
elseif t == "table" then -- recurse into subtables
local copy = nil
for k,v in pairs( value ) do
local nv = trace( name.."."..tostring( k ), v )
if nv ~= v then
copy = copy or setmetatable( {}, { __index = value } )
copy[ k ] = nv
end
end
return copy or value
else -- other values are ignored (returned as is)
return value
end
end
local ftable = {
getPinId = function( ... ) return "x", ... end,
nested = {
getPinId = function( ... ) return "y", ... end
}
}
local ftableTraced = trace( "ftable", ftable )
ftableTraced.getPinId( 1, 2, 3 )
ftableTraced.nested.getPinId( 2, 3, 4 )
Output is:
calling ftable.getPinId: 1 2 3
ftable.getPinId returns: x 1 2 3
calling ftable.nested.getPinId: 2 3 4
ftable.nested.getPinId returns: y 2 3 4
Some things to be aware of:
Table keys can be arbitrary Lua values, not just short strings entirely consisting of printable characters.
Tables can contain cyclic references. If they do, the naive implementation above will die with a stack overflow.
Use the __call metamethod instead:
M = { __call =
function (t,...) print("calling ",t.name,...) return t.func(...) end
}
trace = function(func,name)
return setmetatable({func=func,name=name},M)
end
function f(...)
print("in f",...)
end
g=trace(f,"f")
g(10,20,30)
This is my first time using metatables, I did a simple script to test in Lua demo but it always give me "attempt to call method 'rename' (a nil value)", why?
peds = {}
function peds.new ( name )
local tb = { name = name }
setmetatable ( tb, { __index = peds } )
return tb
end
function peds.rename ( name )
self.name = name
return self.name == name
end
local ped = peds.new ( "max" )
ped:rename ( "randomname" )
There's two (possible) problems in your code, depending on how you are setting things up.
If you are just typing the above into a REPL, then when you declare local ped = ... it immediately goes out of scope and becomes inaccessible. So the expression ped:rename is invalid, although it should report "ped is nil" not "rename is nil".
If you are saving the above to a script and loading it using load_file or something, you will still get a problem, because this function signature isn't right:
function peds.rename ( name )
should be:
function peds.rename ( self, name )
Similar to how it works in C++, in lua, when you make an object method, you have to take the hidden self parameter first, and when you call ped:rename( "random name" ) that's just syntactic sugar for ped.rename(ped, "random_name"). If the self parameter doesn't exist then it's not going to work, or may even say "function not found / rename is nil" because the signatures don't match up.
In Roblox Studio, I have a ModuleScript object that implements an analogous class to the one shown in chapter 16 of the 1st edition of Programming In Lua, as shown below:
local h4x0r = { }
local function setCurrentEnvironment( t, env )
if ( not getmetatable( t ) ) then
setmetatable( t, { __index = getfenv( 0 ) } )
end
setfenv( 0, t )
end
do
setCurrentEnvironment( h4x0r );
do
h4x0r.Account = { };
setCurrentEnvironment( h4x0r.Account );
__index = h4x0r.Account;
function withdraw( self, v )
self.balance = self.balance - v;
return self.balance;
end
function deposit( self, v )
self.balance = self.balance + v;
return self.balance;
end
function new( )
return setmetatable( { balance = 0 }, h4x0r.Account )
end
setCurrentEnvironment( h4x0r );
end
end
return h4x0r
I then attempted to use the following script to access the Account class, assuming that all of the members of the 2nd do-end block would be assigned to h4x0r.Account:
h4x0r = require( game.Workspace.h4x0r );
Account = h4x0r.Account;
account = Account.new( );
print( account:withdraw( 100 ) );
The above script fails with the error Workspace.Script:5: attempt to call method 'withdraw' (a nil value), so it must be an issue regarding the line where I set the __index field of h4x0r.Account.
Can someone please explain to me where I went wrong?
Try using getfenv(2) and setfenv(2, t) instead of getfenv(0) and setfenv(0, t). You essentially want to change the environment of the encapsulating function, which would be stack level 2.
0 is a special argument that would instead get or set the environment of the thread, which is used as a default environment in some cases, but that does not affect the individual closures that have already been instantiated in the thread, hence it doesn't work in this case.
I'm using OO abstraction in LUA and I'm having trouble with tables inside objects.
I'm using this approach to implement OO support: Object Orientation Tutorial
If i define a table inside the Class like this:
fields = {}
It will be shared by all instances of that Class. Something like an static attribute in Java. However, I'd like to get a normal class attribute instead.
That sounds odd to me, since it works correctly for non-table items like this:
attribute = 0
That is not going to be shared between all instances.
Here is a complete example:
local function C1Constructor( self )
print( "C1: Constructor... " )
self.fields = {}
end
local function C1SetName( self, name )
self.name = name
end
local function C1Add( self, name, value )
print( "C1: (" .. self.name .. ") :: Add: " .. name .. " = " .. value )
self.fields[ name ] = value
end
local function C1Show( self )
print( "C1: (" .. self.name .. ") :: Show:" )
for name, value in pairs( self.fields ) do
print(" " .. name .. " = " .. value )
end
end
C1 = {
name = "",
constructor = C1Constructor,
setName = C1SetName,
add = C1Add,
show = C1Show,
fields = {},
}
function C1.new( o )
o = o or { }
setmetatable( o, { __index = C1 } )
o:constructor()
return o;
end
c1a = C1.new()
c1b = C1.new()
c1a:setName( "Obj A" )
c1b:setName( "Obj B" )
c1a:show()
c1b:show()
c1a:add( "k1", "v1" )
c1b:add( "k2", "v2" )
c1a:show()
c1b:show()
I know how to fix it. But I don't know what is happening behind the scenes:
local function C1Constructor( self )
print( "C1: Constructor... " )
self.fields = {}
end
What am I missing here?
Thanks very much,
Tables are referenced values. When you store a table in a variable you are storing a reference to the table and not a primitive value (like for a string or a number). As such if you create one table and share it among instances you only have one table. When you create a table per-instance (e.g. in your constructor) then you have one table per-instance and not a shared table.