Attempt to index local 'tooltip' (a nil value) - lua

Please help. I'm trying to fix this code and I'm just not seeing the error. I am very inexperienced and appreciate any help you can give me. Thanks.
Error: UberInventory-6.8.lua:1325: attempt to index local 'tooltip' (a nil value)
Code - beginning at line 1320
function UberInventory_HookTooltip( tooltip )
-- From global to local
local UBI_Hooks = UBI_Hooks;
-- Store default script
local tooltipName = tooltip:GetName();
UBI_Hooks["OnTooltipSetItem"][tooltipName] = tooltip:GetScript( "OnTooltipSetItem" );
UBI_Hooks["OnTooltipCleared"][tooltipName] = tooltip:GetScript( "OnTooltipCleared" );
-- Set new script to handle OntooltipSetItem
tooltip:SetScript( "OnTooltipSetItem", function( self, ... )
-- From global to local
local UBI_Hooks = UBI_Hooks;
-- Get tooltip name
local tooltipName = self:GetName();
-- Call default script
if ( UBI_Hooks["OnTooltipSetItem"][tooltipName] ) then
UBI_Hooks["OnTooltipSetItem"][tooltipName]( self, ... );
end;
-- Call new script (adds the item information)
UberInventory_AddItemInfo( self );
-- Turn on UberInventory indicator
self.UBI_InfoAdded = true;
end );
-- Set new script to handle OnTooltipCleared
tooltip:SetScript( "OnTooltipCleared", function( self, ... )
-- From global to local
local UBI_Hooks = UBI_Hooks;
-- Get tooltip name
local tooltipName = self:GetName();
-- Force reset of fonts (maxlines is a custom attribute added within the UberInventory_AddItemInfo function)
if ( self.maxlines ) then
local txtLeft, txtRight;
for i = 1, self.maxlines do
txtLeft = _G[self:GetName().."TextLeft"..i];
txtRight = _G[self:GetName().."TextRight"..i];
if ( txtLeft ) then txtLeft:SetFontObject( GameTooltipText ); end;
if ( txtRight ) then txtRight:SetFontObject( GameTooltipText ); end;
end;
end;
-- Call default script
if ( UBI_Hooks["OnTooltipCleared"][tooltipName] ) then
UBI_Hooks["OnTooltipCleared"][tooltipName]( self, ... );
end;
-- Turn off UberInventory indicator
self.UBI_InfoAdded = false;
end );
end;
And here is the code from line 2074 to 2087 where "HookTooltip" is called
function UberInventory_Install_Hooks()
-- Hook the Tooltips (OnTooltipSetItem, OnTooltipCleared)
UberInventory_HookTooltip( GameTooltip );
UberInventory_HookTooltip( ItemRefTooltip );
UberInventory_HookTooltip( ShoppingTooltip1 );
UberInventory_HookTooltip( ShoppingTooltip2 );
UberInventory_HookTooltip( ShoppingTooltip3 );
-- Hook mail stuff
UBI_Hooks["ReturnInboxItem"] = ReturnInboxItem;
ReturnInboxItem = UberInventory_ReturnInboxItem;
UBI_Hooks["SendMail"] = SendMail;
SendMail = UberInventory_SendMail;
end;

The function you are calling (UberInventory_HookTooltip) gets a nil value as toolkit parameter. When you then try to call a method of that tookit object (tooltip:GetName()), you get an expected error as indicated: "attempt to index local 'tooltip' (a nil value)". The code tries to find a field GetName in the table that should be stored in tooltip and fails to do that (to "index" the table) as there value is nil. You need to check the code that calls the function to make sure it passes the correct value. It's not possible to give you any further help without seeing the code that calls UberInventory_HookTooltip.

Related

Metatables, attempt to call method 'rename' (a nil value)

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.

Setting __index of current environment in Roblox

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.

Passing data in functions, doing it wrong?

I'm using Corona SDK to create an Android/iOS app. And I'm trying to pass two different parameters in a function. The function is called like this:
function onCollision( self, event )
The problem is, when the function is called, it returns this error: attempt to index local "event" a nil value. I know why, I think it's because of the comma. But I've read documentation and that's how you're supposed to do it, any help?
If you give a table object and reference your function as part of the table it should work:
local object = display.newImage( "object.png" )
physics.addBody( object , { ... } )
local function onCollision( self, event )
...
end
object.collision = onCollision
object:addEventListener( "collision", object)
Your function should be function onCollision(event), self isn't needed.
If you actually want to pass another parameter to this function, you can do it using a closure like that:
local myParam = 1
Runtime:addEventListener ( "collision", function(event)
return onCollision(event, myParam)
end )

Not sure why extending/injecting an instance method in to a lua object isn't working

In this example, I'm using lunit and am attempting to inject an instance method in to an instance of LuaSocket and am failing to see why the following isn't working.
-- Using lunit for unit testing
local lunit = require('lunitx')
_ENV = lunit.module('enhanced', 'seeall')
local socket = require('socket')
-- connect(2) to the service tcp/echo
local conn, connErr = socket.connect('127.0.0.1', '7')
function conn:receiveLine(...)
local line, err = self:receive('*l')
assert_string(line, err)
return line
end
function conn:sendLine(...)
local bytesSent, err = self:send(... .. '\n')
assert_number(bytesSent, err)
return bytesSent
end
The error message I'm getting is:
attempt to call method 'sendLine' (a nil value)
?? This seems like there is something obvious happening here, but I'm missing the required detail.
Ass-u-me'ing getmetatable(conn).__index == getmetatable(conn) source of confusion.
In this case, conn's metatable's __index metamethod is pointing to a different table than expected, so method resolution isn't happening against the table at getmetatable(conn).
function setup()
-- Update the table at __index, not conn's metatable
local mt = getmetatable(conn).__index
function mt:receiveLine(...)
local line, err = self:receive('*l')
assert_string(line, err)
return line
end
function mt:sendLine(...)
local bytesSent, err = self:send(... .. '\n')
assert_number(bytesSent, err)
return bytesSent
end
end
I was able to tease this out by testing to see if __index was pointing to conn's metatable, which it wasn't:
assert_equal(getmetatable(conn), getmetatable(conn).__index)
Generically, if there is a __newindex handler on getmetatable(conn) that is intercepting new table entries, use rawset() on __index's table.

ZF2 retrieve out parameter from SP call

I have created a stored procedure with following declaration:
DELIMITER $$
DROP PROCEDURE IF EXISTS my_test$$
CREATE PROCEDURE my_test(input_number INT, OUT out_number text)
BEGIN
IF (input_number = 0) THEN
SET out_number='Errorrrr';
ELSE
SET out_number='Testing';
END IF;
END$$
DELIMITER ;
Following is my ZF2 code to call this SP:
$spResponse = 0;
$prepareStmt = $this->dbGateway->createStatement ();
$prepareStmt->prepare ( 'CALL my_test(?,?)' );
$prepareStmt->getResource ()->bindParam ( 1, $spRequest );
$prepareStmt->getResource ()->bindParam ( 2, $spResponse, \PDO::PARAM_STR, 2 );
$resultSet = $prepareStmt->execute ();
This code gives me following error:
Syntax error or access violation: 1414 OUT or INOUT argument 2 for routine zf2.my_test is not a variable or NEW pseudo-variable in BEFORE trigger
Can somebody advice where the issue is? Also, How can i retrieve value of "OUT" parameter.
Appreciate your response and help.
This low level code retrieves the base PDO connection object. This way you can work the results in PHP fashion

Resources