What is a LazyTable?

In the current version of the debugger, the Watch window shows LazyTables instead of just Tables:

image

What does that mean?

1 Like

@IcyTides ?


Hey there! So, a Lazy Table is like a special type of box that you can use to store things in a game you might be making. But, instead of putting things in the box right away, you can wait until you actually need them.

It’s kind of like if you were packing a backpack for a camping trip, but you didn’t want to put everything in at once because it might be too heavy. So, you put some things in a special bag and only put it in the backpack when you actually need it.

With Lazy Tables, you can do the same thing with the things you need in your game. This way, your game will run faster and smoother because it won’t be slowed down by things it doesn’t need yet. Cool, right?

Thanks, but I really want to understand why normal tables are shown as LazyTable instead of Table.
But since you brought it up, where is this documented?
Do you have any examples?

Here is a normal table:

-- Normal table initialization
local swordTable = {
  ["Iron Sword"] = 10,
  ["Diamond Sword"] = 20,
  ["Gold Sword"] = 5,
}

Here are 2 different versions of lazy tables using the same information:

-- LazyTable initialization with two swords
local swordTable = {}
swordTable["Iron Sword"] = function() return 10 end
swordTable["Diamond Sword"] = function() return 20 end
-- LazyTable initialization
local swordTable = setmetatable({}, {
  __index = function(self, key)
    if key == "Iron Sword" then
      return 10
    elseif key == "Diamond Sword" then
      return 20
    elseif key == "Gold Sword" then
      return 5
    end
  end
})

Again, the main difference is if you initialize all the values first or wait until something happens, you would need the values before initializing them.

Reference:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.