How to handle multiple objects

multiple objects like blox fruit that uses so many objects for example creating a house which cannot be inserted in an array since to remove/ads something in it it would cauae performance issues i was thinking about linked list but i havent found any good tutorials about it.

Also if you dont mind helping me with this

1 Like

Linked lists are better for when you need a data structure that is flexible enough to accept new items in the middle of them, but not so much if you need to actually access those middle objects. This is because it needs to be traversed from the start to the actual index, whereas in an array, you go straight to the index you need. They’re also quite difficult to implement, though that idea came from trying to implement it within a pre-existing code structure that I didn’t write myself (ie. it might be easier if you built yourself instead of trying to conform to someone else’s requirements).

If what you’re trying to make doesn’t require that much sorting and you actually need something that’s easier to add stuff in, I’d use Luau’s tables to make a dictionary. They’re flexible enough to accept and remove values in an unordered fashion so that you don’t have to reindex the entire thing when adding or removing a value in the middle. Adding is as simple as adding a key value pair and removing is as simple as setting that key’s value to nil. The only trouble spot I can imagine is figuring out how to reference each object using keys, but I’m sure whatever idea you had in mind for referencing values in an array could be translated over.

Or perhaps you actually need it to be ordered for one reason or another. If you are certain that you need a linked list, or just want to learn more about them, I can give you a rundown of how they work. My professor taught me to think of them as more of a chain of objects with 2 nodes. For a singly-linked list, those objects would have a next node and a value node. The next node would contain the next object while the value node would contain the value. Then, the only thing that needs to be kept track of is the first item (known as the head) so that you can actually use the list. To mark the end of the list, we can just set that object’s next node to nil. Here’s a diagram of what that would look like.

They can also be doubly-linked, which means that they can traverse both ways. They’re basically the same thing as a singly-linked list, but each object has an additional previous node to keep track of the object behind them and the list has a tail (ie. same thing as a head, but at the rear).

To implement this, you could use dictionaries to represent the objects and emulate OOP to have multiple of those objects.