1,441 questions
0
votes
0
answers
148
views
How do I create larger structs using tables in lua? (in the pico-8 game engine)
I am making a card game and im now revamping how individual card information is stored.
a single card would have the following values:
x, y, suit, number, state
this is to be stored in a deck array ...
1
vote
1
answer
100
views
How to iterate through the non-numeric indices of a table without using the pairs or next functions? [closed]
So I have the following table:
local table_arr = {
a = 3,
b = 42,
c = 10
}
And my for loop:
for x=1, #table_arr do
local key, value = table_arr[x], arr[ table_arr[x] ]
print(key) -...
2
votes
3
answers
107
views
Why do these Lua array initialization constructs have differing semantics?
As I understand Lua, an array is just a table where the keys start at 1 and increment sequentially. However, there are features within Lua to manipulate arrays in the traditional sense (e.g. ipairs, ...
1
vote
1
answer
64
views
Lua returns only empty tables
I have a code:
function subsets(t)
local res={}
local res_t={}
function subs(i)
if i>#t then
res[#res+1]=res_t
return
end
res_t[#res_t+1]...
0
votes
2
answers
93
views
Change brick color repeatedly
I'm trying to make a dance floor that lights up and changes colors periodically. So far I've gotten the game to load and pick a random color from the array upon loading, but I can't make the tiles ...
0
votes
0
answers
98
views
Lua table index changes its values without assigning any to them
Lua table (ref) index changes its values without assigning any to them.
Why does ref table change its values after assigning it to tab[c] or using string.match()? The actual behavior is that the ref ...
-1
votes
1
answer
78
views
How to block functions from being overwritten?
I program in Lua and I need some help:
I have file1.lua and file2.lua
In file1.lua:
loadstring('print("Hello World!")')
in file2.lua:
_loadstring = loadstring
function loadstring(code)
...
1
vote
0
answers
32
views
Is it possible to identify a table from a string variable? [duplicate]
I have this function that takes a string (s) and returns a table depending on what I entered.
function String2Faction(s)
if s == "Nomads" then
return Faction_Nomads
elseif s =...
3
votes
1
answer
80
views
table.insert not replacing the first nil position in the table
I started learning Lua recently, as far as I can tell, when I don't specify the index, the function table.insert tries to put the value in the first nil in the table.
Why then is it that in table t ...
0
votes
1
answer
483
views
Attempt to call missing method of table
I'm trying to make my first Roblox studio game and I ran into this problem:
attempt to call missing method 'UpdateCharacter' of table
attempt to call missing method 'Equip' of table
Any function ...
1
vote
1
answer
101
views
Lua equivalent for null conditional operator? [duplicate]
In lua, if we try to index a series of nested table (any of which can be nil), we might write code like this:
-- try to get a.b.c
local ret
if a then
local b = a.b
if b then
ret = b.c
...
1
vote
1
answer
56
views
Any way to combine two tables in lua?
Like I got two tables:
{["a"] = "aaa"} and {["b"] = "bbb"}
And make it into one table: {["a"] = "aaa", ["b"] = "bbb"}
I ...
2
votes
1
answer
30
views
Value not changing between tables
Here's code for a timer:
local copyTable = require("Helpers.copyTable")
local timer = {}
timer.max = 0
timer.time = 0
function timer:add(dt)
timer.time = timer.time + dt
end
function ...
3
votes
2
answers
151
views
Repacking or printing unpacked Lua tables
Why does the table.unpack() function print the unpacked table only if there is nothing following the function?
> print(table.unpack({1,2,3}))
1 2 3
> print(table.unpack({1,2,3}),&...
1
vote
1
answer
81
views
Getting the key a value was assigned to in a table in lua
So I'm trying to make a table of all the creatures in a game, each creature consisting of a table with their stats. I used the creature's name as the key in the first table, but now I need to get that ...
2
votes
1
answer
52
views
How to table.remove() nested elements in lua?
a={ {11,22,33} }
How to remove a[1][1] aka 11 with table.remove() so it becomes a={ {22,33} }?
In the pico8 lua it is del(a[1],a[1][1])
I have no idea what table.remove(a[1],a[1][1]) is doing, it ...
0
votes
1
answer
83
views
How can I parse a LuaTable in C#?
I am at a complete loss with the syntax to parse a LuaTable. I have a table that follows the following structure:
mission =
{
["coalition"] =
{
["blue"] =
...
1
vote
1
answer
233
views
Creating separate lua tables for each object in an array
So I have an array of creatures from a game, each one is just the name as a string. I want to iterate over each one and create a table for each one that contains all of the creatures stats, hp, damage,...
1
vote
1
answer
64
views
Performance of calling a function, passing multiple return values from another function call?
A function f() returns multiple values, which are to be fed into a second function g(). I cannot change f(), but I can change g() and all calls to it. I'm wondering if it's possible to say which ...
4
votes
2
answers
154
views
Why does `for key, value in table` stop working in Lua 5.1 and later?
The source code for every Lua release is available in one handy package, which can be extracted and the versions can be compiled all at once:
wget https://www.lua.org/ftp/lua-all.tar.gz
tar xvf lua-...
2
votes
0
answers
105
views
Can I change the default hash function?
I is possible to change the default hash function that lua (or luajit) uses for the HashTable part of a talbe ?
I want to speedup table access in pure Lua.
I saw that:
The Default hash function for ...
2
votes
1
answer
123
views
Lua table.sort invokes the comparing function with the same element
I have stumbled upon a strange behavior of table.sort(), in which the comparing function is invoked by passing the same array element in both parameters.
Consider the code below. I want to sort the ...
4
votes
2
answers
453
views
Is there a way to use <const> inside a Lua table?
Since Lua 5.4, the <const> syntax lets us set const variables. I noticed that this doesn't transitively affect fields inside a table.
local x <const> = {
a = {1,2,3},
b = {5,6,7}
}
...
0
votes
1
answer
93
views
Roblox Studio Lua: Variable changing without being referenced
This is my module script:
local phases = {}
local function getRoleAssignments(playersToAssignRoles, roles)
--Assigns a random player to each role then removes player from list so player can't ...
2
votes
1
answer
171
views
How can I convert a String that is formatted like a table, into a table?
I did find a version of this for a string into a table that splits up the letters of the string, but I have a table, that I turn into a string to remove the first part of it, and now I want it back as ...
1
vote
1
answer
46
views
Comparing multiple values in a table makes it that it choses to compare only one randomly
I am making a tetris clone in love2d (lua) for practice. And what I am trying to do is to make the collins with the walls.
Note:the tetris pieces are made out of tiles that each one has a relative ...
1
vote
1
answer
82
views
Dual representation and proxies to achieve privacy in Lua
I am reading Programming in Lua, 4th edition with an intention to solve every exercise in that book. The last exercise in Chapter 21 p. 172 is the following (emphs are mine):
A variation of the dual ...
0
votes
1
answer
68
views
Avoid for table of table to mantain same memory allocation when inserted to another table [duplicate]
In Lua 5.3, I have a table t1 in which there is only a table, the latter table consisting only of a key [key] pointing to the value 1.
t1 = {{key = 1}}
My goal is to copy and insert the inner table ...
2
votes
3
answers
2k
views
Lua: is there a need to use hash of string as a key in lua tables
I want to keep a list of words (let's say 20k). The only purpose of it is to check if the given word exists there. So, it's going be something known as a hash set.
-- dict here is just an example, it'...
1
vote
1
answer
70
views
How to reference first entry in keyless table in Lua?
I have a table defined like so:
local foo =
{
bar =
{
{ ['baz'] = "A sample string", ['qux'] = 128 },
},
}
Obviously, the is a small part of a much larger algorithm. I'...
-1
votes
1
answer
100
views
How to make table from io.read in lua
I want to make a Table in lua with io.read, This is my code:
local members = {}
io.write("How many members you wanna add? ")
local memberNum = io.read("n")
print("Add new ...
0
votes
1
answer
153
views
Is it possible to use a variable as a field name in a lua table?
I am new to Lua and am writing a program for a tool changer on my CNC machine. Each tool pocket's detail is stored as an entry in a table, the table looks like:
local PocketDetails = {
...
2
votes
1
answer
79
views
Lua table created in C returns strange values for integers
I want to create a nested table, that shall look like this:
{
SA_1: {1,2,3,4},
SA_2: {11,22,33,44}
}
The code I have (I changed the SA_2 to strings for testing purposes):
int myRegisteredCfunc:
{
...
2
votes
1
answer
109
views
How to remove elements inside a table based on a value of those elements?
Iam trying to remove elements in a table which have a certain value
test = {
{
id = 473385593,
deleted = true,
},
{
id = 473385619,
deleted = true,
},
...
0
votes
1
answer
334
views
How to get the position of a moving object once? without it being updated
I have an object moving "freely" with body:applyForce( x, y ).
I want to track it's previous position.
I set a timer and with every second that goes by i get the position of the moving ...
3
votes
1
answer
188
views
How do I iterate through a table starting at a key position in lua?
I am trying to iterate through a table but start from a position in the table using a key.
list = {
"one",
"two",
"four",
"five",
"six"
}
...
0
votes
0
answers
108
views
Lua: Handle dynamic nested table (get and set)
I have a table t which is totally dynamic: it is nested, and number of levels is unknown in advance. I wish to be able to set (get is easy to do) some value at some level, using a kind of path. The ...
1
vote
1
answer
124
views
How to concatenate matrices in lua with wrapping
im not sure if the title is the correct question, i have very little programming knowledge but i know exactly what i want to accomplish, not how to ask what to do, so ill just give an example.
mX = {
{...
0
votes
0
answers
22
views
Pulling GroupId from moduleScript
I'm working on a Roblox overhead GUI and I'm using a moduleScript for the values. It currently pulls a random value of its liking instead of abiding to the parameters.
The (relevant) part of the ...
0
votes
1
answer
63
views
How to save a function of an Object/table to a variable to be called from this variable in Lua?
I'm experimenting with Lua OOP (Object Oriented Programming) but I'm experiencing some issues.
Here is a sample code with comments:
Object = {string = "Default"}
function Object:new()
...
0
votes
1
answer
101
views
Lua sorting a table twice by different fields
Below is a sample table structure of what I am wanting to sort.
Data = {
["C1_RN1::F1"] = {
["class"] = "Hunter",
["name"] = "C1",...
2
votes
3
answers
106
views
Manipulating table key
This table is structured like this:
local t = {
["track"] = "one#two#three",
{
["track"] = "four"
}, -- [1]
}
The first ...
2
votes
1
answer
73
views
How to check in Lua if there are multiple elements in a table?
I made a program, which checks if in table has (for example) "A" and a "B" in itself. But it doesn't works.
Here's code:
function inTable(t,e)
return t[e] ~= nil
end
--Example
...
0
votes
1
answer
210
views
Custom foreach implementation in Lua
I am trying to implement a custom iterator which works like table.foreach in Lua, but in more "syntax friendly" manner. I came across following construction (not sure if it's done purely in ...
0
votes
0
answers
105
views
How to add an empty table to an existing table in Lua using its C-API?
The core detail of my problem is: I don't know how to do it.
I encountered the problem I'm trying to solve by wanting to add a table to an existing table in Lua.
The major difficulty that prevented me ...
0
votes
1
answer
190
views
Table value returns nil when it should not be
My table:
local Cache = {
SurvivalGameFramework = {
parent = game.ServerStorage
}
}
As you can probably tell, this table is not an empty value or "nil" but according to ...
0
votes
1
answer
356
views
How do I find the index of an element in a table in Lua? [duplicate]
Let's say I have this code:
local testtbl = {"foo", "bar", "baz"}
How do I get the index of an element?
For example:
print(indexOf(testtbl, "bar")) -- 2
1
vote
2
answers
81
views
What am i missing on my table creations / returns that they are nil?
I'm currently learning and trying to write a script for a map i created for a pretty old game (Settlers 5 Heritage of Kings) and while doing so i tried to split up my code in different files for ...
1
vote
1
answer
119
views
Read both key and values of a nested lua script in C/C++
I've the following lua script, in which I use a C++ function that needs to read both keys and values of a nested table:
local scenariolist = {
scenarios = {
'scenario1',
'scenario3',
...
0
votes
1
answer
140
views
Lua JSON unexpected type `function`
I have following code for DCS:World scripting:
koUDPSocket = {}
koUDPSocket.host = "127.0.0.1"
--koTCPSocket.host = "85.221.224.254"
koUDPSocket.port = 52525
koUDPSocket.socket = ...