A few simple questions I have had for a while

To preface this, I’ve had these questions for a while and I’ve managed to code perfectly fine without having them answered. However, if certain things turn out to be true then it’ll make things a lot easier on me lol.

Question 1: Is there a way to break/terminate a function?

If I have something like this,

local condition = true

function blah(cond)
     if cond then
          condition = false
          blah(condition) -- it is called again
          -- terminate the first call here please
     end
     print("I only want this to print 1 time")
end

blah(condition)

In this scenario the blah function is ran twice. Ideally, I could somehow break/stop/destroy the function right after its called again, and that print message would only be printed one time. Is it possible to do that?

Question 2: if I run a for i,v in pairs() loop for a table, and within that loop things get removed from the table, will it mess up in any facet?

Any help is appreciated. I tried to figure out how to format that code so it showed up as such. Not sure if I accomplished that so, sorry.

Adding a return statement will break your function and yield it wherever you put it.

Example

local function hello()
   print("Hello!")
   return -- Will return and end here
   print("World!") -- Will error & not print this lien
end

hello()
2 Likes

Couldn’t you just print the message from inside the condition to solve this?

local condition = true

function blah(cond)
	if cond then
		condition = false
		blah(condition) -- it is called again
		print("I only want this to print 1 time")
	end
end

blah(condition)

See, I figured you could do it like that. and just have some conditions on the other end about the return.

Its not so much about printing the message, the print thing is used only to demonstrate my point that, after it is called and ran through the second time, the first call would finish.

1 Like

If you are properly removing the table, and you don’t depend on the position of the values (in the table) to correctly identify it, it should be fine.

1 Like

Well, I always remove dependent on the position. I thought that was the only way to do it. Enlighten me please.

What I mean is, if you are not going to use the position of an item to identify it, it shouldn’t be a problem.

Example:
local t = {1, 2, 3}

table.remove(t, 1) -- Equal to t[1] = nil
--[[
 Every time you remove, the items shift to the left
Eg; when the first item is removed, the others shift over,
so the table you end up having is {2,3}
]]--

print(unpack(t)) -- t = {2, 3}

Output
--> 2, 3

So if you always thought the number 3 was in the third spot, you are depending on the position to identify it, when in reality…

print(t[3])

Output:
--> nil

I hope I summed it up lol

2 Likes

It’s perfectly fine to set indices in a table to nil during a traversal; the reason why is because Lua does not shrink the size of the table when a nil is inserted (if it did, it would mess up the traversal).

It’s also worth mentioning that table.remove should never be used in time-critical code, because it (as AbiZinho noted) re-indexes all of the subsequent keys every time it is used to remove an index. If order doesn’t matter, a better alternative to this function is a technique known as a “swap remove:” the value of the last index in the table is swapped into the index to be removed, and then the last element is set to nil. In code:

local t = { 1, 2, 3, 4, 5 }

-- removing value at index 1
local tSize = #t
t[1] = t[tSize]
t[tSize] = nil