Oi! I'm trying to create a simple script that loops through element IDs and changes their gravity.
But then I get:
for i=0,170,1 do
tpt.el.i.gravity=-1000
end attempt to index field 'i' (a nil value)
How do I loop through the element IDs and get TPT to change their gravity values?
In Lua, the dot syntax is just a shortcut for the bracket syntax.
tpt.el.i.grav
is the same as_G["tpt"]["el"]["i"]["grav"]
where _G is the environment table of the function (well, in Lua 5.1 at least).
So you're really just requesting the .i field, which of course doesn't exist. What you could try is use the bracket syntax with the variable i, something like tpt.el[i].grav
but that is also incorrect because tpt.el doesn't have integer-keyed elements. Instead its keys are the names of the elements.
You should use the new API instead. elements.property takes element IDs, so let's use that:for ix = 0, 255 do
elements.property(ix, "Gravity", -1000)
end
255 is the absolute maximum number of elements that can exist in TPT (in version 91.5.330). But wait, this is also wrong because there are unused element IDs! Dang. Let's add pcall to make Lua not care:for ix = 0, 255 do
pcall(elements.property, ix, "Gravity", -1000)
end