this post was submitted on 23 May 2026
7 points (88.9% liked)

Learn Programming

2195 readers
1 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 3 years ago
MODERATORS
 

I want to preface this by saying that I think I got cooked by AI here.

I have a game where I am working on a job system. The job system has a job objects which I need to put into a data structure that can be searched via id, location, or work type. At first I was building with the mindset of "do whatever to solve this problem and make it work" but it became very hard to implement new features because I would break everything. So I started trying to think ahead and design ways of doing things that would handle all the different jobs and things.

At first I put all jobs into an array and iterated through. Then I learned about dictionaries and started using them for way to many things and so i updated my job queue to be a dictionary with the Key as ID > JobObj

Then I decided to plan out the job system before writing anything and "do it properly". I decided I needed to upgrade the data structure holding jobs because its a core part of the game and will be heavily interacted with. But I realized I only know array, dictionary and database, so I asked AI what data structure I should use and it suggested a nested dictionary.

Now im using multiple dictionaries but im really hating it. Its hard for me to work with and conceptually im not sure I can visualize how its even working.

How I am thinking about it is there is multiple layers of keys, 1st layer is work types, then once I find the work type it points to a dictionary of region IDs and that points to an array of jobs in the region.

Job def is work type like Planting Region id: the map is broken down into region IDs so i dont have to check every tile and can limit seaching

#python
var job_pool: Dictionary = {}

func register(designation: DesignationObj) -> void:
	var job_def = designation.job
	var region_id = designation.region_id
	if not job_pool.has(job_def):
		job_pool[job_def] = {} 
	if not job_pool[job_def].has(region_id):
		job_pool[job_def][region_id] = []
	job_pool[job_def][region_id].append(designation)

Here is how I am picturing it in my head.

var job_pool {
	"plants": {
		"1": {
			"job1"
			"job2"
		}
		"2": {
			"job3"
		}
	}
	"mining": {
		"1": {
			"job4"
			"job5"
		}
	}
	"hunting": {
		"5":{
			"job12"
		}
	}
}

But now I want to add ID look up into this im stuck and im thinking the entire structure does not work for what it needs to do.

top 6 comments
sorted by: hot top controversial new old
[–] vk6flab@lemmy.radio 9 points 2 weeks ago (1 children)

You're describing a database.

Depending on the number of jobs, you may be able to store it in memory, otherwise, for small to medium size, sqlite. For larger datasets, Postgresql or MariaDB, for humungous, Parquet or duckdb.

[–] Fizz@lemmy.nz 1 points 2 weeks ago (2 children)

ugh i was hoping this wasnt going to be the answer because its another thing to learn. I need something easy to learn would that be sqlite.

If I have a database should it be explicitly for the job queue or can I look at storing other things in it? Like I may want to store event history.

[–] Gyroplast@pawb.social 5 points 2 weeks ago

I would double down on sqlite for your case. If you are using python, the sqlite3 module is included, but looking at your pseudocode you are probably looking for Godot-SQLite instead. I have no idea what the #python line is supposed to be doing in the first line of your code snippet, besides confusing me.

SQL is old, and it shows. "Proper" relational database design is a black art, as is optimizing SQL queries, but speaking from my own early experience, you'll be doing 95% with INSERT, SELECT, UPDATE, DELETE, anyway, with an occasional simple JOIN. This is absolutely manageable and somewhat worth learning, as you're basically custom building your own data structure according to how you plan to use it most easily. This SQL with sqlite tutorial seems solid to me.

Beware LLM output. Wild guess is you'll be sent off on a tangent to normalize your schema, unconditionally use synthetic, unique integer IDs everywhere, and other "best practices" that may not be necessary, and only complicate your structure beyond the point of understanding. Then it'll be hell to use. Solid groundwork goes a long way, and you can always change things up when you realize "Damn, I'd love to have in that tuple for a really simple query!"

Give it a try. Creating your own data structures in the way you want can be really rewarding in itself.

If you're gravitating towards a NoSQL database to avoid SQL, you're basically using dicts again, and will have to learn a different query language, anyway. This may be a more approachable option for you IF your data structure isn't very nested and uses few complex types, like arrays.

And yes, you absolutely will find dozens of things to slap into a DB once you got a taste of it, and there's little reason not to. Save your settings in there, debug info, events… everything can be a table if you're brave enough. :)

[–] vk6flab@lemmy.radio 3 points 2 weeks ago* (last edited 1 week ago)

While databases have struck fear in the hearts of many developers, it's really just a structured way of storing (sometimes large amounts of) information.

From a naming perspective, a database is essentially a collection of tables, each of which you can think of as a simple list of rows, each row containing a collection of fields, each not unlike a single spreadsheet with a label at the top of each column.

Like a spreadsheet, you can decide what types of values are permitted in each field (column).

In spreadsheets you can refer to a row by its row number, which breaks down as soon as you sort a sheet, database tables can similarly be ordered, or not, depending on the requirements.

A query returns one or more rows as required.

You can relate two tables if you want to by having both tables have a field with the same definition and value, and then query the information together, linked by that common field. You can link many tables together if your data requires it.

For example, you might have a list of job types, with information that's specific to each job of that type. By storing those types in their own table, you don't need to duplicate the information for every single job stored in another table.

Essentially that's pretty much all there is to it.

If you get into bigger and more complex stuff, you'll come across a thing called an index, which tracks one or more field values across one or more tables, for the purpose of making retrieval faster, but until you hit a thousand rows, you're unlikely to need them.

Disclaimer: This is a summary of 40+ years of database experience and it's purposefully written to simplify concepts. It doesn't get into different database engines, but at this stage that likely won't matter much.

The underlying purpose of a database is to ensure data integrity, in other words, to make sure that a telephone field contains a telephone number and not an email address by accident. It's also intended to minimise the amount of data duplication and associated resource use, like memory, disk and network. Finally it provides mechanisms for multiple simultaneous users and associated conflicts.

Have fun!

[–] MadhuGururajan@programming.dev 2 points 1 week ago* (last edited 1 week ago)

Since you have a sequence of keys, go with a trie data structure. The key will be a string of "worktype|region|job".

You can encode/decode the individual keys from enums to integers (vice-versa for decode).

but since you said you want it to be simple and want this for a game you probably can go with any SQL database as they are reliable and support locking so you don't have to worry about parallel access later.

[–] Buddahriffic@lemmy.world 2 points 1 week ago

One important thing to think through is how your resulting data structure will be used and how each layer and its overhead fits in.

Like for example, I once used a data structure that ended up being an array of maps to store a series of chunks of arbitrary data. It would parse each line of data into key/value pairs in a map/dictionary and then add that as an entry for the array. It was slow.

It was much faster and used less RAM when I changed it to a map of arrays, since each line would have the exact same pieces of data in a file, it just wasn't certain what would be in any given file. This meant that there was only one place where all the map overhead was stored, and each entry in the arrays corresponded to the same entry in the other arrays.

I'd also suggest a design where you don't care what the actual implementation is outside of the module that needs to do the storing itself. The main thing you need is a unique addressing system to ensure that a) each piece of data has a predictable label/address to find it and b) each label/address only points at the relevant data.

Eg, if you end up using an SQL database, you don't want to be writing SQL queries into your crafting system, but to use identifiers that you pass into the storage module where it builds the queries (or even better, uses stored procedures so the queries themselves are also stored on the database, which is faster and more secure). Then, you can decide later on down the line that instead of an SQL database, maybe you should use the new DreamStorage that uses the brainpower of those foolish enough to be early adopters of brain chip technology to do table lookups in constant time as long as it doesn't involve math. If you do modularization correctly, you should just be able to swap them out by matching the interface, regardless of what the implementation is.

If there's a hierarchy built into the way the data is organized, try to abstract it (eg something like "level0.level1.level2.item", where you can split the levels by "." when you need to). You should be designing the data structures for what your algorithms need to do with the data, not based on how you intend to store that data. Then the control flow side extracts the data each function needs and provides it to the function and then handles the result accordingly.

That said, don't worry too much about getting it perfect. Experience is going to be your biggest ally, so go ahead and make mistakes and learn from them. There isn't likely a solution that will trivialize everything. I usually just aim for "have a good idea of what all the blocks need to do, ideas on how they will do it, and how they will fit together" with my designs.