Help with Inventory system!

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.
User avatar
H.I
Prole
Posts: 8
Joined: Wed Mar 29, 2023 3:34 pm

Re: Help with Inventory system!

Post by H.I »

RNavega wrote: Mon Jun 10, 2024 12:43 am
H.I wrote: Sat Jun 08, 2024 8:15 am
Hi. In that code the loops are redundant as you're not using the "x" and "y" variables.
The function can be reduced to just this line, "self.arr[iy][ix] = 0"
Thank you for noticing, i guess i didn't notice that.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Help with Inventory system!

Post by RNavega »

H.I wrote: Sun Jun 16, 2024 3:36 pm Thank you for noticing, i guess i didn't notice that.
Reading it again, I should've said "the way that the loops are used", without mentioning the index variables, because you can have a totally useful loop where the index variable happens to not be used.
For example:

Code: Select all

-- Saving time by both emptying a table and calling a method of its items.
for unusedVariable = 1, #myTable do
    local myItem = table.remove(myTable)
    myItem:method()
    (...)
end
Edit: that is more performant than "while #myTable > 0" because it only calculates the table length once, when the FOR loop starts, instead of on each iteration of that WHILE.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests