0

This is something that's really been nagging at me for some time:

for i = 1, 4 do
   x = love.physics.newFixture(diffTable[i].body, diffTable[i].shape):setCategory(10)
   x = x:setUserData('Border') -- error here
   table.insert(data, x)
end

Let's say I want to insert a variable into the table (basically creating the variable, and then modifying it) and then inserting it:

When I do the x = x:setUserData(...) an error comes up.. saying attempt to index global variable x (nil)

So my question is, how would I create a variable inside a for loop, specifically I need to do it this way because I'm using love.physics, and creating a fixture with a category. I also need to setUserData at that time but it's not possible.

And I'm sure there has to be a way of doing this... Thanks in advance!!

6
  • 2
    Can you specify what your expected and actual outcome is? Like, do you want data.x to be the same value as x? Or do you want data[1] = { x = x, y = y } (that's what your first line with table.insert would do)? Commented Mar 22, 2021 at 9:48
  • data.x = x..... Commented Mar 22, 2021 at 13:57
  • your code doesn't make any sense. what do you want to achieve? improve your post. the provided code is full of errors and it does not help to show whwat you want to do. improve it Commented Mar 22, 2021 at 14:09
  • @codeRdevelopR if you expect that result then you shouldn't be using table.insert. table.insert will put the value at the next numeric index available. lua.org/manual/5.3/manual.html#pdf-table.insert lua.org/pil/19.2.html Commented Mar 22, 2021 at 14:19
  • Yes I've changed the question slightly.. its more detailed :) Commented Mar 22, 2021 at 14:23

1 Answer 1

3

The function Fixture:setCategory does not return a value.

so when you do this

x = love.physics.newFixture(diffTable[i].body, diffTable[i].shape):setCategory(10)

you are setting x = nil. Fixture:setUserData also does not return a value.

If you change it to this you will no longer get that error.

for i = 1, 4 do
   x = love.physics.newFixture(diffTable[i].body, diffTable[i].shape)
   x:setCategory(10)
   x:setUserData('Border') -- error here

   table.insert(data, x)
end
Sign up to request clarification or add additional context in comments.

3 Comments

Looked into setUserData just now it also does not return a value.
@codeRdevelopR that's why you should always refer to manuals. programming is not some magic power. it basically boils down to reading documentations.
This is very true, in fact, I really dont know anything about love2d I just looked up the function you were using to set the value of x in the love2d documentation, as the simplest answer to why x = nil is that the function returns nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.