#Linked list or array ?

1 messages · Page 1 of 1 (latest)

ruby marlin
#

Hello,

I a little of game dev during school, but it was in C, and with a poor library (CSFML). During that time we learned to use linked list as alternative of arrays to manage some types of specific datas.

In my current godot game, I have a list of groups, that are visually one behind each other and move forward at same speed. I want to be able splitting a group in 2, merge 2 etc. Since my groups needs to be dependant of the group before & after it, linked list seems to be exactly kind of relation I want, but I read that GDScript arrays are really flexible and should be use in most cases. Any suggestion ?

Thanks 🙂

potent prawn
#

If you're working with a small number of objects, (single or double digits, not thousands) then it probably doesn't matter which container you you choose in terms of performance. However, to my knowledge Godot does not ship with any sort of linked list, so you'd have to write (or find online) your own.

Just use arrays for now because it will be easiest to implement and test. If it proves too slow in some respect, then start worrying about alternatives.

ruby marlin
#

Okay, it was clearly more about usage than performance tbh 🙂 linked list just feel really comfy sometimes, but noted ! I’ll go with arrays, they seems pretty flexible

#

By the way, is there any memory management in Godot ? Or does it manage it itself ?

potent prawn
#

I'm not qualified to answer the memory question.

But if your primary concern is ease of use over performance, and you expect to have a lot of interaction with these lists of items then I would design an interface first; i.e. encapsulate the problem.

Think about the operations you need to do on this list (merging lists of objects, splitting lists of objects, moving a single item from one to another, etc) and then write a class that implements those behaviors using whichever container you please. Now you can change the underlying container more easily than if it was embedded directly in other code.

So if you decide later on that arrays are for chumps and linked lists are what the cool kids are doing, then the interface to this class will guide your refactoring. You will know exactly how it is used, and where.

ruby marlin
#

I do need to make merge, split, insert etc, but arrays already have functions to make this easily, and i'm going to manage like 100 to 1000 items maximum, so it should do the job

restive rivet
ruby marlin
#

Hmm what is Object ? A basic boilerplate to make some objects class ?

restive rivet
#

Its the root type of the type system

ruby marlin
#

Okay interesting

restive rivet
#

You most likely won’t be making anything directly extending object though

ruby marlin
#

Yeah I guess that there's almost everytime a subclass more suitable for my needs to extend from ?

restive rivet
#

Yeah the most common things to subclass are gonna be Node, Node3D, Node2D, and Resource

ruby marlin
#

Okay I see

restive rivet
#

And all those inherit from RefCounted

#

So you don’t really need to worry about memory management most likely

ruby marlin
#

Okay that's pretty nice

#

And is it okay to make class extending from nothing ?

#

I have a class that i'm using as to group some nodes, but the group itself is not physical in my game

restive rivet
ruby marlin
#

Hmm

restive rivet
#

Just like in C++/Java if you don’t declare a super type you implicitly extend Object

ruby marlin
#

ok ok, so I'll need to free this class manually if needed ?

restive rivet
#

No since it extends RefCounted

ruby marlin
#

Ah yeah my bad !