How to delete class variable in Torch? - lua

I have problems in understanding how a class variable in Torch works.
I did the following:
mydata=torch.class('something')
I checked the User variable by typing who() and it shows:
== User Variables ==
[_RESULT] = table - size: 0
[mydata] = table - size: 0
[something] = table - size: 0
I first of all tried to delete mydata by
mydata=nil
it works. mydata is now freed and can be reinitialized to any values. But when I tried to delete the variable something by typing
soemthing=nil
It seems it's not working even though the variable something was not list in who() anymore. When I try:
mydata2=torch.class('something')
the error pops out:
/data/torch/install/share/lua/5.1/torch/init.lua:65: something has been already assigned a factory
stack traceback:
[C]: in function 'newmetatable'
/data/torch/install/share/lua/5.1/torch/init.lua:65: in function 'class'
[string "mydata2=torch.class('something')"]:1: in main chunk
[C]: in function 'xpcall'
/data/torch/install/share/lua/5.1/trepl/init.lua:648: in function 'repl'
/data/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
[C]: at 0x00406670
Could anyone tell me the reason behind it?

torch.class() stores the class metatable in the lua-registry, see http://www.lua.org/pil/27.3.1.html and the luaT_lua_newmetatable() function in the torch C-backend.
In order to unregister an existing class it is necessary to remove the entry from the lua-registry. You can access the registry from lua with help of the debug.getregistry() function.
With removeal from registry your example works:
mydata = torch.class('something')
mydata = nil
soemthing = nil
-- remove the global registration:
debug.getregistry()['something'] = nil
-- now it is possible to register the class again
mydata2 = torch.class('something')

Related

Error with push.lua from CS50 game course

I have a problem with the folder "pong-1" that contains "main.lua" and "push.lua". When I select them into "LÖVE", it says:
>Error
push.lua:71: bad argument #1 to 'insert' (table expected, got number)
[C]: in function 'insert'
push.lua:71: in function 'setupCanvas'
main.lua:12: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
I tried with the outdated version of push.lua (which offers the source code of the course) as well as the newest version I could find, but neither of them worked. Both display the same error message. What could be the problem?
function push:setupCanvas(canvases)
table.insert(canvases, { name = "_render", private = true }) --final render
self._canvas = true
self.canvases = {}
for i = 1, #canvases do
push:addCanvas(canvases[i])
end
return self
end
This functions expects canvases to be a table value.
You provide a number value VIRTUAL_WIDTH in your function call
push:setupCanvas(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH,
WINDOW_HEIGHT,{ fullscreen = false, resizable = false, vsync = true })
instead.
Looks like you're confusing setupCanvas with setupScreen

Attempt to call global "Sicherheitskreis" (a nil value ) stack traceback

I'm trying to secure the doors of my CNC with switchs that send a Signal to my Laptop (I got Mach 4 on it). I created this Code that should make the Spindle stay still if the doors aren't locked, but I always get an error that says :
[string""]1576 attempt to call global 'SicherheitsKreis' (a Nil value) stack traceback:
I've tried to move the Code around and read topics on this, but Nothing works.
Does someone have a solution ?
Here's all my Code :
function SicherheitsKreis(Schliesserstate, Oeffnerstate)
if (Schliesserstate ==0 and Oeffnerstate ==1 ) then
mc.mcSpindleSetDirection(inst,0)
elseif (Schliesserstate == 1 and Oeffnerstate == 0 ) then
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGet State(sigh);
if (sigState == 1) then
mc.mcSpindleSetDirection(inst,0)
else
mc.mcSpindleSetDirection(inst,1);
end
else
mc.mcSpindleSetDirection(inst,0)
end
end
if (mc.mcInEditor() == 1) then
SicherheitsKreis()
end
The Code that I'm using to call SicherheitsKreis is :
local inst = mc.mcGetInstance()
local hsigSchliesser = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT8);
local hsigOeffner = mc.mcSignalGetHandle (inst, mc.ISIG_INPUT9);
local Schliesserstate = mc.mcSignalGetState(hsigSchliesser);
local Oeffnerstate = mc.mcSignalGetState(hsigSchliesser);
SicherheitsKreis(Schliesserstate, Oeffnerstate)
This Script is typed in Mach 4 and the function is saved as a m function (nach4 has free m function that the user can customize) in the Memory of Mach 4 (for my Computer it is m146)
Your code:
function SicherheitsKreis(Schliesserstate, Oeffnerstate)
...
end
First possibility is that code is in some other function or other element, so it is not global. Second possibility is that you run SicherheitsKreis(Schliesserstate, Oeffnerstate) before loading this part of code. Third (very uncommon) is that you override it by SicherheitsKreis = nil or equivalent. There is no other possibilities.

Store function to indexed arrays and call him with undefault param

I'm trying to create and indexed array of functions to call him with changed params, like this:
local function wubba(lubba)
return lubba
end
local dub = {
["wubba"] = {func = wubba(lubba)}
}
print(dub["wubba"].func("hi"))
But in all my tries i got errors, i can't figure out how to do it. Anyone can help me?
lua: wubba.lua:9: attempt to call field 'func' (a nil value)
stack traceback:
wubba.lua:9: in main chunk
[C]: in ?
Solved, just not to store with params:
local dub = {
["wubba"] = {func = wubba}
}

Getting access to parameter inside a string being executed with 'load'

I would like to know if there's a way for the 'load' function to get a variable value from a local variable instead of a global variable ?
Say that I've got a string like this 'trade.version == 2' that I want to execute with the 'load' function inside a function taking the trade as parameter.
function doTest( trade, test )
-- inside the string 'test', I would like that any reference to 'trade'
-- refer to the function parameter instead of a global variable
if ( assert(load("return "..test))() ) then
-- do something
end
end
a_userdata = { version = 2 }
-- Calling the function above
doTest( a_userdata , "trade.version == 2" )
[string "return trade.version == 2"]:1: attempt to index global 'trade' (a nil value)
stack traceback:
[string "return trade.version == 2"]:1: in main chunk
stdin:2: in function 'doTest'
stdin:1: in main chunk
[C]: in ?
As a workaround, I have defined a global variable and it's working pretty fine.
But I would like to avoid this global variable.
Thank you very much
function doTest( trade, test )
-- inside the string 'test', I would like that any reference to 'trade'
-- refer to the function parameter instead of a global variable
if assert(load("local trade = ...; return "..test))(trade) then
-- do something
print('Yes, version equals to 2')
end
end
a_userdata = { version = 2 }
-- Calling the function above
doTest( a_userdata , "trade.version == 2" )

String compatibility error, attempt to index a nil value

I am using Gideros and getting this error:
main.lua:47: attempt to index a nil value
stack traceback:
main.lua:47: in function 'func'
[string "compatibility.lua"]:36: in function <[string "compatibility.lua"]:35>
I have this piece of code and as soon as the text is displayed, it gives me the above mentioned error:How can I fix this?
function onEnter()
function youLoose()
local font2 = TTFont.new("billo.ttf", 20, "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
LooserText = TextField.new(font2, "You Loose , Try AGAIN?")
LooserText:setPosition(100, 100)
stage:addChild(LooserText)
Timer = Timer.delayedCall(1000, removing)
end --line 36
end
function removing()
LooserText:getParent():removeChild(LooserText) --line 47
end
The index nil error means that on that line you are probably getting nil as a return value from LooserText:getParent().
Why you would be getting nil for that I can't tell you other than presumably because it doesn't have one.
The documentation indicates that there is no error condition for Stage.addChild except that the object added must be a Sprite. TextField inherits Sprite so there is no apparent reason for you to get this error. However, you should not re-assign the return value of delayedCall to a global variable of same name as the Timer class, this could affect other parts of the application. Since you don't use the returned Timer instance, I have removed the assignment. Also, if the stage:addChild succeeded then the removing can use stage. One thing that is strange is that your onEnter just defines youLose() but does not call it or return it, is this part of code you ommitted? In any case, you need to add some sanity checks to verify that what you think is happening is really happening w/r/t child add/remove:
function onEnter()
function youLoose()
local font2 = TTFont.new("billo.ttf", 20, "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
LoserText = TextField.new(font2, "You Lose , Try AGAIN?")
LoserText:setPosition(100, 100)
print('Stage num children:' .. stage:getNumChildren())
stage:addChild(LoserText)
print('Stage num children:' .. stage:getNumChildren())
print('LoserText is stage child #' .. stage:getChildIndex(LoserText))
Timer.delayedCall(1000, removing)
end
end
function removing()
print('Stage num children:' .. stage:getNumChildren())
print('LoserText is stage child #' .. stage:getChildIndex(LoserText))
stage:removeChild(LoserText)
print('Stage num children:' .. stage:getNumChildren())
end

Resources