w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
  Home » LUA » Page 1
Lua (Lapis) random values returns the same result even using randomseed
I found that this is "feature" of lua to generate random using seconds. Use this link to read more: http://lua-users.org/wiki/MathLibraryTutorial But the solution which can help is to user /dev/urandom The link to describe: http://lua-users.org/lists/lua-l/2008-05/msg00498.html In my case example: local frandom = io.open("/dev/urandom", "rb") local makeString makeString = function(l)

Categories : Lua

how to extract the 4 bytes of a 32bit int in lua
You can use bit or bit32 libraries (included in Lua 5.2+ and LuaJIT and available as modules for 5.1). You can also use the reverse operations to what you already have: print(math.floor(num / 2^24), math.floor((num % 2^24) / 2^16), math.floor((num % 2^16) / 2^8), num % 2^8)

Categories : Lua

Lua gsub second instance
You can replace the first instance with something else (assuming the replacement is not present in the string itself, which you can check), then replace it back: print(mystring:gsub("% ", "1",1):gsub("% ", "/",1):gsub("1","% ", 1)) This prints: Thats a/really nice house.. Also, you don't need to escape spaces with %.

Categories : Lua

Using Lua libraries from within tup
According to the documentation require is not available: "The base functions are defined, excluding dofile, loadfile, load, and require." It seems like you may be able to implement your own "require" based on tup.include, which "Parses and runs the Lua file at path".

Categories : Lua

Failed to back to Menu from Game Over . Score unable to display on game over screen
I suggest to make another composer scene for gameOverScreen (just like you did with these code block you post). Then do a composer.gotoScene(gameOver) read more here. And for the score I suggest that you make something like this.

Categories : Lua

Error handling in Makefile
As @siffiejoe says in the comment. $(error) is make function and is run when the recipe as a whole is being evaluated (you can think of it like hoisting if that helps). So as soon as the recipe needs to be run (and the first line executed) the $(error) call is evaluated. Note: In the shell X && Y || Z is not a ternary operation. Z will be run if X succeeds and Y fails as well as when X f

Categories : Lua

Redis: Expand key references in values to their values
I don't know Redis but perhaps this works for you: local T=[[ partial:1 => {id: 1, title: 'Foo1', ...} partial:2 => {id: 2, title: 'Foo2', ...} partial:3 => {id: 3, title: 'Foo3', ...} page:home => { id: 'Homepage', teaser1: 'partial:1', teaser2: 'partial:2', teaser3: {type: Slider, content: 'partial:3'} } ]] local D={} for k,v in T:gmatch("(%w+:%w+)%s*=>%s*(%b{})") d

Categories : Lua

How to make a weighted RNG in ROBLOX Lua? (Like CS:GO cases)
I don't think so, but it's easy to write yourself: function(weights) local sum = 0 for _, v in next, weights do if v < 0 or math.floor(v) ~= v then error "Weights must be non-negative integers" end sum = sum + v end sum = math.random(sum) for k, v in next, weights do sum = sum - v if sum <= 0 then return k end end error "Should not happen."

Categories : Lua

How does Lua 5.x represent sparse arrays?
It depends on which version of Lua you use. In Lua 4, tables are implemented strictly as hash tables. In Lua 5, tables are part hash tables and part arrays, see the Lua Implementation where Section 4 covers tables and sparse arrays. The array part tries to store the values corresponding to integer keys from 1 to some limit n. Values corresponding to non-integer keys or to integer keys outs

Categories : Lua

How to fix this unpack issue?
This is standard Lua behavior: in an enumeration of function calls separated by commas, only the last one can return multiple results. For instance: > function f() return 1, 2, 3 end > print(f(), f()) 1 1 2 3 If I were you I would do the simple thing and use a for loop.

Categories : Lua

Lua attempt to index ? (a nil value)
shot in the dark, but does players[0].getColony() return the string '0'? because that'd print out in the lua interpreter as 0, but would definitely not index into the table as zero. example of what i'm talking about below: local t = '0' print(t) -- below prints exactly the same as variable t above local u = 0 print(u) local temp = { [0] = true } -- try to index into the temp table with '0' print

Categories : Lua

Access internal storage specific folder in Corona SDK
The following code worked. However, the sdcard actually means internal storage in the app. Also the middle two lines is necessary for the code to work even though the fourth line refers to first line directly. filePath = "/sdcard/coordinates.txt" local file = io.open( filePath, "r" ) local contents = file:read( "*a" ) for line in io.lines(filePath) do ii=ii+1 p

Categories : Lua

How would I go about making my own spherecast function?
I would probably start to implement it something along the lines of this: function spherecast(radius, centre, objects) local hits = {} for i, obj in pairs(objects) do local d = radius + obj.size local dx = obj.x - centre.x local dy = obj.y - centre.y local dz = obj.z - centre.z local sqrdist = dx * dx + dy * dy + dz * dz if sqrdist <= d * d then

Categories : Lua

What is the return type of HGETALL in Lua
Some quick searching online seems to suggest that the result from HGETALL (and other functions which return key/value pairs) is a table of sequential key/value pairs. So {"key1", "val1", "key2", "val2"}. This would indicate that the empty result (given that it isn't nil) would be an empty table (i.e. session[1] == nil).

Categories : Lua

How do i add a button to back to Main Menu (another .lua file)?
Make sure the scene is called "menu.lua" when you try to use the composer.gotoScene("menu") First make sure you require the libraries at the top local composer = require( "composer" ) local widget = require( "widget" ) Then add a listener that will handle the button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then composer.gotoScene ("menu")

Categories : Lua

Lua How to replace 1 specific character inside table without whole string?
In Lua, strings are strictly immutable, so they cannot be changed, per se. As such, the only way to accomplish this is to create a new string with the content you want and insert it in table[1]. Whether or not the string is inside a table does not matter. This would be accomplished, for example, by taking the beginning and end of the string and concatenating them with the new part: local index

Categories : Lua

Why does Corona give an error loading code that loads fine with lua?
maybe Corona uses older version of Lua which does not support this syntax. You can try workaround instead of building.transmitter:[operation](player, unpack({...})); you can call building.transmitter[operation](building.transmitter, player, unpack({...}));

Categories : Lua

Lua: Read table parameter from c function call
lua_getfield(L, 1, "x") //pushes a value of t["x"] onto the stack lua_tonumber(L, -1) //returns the value at the top of the stack lua_getfield(L, 1, "y") //pushes a value of t["y"] onto the stack lua_tonumber(L, -1) //returns the value at the top of the stack

Categories : Lua

attempt to call method 'insert' (a nil value) when i restart game
Don't add directly to the group view. Make a separate display group for the objects local objectGroup = display.newGroup() group:insert(objectGroup) by doing it this way, you are not obligated to kill out the group when you change scene (if you are using composer or storyboard). It will clear it self in the memory when you change scene.

Categories : Lua

Lua Compiling Error 'do' expected near '['
Expressions like while {}[1] do and if {}[1].parentFolderName then are invalid because of {}[1] reference. It needs to be ({})[1]. It's probably a result of some sort of automated processing, but you should be able to fix it manually.

Categories : Lua

Showing Text for a sertain amount of seconds in lua
The solution is simple: (1) show the logo, (2) wait for three seconds, (3) hide the logo. If you know how to do steps 1 and 3, then for step 2 you have a couple of options. If you have access to a library like luasocket, you may use its select call that will wait for a number of seconds: socket.select(nil, nil, 3) If this library is not available, you can do a "busy" loop: local start = os.ti

Categories : Lua

How to merge clib functions into a table using LuaJIT and FFI?
FFI library objects don't support iteration; you can't run pairs over them. You'll have to write an assignment for each function manually. Also keep in mind that it's faster to access C functions directly from the library object, rather than storing them in a table (or even a local variable) and accessing them there. See the last section of the FFI tutorial.

Categories : Lua

How to initialize a table with tables in Lua?
In the function Cell:new, self is the table Cell itself, so you are returning the same table every time. A simple fix is to create a new table instead: function Cell:new(i, j) local t = {} t.x_min = (i-1) * CELL_SIZE --omit the rest return t; end For future improvement, you may be interested in another way, which implements prototype: function Cell:new(i, j) local o =

Categories : Lua

Minecraft Rs Generator issue (attemt to index ? (a nil value)) (lua language)
The only problem i can see with your code that would allow for that error, is if you cannot wrap the peripheral. Make sure that it's actually connected on the back of the computer. If you're connected to the energy cell by wired modem, when you right click on the modem attached to the energy cell you should get a message like "Connected thermalexpansion_energycell" or whatever, and that thermal

Categories : Lua

How do I make os.pullEvent not yield?
You can easily use the parallel api to run both codes essentially at the same time. How it works is it runs them in sequence until it hits something that uses os.pullEvent and then swaps over and does the other side, and if both stop at something that does os.pullEvent then it keeps swapping between until one yields and continues from there. local function getClick() local event,side,x,y = os.p

Categories : Lua

Lua Wireshark Scripting
I think there is some confusion about parameters; according to the Dissectors page: "When called by Wireshark, they are passed (1) a TVB buffer of the data (a Tvb object), (2) a tree root (TreeItem object), and (3) a packet information record (Pinfo object)." From the example on the same page: function trivial_proto.dissector(buffer,pinfo,tree). It appears that buffer is Tvb object, but pkt is Pi

Categories : Lua

(Lua) Condition statements not behaving as expected
I think there are two main issues. I don't see the function mRandom, but I assume it returns a number and you are comparing it with a string. Number 2 is not the same as string '2', so 2 == '2' returns false. The second issue is that (it seems) you are trying to compare if either of variables has a specific value, but you can't do if a or b == 2 then meaning: execute if either a or b equals 2. Fo

Categories : Lua

Assigning Lua Script to a Button - Logitech G500s
if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then --set flag for mb1 mb1_pressed = true elseif event == "MOUSE_BUTTON_RELEASED" and arg == 6 then --set flag for mb1=false mb1_pressed = false end This will set mb1_pressed when mouse button is pressed and unset it when it is released. So, the variable will only be true while the button is pressed. If you want to toggle the m

Categories : Lua

attempt to index local 'f2' ERROR in lua
f2 is nil because io.open failed. Use this to see the error message: local f2 = assert(io.open(path, "r")) However, note that the previous call to io.open destroyed the file you're trying to read.

Categories : Lua

How in Corona make distinction between click and long click?
You can achieve it by creating 2 event handlers: One for tap event for moving to the next scene One for touch event to show some data. In the touch event handlers, have a condition checking the current phase of the event so that you show the data on "began" phase, hide the data on "ended" phase Example: function object:touch( event ) if event.phase == "began" then -- Show data

Categories : Lua

Non numeral indeces and the # never counts?
Yes, you can count on that (in lua 5.1). From the lua reference manual: The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte). The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil,

Categories : Lua

Trouble making a heart symbol in Lua?
For ASCII, only range 0x20 to 0x7E are printable. Others, including 0x03, isn't printable. Printing its value would be up to the implementation. If the environment supports Unicode, you can simply call: print("♥") For instance, Lua Demo outputs ♥, same in ideone.

Categories : Lua

Retrieve table name
You can iterate over the inner table using pairs: for k in pairs(UnitScanDB.profiles) do -- handle k end Using the above loop, you could, for example, copy all of the keys into a new table: local names = {} for k in pairs(UnitScanDB.profiles) do table.insert(names, k) end

Categories : Lua

Stuck with I/O operations in lua
I think the issue is that you never write to the score file: io.write (highScore) should be f1:write(highScore) and you should close the file as soon as you no longer need it (will prevent loss of data due to a crash, e.g.). Also don't forget to use system.pathForFile( filename, system.DocumentsDirectory) as described in Corona docs, so the file is put in the correct location.

Categories : Lua

Why is my matrix value nil?
grid[row].col = testCircle This line is the problem, grid[row].col is equivalent to grid[row]["col"], which is obviously not what you want, change it to: grid[row][col] = testCircle

Categories : Lua

Why does this cause a C stack overflow?
Deep Recursion. Inside the call to the metamethod __newindex, self[key] = value calls the metamethod __newindex again, recursively, until stack overflow.

Categories : Lua

Is possible in any way in Corona/Lua to take advantage of multiple cores on mobile device?
No. For the time being at least, Corona is single-threaded. I guess you could try using Corona Enterprise thing to hook up with native iOS & Java, but you would have to "cook things yourself".

Categories : Lua

When instantiating multiple instances of a lua object when to use require vs new?
require is a function for loading modules, not instantiating classes. require("mary") only loads mary.lua once, then stores the return value inside of package.loaded["mary"]. All subsequent results of require("mary") return the object at package.loaded["mary"], and do not create new instances. This makes require unsuitable for class instantiating. local r = require("mary") local s = require("mar

Categories : Lua

io.read in Lua doesn't work
Not that I know the least bit about Lua, so it's a wild guess, but shouldn't that be: function playernames () os.execute ("cls") io.write ("Player 1, choose a name: ") playername1 = io.read () io.write (" ") os.execute ("cls") io.write ("Player 2, choose a name: ") playername2 = io.read () io.write (" ") end

Categories : Lua




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.