Lua: converting from float to int
You could use math.floor(x) From the Lua Reference Manual: Returns the largest integer smaller than or equal to x.
You could use math.floor(x) From the Lua Reference Manual: Returns the largest integer smaller than or equal to x.
Here a short comparison on pytorch and torch. Torch: A Tensor library like numpy, unlike numpy it has strong GPU support. Lua is a wrapper for Torch (Yes! you need to have a good understanding of Lua), and for that you will need LuaRocks package manager. PyTorch: No need for the LuaRocks package manager, no … Read more
Use os.exit() or just return from some “main” function if your script is embedded.
the general case of iterating over an array and removing random items from the middle while continuing to iterate If you’re iterating front-to-back, when you remove element N, the next element in your iteration (N+1) gets shifted down into that position. If you increment your iteration variable (as ipairs does), you’ll skip that element. There … Read more
Try this: — http://lua-users.org/wiki/FileInputOutput — see if the file exists function file_exists(file) local f = io.open(file, “rb”) if f then f:close() end return f ~= nil end — get all lines from a file, returns an empty — list/table if the file does not exist function lines_from(file) if not file_exists(file) then return {} end local … Read more
You guessed right, table variables are passed by reference. Citing Lua 5.1 Reference Manual: There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. …. Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and … Read more
Use io.write instead print, which is meant for simple uses, like debugging, anyway.
Game engines that use Lua Free unless noted Agen (2D Lua; Windows) Amulet (2D Lua; Window, Linux, Mac, HTML5, iOS) Cafu 3D (3D C++/Lua) Cocos2d-x (2D C++/Lua/JS; Windows, Linux, Mac, iOS, Android, BlackBerry) Codea (2D&3D Lua; iOS (Editor is iOs app); $14.99 USD) Cryengine by Crytek (3D C++/Lua; Windows, Mac) Defold (2D Lua; Windows, Linux, … Read more
local keyset={} local n=0 for k,v in pairs(tab) do n=n+1 keyset[n]=k end Note that you cannot guarantee any order in keyset. If you want the keys in sorted order, then sort keyset with table.sort(keyset).
Looking at the code of string.format, I don’t see anything that supports boolean values. I guess tostring is the most reasonable option in that case. Example: print(“this is: ” .. tostring(true)) — Prints: this is true