[SOLVED] I'm confused, Is a bug of 'repeat until'?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
abyssol
Prole
Posts: 25
Joined: Wed Nov 03, 2021 6:20 am

[SOLVED] I'm confused, Is a bug of 'repeat until'?

Post by abyssol »

I have never had this problem

Code: Select all

local queue = {}
local bananas = {}

function love.run()
  -- ***
  -- above are same as love.run
  local i = 1
  repeat
    if queue[i] then
      for _, o in pairs(queue[i]) do
        o:func(bananas[i])
      end
      bananas[i] = nil
    end
    i = i + 1
  until(i > 5)
  -- below are same as love.run
  -- ***
end
the code is right, isn't it?

but it alwalys runs that

Code: Select all

bananas[i] = nil
first

whatever I change the code to prevent it happening

Code: Select all

repeat
  if queue[i] then
    for _, o in pairs(queue[i]) do
      o:func(bananas[i])
    end
    if i < 5 then
      bananas[i + 1 - 1] = nil
    end
  end
  i = i + 1
until(i > 5)

Code: Select all

repeat
  if queue[i] then
    for _, o in pairs(queue[i]) do
      o:func(bananas[i])
    end
  end
  i = i + 1
  if i < (999 + 1) then
    bananas[i * i / i] = nil
  end
until(i > 5)
If conditions are satisfied, however i change it and its place, 'repeat until' always runs these bananas code first, and then it causes error, which says attempt to index nil value in o:func, because the values in bananas had been assigned nil

when I use 'for' with bananas[ii] = nil

Code: Select all

for i = 1, 5 do
  if queue[i] then
    for _, o in pairs(queue[i]) do
      o:func(bananas[i])
    end
    bananas[i] = nil
  end
end
everything is right!? :shock: values in bananas are not nil in o:func :shock:

for logic reason i must use repeat until, or alternative 'goto', well goto has been tested, it also has this problem
main.7z
(609 Bytes) Downloaded 46 times
--test file here
Last edited by abyssol on Fri Dec 01, 2023 4:05 am, edited 7 times in total.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: I'm confused, Is a bug of 'repeat until'?

Post by MrFariator »

Okay, I'll take just this snippet, and step you through it:

Code: Select all

local i = 1
repeat
  if queue[i] then
    for _, o in pairs(queue[i]) do
      o:func(bananas[i])
    end
  end
  i = i + 1
  if i < (999 + 1) then
    bananas[i * i / i] = nil
  end
until(i > 5)
1. The code starts a repeat-until loop, ok.
2. If queue table at index i contains a value, execute contents of the if statement, ok.
2.1. Start a for-pairs loop, looping through the contents of queue at index i. So long whatever it is at queue at index i is a table, this is ok. Otherwise, we may crash here.
2.2 For each object contained within the table we're iterating with for-pairs, invoke the contained table o, calling a function called "func", and pass it whatever is contained at index i in 'banana's table. So long the value of "o" is a table/object that contains the function "func", this is ok. Otherwise, we may crash here.
3. Increment the value of i, ok.
4. Check if the value of i is less than 1000. Given the full contents of the code, this will always be true. This is ok, but a redundant if-check.
5. Inside the banana table, set whatever it is at index i * i / i to nil. Given that we start with i's value at 1, by the time we get here, the first time the formula evaluates to 2 * 2 / 2 = 2. So, set banana table's contents at index 2 to nil.
6. Check if i is larger than 5. If not, go back to 2.
(note: it's 3am, so pardon me if some details are wrong)

Ultimately, however, because you have the following:

Code: Select all

local queue = {}
local bananas = {}
...Nothing really happens. The if-check never evaluates to true in step 2, so the if-statement's contents are never executed, and setting random indices at step 5. accomplishes nothing. Effectively, this code is just repeating steps 3 through 6, accomplishing nothing of note, other than waste some CPU cycles.

However, if you are actually populating your tables with legible objects, I believe it's step 2.2 that is the source of your woes: do the tables or objects contained within the queue table actually contain the function "func"? You haven't posted any error traceback, or even code about how you might populate either queue or bananas table, so at best I can guess what your actual error is.

If I may be brutally honest, this code doesn't make much sense. Particularly the bit about "i * i / i", which you could just replace with "i". Was this AI generated, or what is the logic here? Please write what you actually aim to do with your code. No code, just words, explaining what your algorithm aims to do, though attaching any error traceback is also helpful.
abyssol
Prole
Posts: 25
Joined: Wed Nov 03, 2021 6:20 am

Re: I'm confused, Is a bug of 'repeat until'?

Post by abyssol »

MrFariator wrote: Fri Dec 01, 2023 1:31 am Okay
i uploaded a main.7z, you can unpack it, it is really a bug in my device, maybe you can try to run it, and find something, thanks
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: I'm confused, Is a bug of 'repeat until'?

Post by MrFariator »

The contents of your uploaded main.lua do not match the code in your opening post, since you replaced the repeat-until with a goto, but...

Code: Select all

local queue = { { 1 } }
local bananas = { 'got nil?' }

function func(o)
  love.graphics.print(o[1])
end

-- inside the loop
for _, o in pairs(queue) do
  func(bananas[i])
end
There are so many things wrong here, but I'll keep it simple:
1. For each value within the queue table, the pair loops doesn't make any use of it, and instead always sends whatever is in bananas table at index i at that time. On first iteration, this will be the string "got nil?", because i is 1, but on any subsequent run of the for-pairs loop it will hit an empty index (2 is nil, 3 is nil, 4 is nil, ...).
2. Inside the "func", the code assumes that you can treat it like a table or object, and print whatever is at index 1. However, because your code sends the string "got nil?", this returns nil, and love.graphics.print raises an error. It doesn't know what to do with a nil value.
3. Even if point 2. didn't crash, you'd crash on the next iteration, because i would increase to 2 (if the code were to loop, you changed the exit condition for the loop between your opening post and the attached main.lua), and there is nothing in bananas table at index 2 and beyond.

So, in an essence, what you're trying to do is:

Code: Select all

local myVariable = "value"
love.graphics.print(myVariable[1]) -- myVariable[1] returns nil, crash
As such, to make this code snippet run, the bananas table should contain the following:

Code: Select all

-- wrap the elements inside tables, so to match the bananas[i] -> o[1] access
-- add as many elements as desired to match how long the loop is supposed to run
local bananas = { [1] = {[1]='got nil?'}, [2] = {[1]='2'}, [3] = {[1]='3'} }
-- or
local bananas = { {'got nil?'}, {'2'}, {'3'} } 
Both of those lines are equivalent, so you may take your pick.

Also, there is nothing here that necessitates the use of repeat-until or gotos, nor is this bug related to repeat-until specifically. It's how you're treating the contents of your tables.
abyssol
Prole
Posts: 25
Joined: Wed Nov 03, 2021 6:20 am

Re: I'm confused, Is a bug of 'repeat until'?

Post by abyssol »

MrFariator wrote: Fri Dec 01, 2023 1:51 am The contents of your uploaded main.lua
so do you also have this problem :shock: ? i am getting annoyed about it

i think this is impossible in programming, maybe it is something wrong with compiler
Last edited by abyssol on Fri Dec 01, 2023 2:08 am, edited 2 times in total.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: I'm confused, Is a bug of 'repeat until'?

Post by MrFariator »

I explained what the issue in your code is, and how to address it. However, I'd recommend taking a step back, and trying to understand your table accesses. That's the root of your issues.
abyssol
Prole
Posts: 25
Joined: Wed Nov 03, 2021 6:20 am

Re: I'm confused, Is a bug of 'repeat until'?

Post by abyssol »

MrFariator wrote: Fri Dec 01, 2023 2:03 am I explained what the issue in your code is, and how to address it. However, I'd recommend taking a step back, and trying to understand your table accesses. That's the root of your issues.
main.7z
(609 Bytes) Downloaded 64 times
a better one to present the bug, my previous uploaded file is so bad i have to admit.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: [solved] I'm confused, Is a bug of 'repeat until'?

Post by MrFariator »

What is the remaining bug? The text not rendering? You might want to take a a look what the the code between lines 55 and 61 is doing.

Edit:
Actually, reading the code, you're setting the index 1 in the bananas table to empty, but yet to you continue to run the code because it's defined inside love.run, the update loop of löve. So, you will on second run of the code index a nil value, because the queue table's contents don't change, which means that the func function is always called, and it always tries to index whatever value it receives.

Again, I ask you: what is this code supposed to do? Why the queue and bananas tables? In your own words.
abyssol
Prole
Posts: 25
Joined: Wed Nov 03, 2021 6:20 am

Re: [solved] I'm confused, Is a bug of 'repeat until'?

Post by abyssol »

MrFariator wrote: Fri Dec 01, 2023 3:24 am What is the remaining bug? The text not rendering? You might want to take a a look what the the code between lines 55 and 61 is doing.

Edit:
Actually, reading the code, you're setting the index 1 in the bananas table to empty, but yet to you continue to run the code because it's defined inside love.run, the update loop of löve. So, you will on second run of the code index a nil value, because the queue table's contents don't change, which means that the func function is always called, and it always tries to index whatever value it receives.

Again, I ask you: what is this code supposed to do? Why the queue and bananas tables? In your own words.
Thank you so much.

I realized your anwser, it is solved.

for k = 1, n do
if queue[k]
end

Limit n is not increasable, I try to use 'repeat until' and 'goto', and then I forget to set queue's every value to nil, as I don't set queue's values to nil in 'for' loop cos it will be reuse per frame and that is fine, while n is not increasable and will set to 0 per frame end. I think it is fine so I haven't considered it in 'repeat until', this is the error cause. Thanks!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 63 guests