Coding things you probably didn't know (part 1)

When the faster Lua VM was released, they discouraged use of next because using pairs() would return a ‘faster’ next. I believe this was updated with time as now they are now using the same ‘next’.

I know loleris uses next in some weird way on ProfileService but I don’t know how to use it. He should have shown probably how to use next in different ways.

1 Like

what is the point???

It’s used for variable scoping. If you wanted to keep a variable in a certain scope, you would be using it.

2 Likes

If math.random has no arguments, it will generate a number between 0 and 1, and not an integer.

local randomBool = math.random() == 1;

The chances of this being true would be very low

1 Like

Next alone can be used to retrieve the first key-value pair in a table or if a key is specified then the next entry from that key. This is different from using it as a generator for a for loop. It’s very useful if you want to be able to get the next key-value pair without setting up a whole loop; additionally, as dictionaries do not have order, you do have to use it if you want to get the next entry.

ProfileService uses it recursively but if you want an easier and simpler example of it being used in a resource then check out Quenty’s Maid class. Next is used with a while loop to go through a task dictionary’s entries and clean them out while accounting for potentially new tasks that might get added during the cleanup up until next returns a nil key-value pair.

(Also sorry for the double reply notification you might’ve gotten, misread your post and deleted the other one I made.)

1 Like

what is this???

Please do surface-level researching on your own time! You can look through the Developer Hub or PiL if you see a function in use that you don’t know of.

https://www.lua.org/pil/7.3.html

You should always do a bit of researching first before forwarding a question. The same would be applicable for a lot of the questions asked here.

1 Like

didn’t know next was even documented lol but ill do my research

Oh ok thanks for the help.

Also it seems like it’s still ‘unoptimized’.

image

So using next in loops instead of pairs is discouraged.
Maybe it’s possible to replace the next function with the one that is inside pairs.


Yeah you can*

image

local next = table.pack(pairs({}))[1]; --\\ ugly lol

3 Likes

pairs and ipairs received performance boosts in 2019 and was briefly covered in the RDC presentation Lua As Fast As Possible, so it’s not particularly surprising that they run faster and better. That and they’re canonical generators so regardless they should be used anyway unless you’re creating your own generator.

1 Like

I could have sworn Lua did not have a continue keyword but anyway thank you for the helpful tips

1 Like

yup it some how does wen i learned about it i was stumped too

That is redundant. You can literally just use:

local randomBool = math.random(1, 2) == 1

A few things you forgot to mention were the comparison from pairs and ipairs. Whereas one is better meant for dictionaries and the other for arrays.

Oh and you should add a note that spawn and delay should be discarded in favor of coroutines

oh my bad, I didn’t even notice that.

this is there bc its a random bool not a random number

Precisely why we use the == operator. It only returns true, false or errors.

1 Like

i tried it out no errors happened

Never said it would error. I’m saying that the == operator can only do those things.

1 Like

/reply to this post a suggestion on coding things you probably didnt know` to be featured in pt 2 you will be credited

1 Like

On your next things you didn’t know about coding, you should mention that you can put semicolons at the end of your lines and it still works.

For example this works even with the semicolons:

local a = 2;
print(a+5);

It’s usually unnecessary to put semicolons after your lines but I still thought it was cool to point this out.

1 Like