#Having trouble accessing a list (error:
1 messages ยท Page 1 of 1 (latest)
Will post inside of this thread.
What might be the issue here:
-- Data for coordinates
local tileSpawnPositions = {
["0498ed"] = {
{ -- Rotation 0
{-0.30, 1.10, 5.92}, {0.18, 1.10, 5.82},
},
},
}
list goes on, 8 valid GUIDs
-- GUIDs of the bags
local bags = {"0498ed", ....}
-- Get a random bag GUID from the list
local randomBagGUID = bags[math.random(1, #bags)]
-- Get the bag object
local randomBag = getObjectFromGUID(randomBagGUID)
-- Get a random object from the selected bag (which is the tile)
local randomObject = randomBag.takeObject()
local randomObjectGUID = randomObject.getGUID()
print(randomObjectGUID)
-- Define the target position for the object (place this in your desired position)
local targetPosition = {-1.36, 1.09, -0.05}
-- Define possible Y rotation values
local rotations = {0, 45, 90, 135, 180, 225, 270, 315}
-- Choose a random Y rotation value
local randomRotationY = rotations[math.random(1, #rotations)]
-- Set the position and apply the rotation
randomObject.setPosition(targetPosition)
randomObject.setRotation({x = 0, y = randomRotationY, z = 0})
local index = math.floor(randomRotationY / 45) + 1
-- Access the spawn positions based on the object GUID and rotation
local spawnPositions = tileSpawnPositions[randomObjectGUID][math.floor(randomRotationY / 45) + 1]
local bagGUID = "f826d1"
local bag = getObjectFromGUID(bagGUID)
-- Ensure the bag exists and spawn positions are valid
if bag and spawnPositions then
for _, pos in ipairs(spawnPositions) do
-- Take an object from the bag and place it
local takenObject = bag.takeObject({
position = {pos[1], pos[2], pos[3]}
})
end
end
Goal:
Have a tile taken from a random bag, positioned and rotated randomly.
Rotation is only in 45ยฐ steps, so 8 possible values.
Taking the rotation from the object after the random rotation is done, then divided by 45 to get a single digit integer. That is used to access the shown list at the top, to then get coordinates for precise spawn points.
But print(spawnPositions) is the part where it logs 'nil', despite all other logs being just fine.
Error message:
Line 293 is
local spawnPositions = tileSpawnPositions[randomObjectGUID][math.floor(randomRotationY / 45) + 1]
Don't have a lot of time for this, but I'd recommend adding some logging directly before this line to make sure each part is exactly what you want it to be
i logged all of the nested entries
(if that's how you properly say that(?))
i got what i expected
but I might be on to it
first thing to do is split your line up so you have only one table access at a time
(as well as prove this is the error line, because sometimes it is not)
I was careless with my syntax, that was part of the issue, I think I have that solved, but the nil value is still logged
log(1)
local k = math.floor(randomRotationY / 45) + 1
log(2)
log("k = "..logString(k))
log(3)
log("randomObjectGUID = "..logString(randomObjectGUID))
log(4)
local tileSpawnPositionEntry = tileSpawnPositions[randomObjectGUID]
log(5)
log("tileSpawnPositionEntry = "..logString(tileSpawnPositionEntry))
local spawnPositions = tileSpawnPositionEntry[k]
log(6)
log("spawnPositions = "..logString(spawnPositions))
as an excessive example of how to debug this
local tileSpawnPositions = {
["0498ed"] = {
{
{-0.30, 1.10, 5.92}, {0.18, 1.10, 5.82}, {0.64, 1.10, 5.69}
},
{
{-0.30, 1.10, 5.92}, {0.18, 1.10, 5.82}, {0.64, 1.10, 5.69}
}
}
}
The data
print(tileSpawnPositions)
print(tileSpawnPositions["0498ed"])
print(tileSpawnPositions["0498ed"][0])
print(tileSpawnPositions["0498ed"][0][1]) -- first error: nil
print(tileSpawnPositions["0498ed"][0][1].x)
Logging it
I didn't realize logging the first index isn't 0, but 1 instead.
rookie mistake....
๐
print(randomObjectGUID)
local spawnPositionstest = tileSpawnPositions[randomObjectGUID]
print(spawnPositionstest)
Unfortunately it isn't over yet
now I'm starting to think that the GUID as a variable isn't valid here?
list goes on, 8 valid GUIDs
this is clearly not the case
not in this case, I reduced it for this example
to have some overview
local spawnPositionstest = tileSpawnPositions[randomObjectGUID]
this is giving me nil because its just not right, despite randomObjectGUID being a valid GUID
so I'm trying different ways right now, chatGPT says it's correct, but it obviously isn't
๐
using chatGPT for one ๐
it knows more than me at this point
let's prove some things to you, first of all
print(randomObjectGUID)
local spawnPositionstest = tileSpawnPositions[randomObjectGUID]
print(spawnPositionstest)
this isn't a very "complete" test of what you're doing
for showcasing purposes, the first print is a GUID
so I'd assume the second one would be as well, if there's an actual list called 'tileSpawnPositions'
and I did print all possible GUIDs as well to see if it exists
log("randomObjectGUID = "..logString(randomObjectGUID))
log("tileSpawnPositions = "..logString(tileSpawnPositions))
local spawnPositionstest = tileSpawnPositions[randomObjectGUID]
log("spawnPositionstest = "..logString(spawnPositionstest ))
prove what your table contains, then theorise, then prove again
assuming is very useful, but only when you know what you are doing - if chatGPT knows more than you at this point, my advice is for you to stop assuming
I'm sitting at this particular function for hours now
this error breaks my spirit, and it's just basic knowledge I'm missing at this point
it is late, I'll continue tomorrow
thanks so far, I think I'll need to read a little about LUA lists
what you could do with more than anything else is examples of how to break down a problem, as I am doing above.
come back to this with a clear head tomorrow but do run the code above and do consider the results
I'll give it my best