How to get an Array of Meshes - lua

myMesh = {}
myMesh[0].x = 30
Nope... doesn't work.
myMesh = Mesh.new{}
myMesh[0].x = 30
Nope... no go.
An array of meshes should be possible, but I don't know how. Can someone help? Thanks
Edit:
Thanks for your responses. Pardon me for this stupid mistake. I just realized, "What nonsense are you doing J?" I really wanted an array of vertices of the mesh, so that I can use loops to manipulate them. Sorry about that... although it didn't hurt to learn how to get an array of meshes.
So I figured it out, because I was sure I did this before, when I used gml.
ptx = {}; pty = {}; ptz = {}
ptx[1] = myMesh.x; pty[1] = myMesh.y; ptz[1] = myMesh.z;
Thanks for the help though. I also learned that lua doesn't use index 0
Edit: Wait. That doesn't work either does it?
Well for now this gives no error, so I'll see if it does what I want.
pMesh = {}
for m=1, 6 do
pMesh[m] = Mesh.new()
end
pMesh[1].x = 0; pMesh[1].y = 0; pMesh[1].z = 0
Thanks guys. If you have any other suggestions, I'm all ears.

myMesh = {}
myMesh[0].x = 30
Will cause an error for indexing a nil value.
myMesh = {} creates an empty Lua table.
doing myMesh[0].x is not allowed because there is no myMesh[0].
First you have to insert a table element at index 0.
myMesh = {}
myMesh[0] = Mesh.new(true)
myMesh[0].x = 30
myMesh is a stupid name for an array of meshes as it suggests it's just a single mesh.
Also in Lua we usually start with index 1 which will makes things a bit easier using Lua's standard table tools.
I'm not sure if
mesh = Mesh.new()
mesh.x = 30
is actually ok. Why would a mesh have an x coordinate? This is not listed in Mesh's properties in the manual.
Usually you would create a Mesh with multiple points And if you want an array of multiple meshes you would simply put those meshes into a table unless there is a particular user data type for this.

Try this:
myMesh = {}
myMesh[0]= Mesh.new{}
myMesh[0].x = 30

you need to initialize as a table and columns and rows:
myMesh = {}
myMesh[0] = {}
myMesh[0].x=30
or
myMesh = { [0]={} }
myMesh[0].x=30

Related

/Lua/ How to do this (idk how to call that lol)

I need to make a trolleybus number, which won't repeat for game. For example, there is a number "101" and there musn't be more "101". How to do that? I have a code, but I know, he won't work and I won't test it lol
function giveNumber()
local number = math.random(100, 199)
local takedNumbers = {}
local i = 0
local massiv = i+1
script.Parent.pered.SurfaceGui.TextLabel.Text = number
script.Parent.zad.SurfaceGui.TextLabel.Text = number
script.Parent.levo.SurfaceGui.TextLabel.Text = number
script.Parent.pravo.SurfaceGui.TextLabel.Text = number
takedNumbers[massiv] = {number}
end
script.Parent.Script:giveNumber() // what I wrote here? idk...
if number == takedNumbers[massiv] then
giveNumber()
end
i didn't test it, because I think it won't work because this code is something bad
I think this will serve your needs.
In the function generateUniqueNumber, the script loops until it found a number that is not yet in the array. (in other words, that it hasn't given out yet)
Once it found that number, it will insert it into the table to remember that it has given it out, and then it will return the number.
Then on the bottom of the script we just give the numbers to the buses :-)
--[[
Goal: Give all buses a unique number
]]
-- Variables
local takenNumbers = {};
-- This function returns a random number in the range [100, 199] that has not been taken yet
function generateUniqueNumber()
local foundNumber = false;
while not foundNumber do
randomNumber = math.random(100, 199);
if not table.find(takenNumbers, randomNumber) then
table.insert(takenNumbers, randomNumber);
return randomNumber;
end
end
end
-- This function sets the number of the bus
script.Parent.pered.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.zad.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.levo.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.pravo.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
2 things:
I didn't test this code as Roblox is not installed on the pc I'm currently on.
Please try formatting your code nicely next time. It greatly improves the readability! For example, you can use this website:
https://codebeautify.org/lua-beautifier
Simpler
Fill a table with free numbers...
local freenumbers = {}
for i = 1, 99 do freenumbers[i] = i + 100 end
...for every new takennumbers use table.remove() on freenumbers
local takennumbers = {}
if #freenumbers > 0 then
takennumbers[#takennumbers + 1] = table.remove(freenumbers, math.random(1, #freenumbers))
end

Lua, local variables in a table

Sorry in advance if this is an incorrect question. I'm fairly new to Lua and I'm not sure how to go about this. I want to access a variable stored in a table from a function variable.
As far as I know there is no self-referencing tables before constructed.
An example would be this:
local bigTable = {
a = {
foo = 0,
bar = function(y)
print(foo) --Incorrect
end
}
}
What would be the best approach for this situation?
What you want to do is to create a table first, and append the keys to it:
local a = {}
a.foo = 0
a.bar = function()
print(a.foo)
end
local bigTable = {
a = a
}
bigTable.a.bar() -- prints 0
local bigTable = {
a = {
foo = 0,
bar = function(self, ...)
print(self.foo)
end,
}
}
-- Somewhere else in the code...
bigTable.a.bar(bigTable.a) --> 0
-- or the shorter but (almost) equivalent form:
bigTable.a:bar() --> prints 0
I anticipate your next question will be "What does the : do?", and for that there's lots of answers on SO already :)
Note that there's potential for a performance improvement here: if the above code gets called a lot, the bar method will be created again and again, so it could make sense to cache it; but that's pointless unless the surrounding code is already fast enough that this one allocation would have a noticeable impact on its runtime.

Fighting a bit with lua table indexing

I am new to Lua, and would like to understand the following syntax:
init_state_global = some_integer
rnn_state = {[0] = init_state_global}
My Pythonic interpretation would be that the first element has index 0 and that the value of the element is equal to the variable init_state_global.
However, when I do
print(rnn_state[0])
I get
>> nil
Can someone can help me interpret this:
rnn_state = {[0] = init_state_global}
In Lua, you do it before the expression set(=) and after the array name. Also, Lua uses 1-based index
init_state_global = some_integer
rnn_state = {}
rnn_state[1] = init_state_global;
Perhaps you forgot to declare a variable:
init_state_global = 5
rnn_state = {[0] = init_state_global}
print(rnn_state[0])
it turns out it is easier than I thought.
Even though lua starts indexing at 1, you can set an index ad hoc to zero.
So
rnn_state = {[0] = init_state_global}
means just that,
rnn_state[0] = init_state_global
However, I stated above that
print(rnn_state[0]) was equal to
>> nil
that is because I forgot to declare the variable (in my code, not in the initial thread)
init_state_global = some_integer
:(
So if you declare the variable correctly, the following statement
print(rnn_state[0])
will return
>> some_integer
if you previously declared
init_state_global = some_integer
Another thing (may not be obvious to those of us used to python lists) is that
rnn_state = {}
rnn_state[0] = 4
is the same as
rnn_state = {[0] = 4}

Lua: Desired Results are Received Then Not Without Changing Code

Right now I'm trying to use Lua to receive variables from barcodes sent out from an outside source. When I run this, there is a variable rotation from the local function rot(input) that appears to be "buggy". If I run this code exactly as it is with the print statements below, the rotation will appear and disappear. Please help me understand why this may happen?
Please note: There are two aspects of this code that I'm currently working on. A) Code128 is not properly retrieving the variables. B)My code can definitely be shortened. But I'm new and learning as I go. The main purpose for this thread is to help me understand why code will sometimes display the desired result, then won't the next minute?
Thank you.
Edited: I've updated the code a bit to make it cleaner. Condensed all of my string.match statements into tables with other barcode related fields. Still learning and looking to make it even more cleaner. I love learning this, but am still having the same problem with my local function rot(input) and getting intermittent results. Any help is greatly appreciated!
local function rot(input)
rotTable = {["R"] = "cw", ["I"] = "180", ["B"] = "ccw"}
for k,v in pairs (rotTable) do
if input == k then
rotation = v
else
rotation = ""
end
end
return rotation
end
local function barCode(input)
local bcID = string.match(input,"%^(B%w)")
if bcID == "BY" then
bcID = string.match(input,"%^BY.*%^(B%w)")
end
local bcTable = {
["BC"] = {"code128", 10, string.match(input,"%^BY.*%^BC(%u),(%d*),(%u),%u,%u%^FD(.*)%^FS")},
["B2"] = {"bc2of5i", 20, string.match(input,"%^B2(%u),(%d*),(%u),%u,%u%^FD(.*)%^FS")},
["BE"] = {"ean13", 10, string.match(input,"%^BE(%u),(%d*),(%u),%u%^FD(.*)%^FS")},
["B8"] = {"ean8", 10, string.match(input,"%^B8(%u),(%d*),(%u),%u%^FD(.*)%^FS")},
["B3"] = {"code39", 10, string.match(input,"%^B3(%u),%u,(%d*),(%u),%u%^FD(.*)%^FS")},
["BU"] = {"upc_a", -1, string.match(input,"%^BU(%u),(%d*),(%u),%u%,%u^FD(.*)%^FS")}
}
for k,v in pairs (bcTable) do
if bcID == k then
bcFields = v
bcType, qzone, bcR, bcH, bcHr, bcData = unpack(bcFields)
end
end
hPos = 0
vPos = 0
bcOutput = '<'..bcType..' qzone=\"'..qzone..'\" hbb=\"0\" vbb=\"0\" bbwidth=\"1\" hpos=\"'..hPos..'\" vpos=\"'..vPos..'\" rotation = \"'..rot(bcR)..'\" bgcolor=\"0\" barcolor=\"255\" textcolor=\"255\" barwidth=\"1\" height=\"8\">'..bcData..'</'..bcType..'>'
return bcOutput
end
print(barCode("^BY3^BCN,102,N,N^FDCHF05000042^FS"))
print(barCode("^B2B,110,N,N,N^FD45681382^FS"))
print(barCode("^BUN,183,N,N,N^FD61414199999^FS"))
print(barCode("^B8I,146,N,N^FD212345645121^FS"))
print(barCode("^BEB,183,N,N^FD211234567891^FS"))
I'm not sure what is wrong with your code, if anything, but rot can be written more simply as
local rotTable = {["R"] = "cw", ["I"] = "180", ["B"] = "ccw"}
local function rot(input)
return rotTable[input] or ""
end
In general, you shouldn't need to search Lua tables. For instance, the loop for k,v in pairs (bcTable) do can be replace by indexing as in the code above.

Lua return index of a nested table

This is my first attempt to use Lua tables and I'm getting on with it quite well. I'm struggling with one thing though, heres (a small sample of) my table as it currently stands:
objects = {
["1/1/1"] = { tl = 1, startVal = 1, stopVal = 0 },
["1/1/2"] = { tl = 11, startVal = 1, stopVal = 0 },
["1/1/3"] = { tl = 22, startVal = 1, stopVal = 0 },
["1/1/4"] = { tl = 33, startVal = 1, stopVal = 0 },
}
The typical operation of this is that I use the "1/1/1" values as a lookup to the inner tables and then use those values in various functions. This all works well. Now, I need to go the other way, say I have tl = 22 coming in, I want to return the top value ("1/1/3" in this case).
I think I need to do something with the inpairs I keep seeing on the web but I'm struggling to implement. Any help would be massively appreciated.
You can't use ipairs because your table is an associated array not a sequence, so you have to use pairs. Also, there is no search function builtin to Lua, so you have to loop over all items yourself, looking for the right field value:
function findTL(tbl)
for key, data in pairs(tbl) do
if data.tl == tlSearch then
return key
end
end
end
local key = findTL(objects, 22)
If you want something a tad more object-oriented you could do this:
objects.findTL = findTL -- ok because first arg is the table to search
local key = objects:findTL(22)
isn't it better to take the value directly?
objects.1/1/1.tl
I don't know if it will work also with slashes, but if not, you may replace it by 'x' for example. Then it will be:
objects.1x1x1.tl

Resources