Intro
I’ve came a long way from this absolute dumpster fire of a project from 5 months ago. I’ve improved so much it’s ridiculous. Looking at the code I wrote back then is so embarassing. But I wanna keep it there for growth refference.
Making mistakes and learning from them is just so so valuable. There are so many principles to follow, but I feel like you only learn WHY it’s good to follow these principles when you the trash you wrote before comes back to haunt you. It’s a never ending cycle.
Tangent over, lets get it into it ^~^
Abstract Data Structures
Alright so in this task we’ve gotta implement the linked list, hashmap, stack, queue and all that good stuff. The restriction we have is that we can’t use any Systems.Collections namespaces for any adt implementations.
I definitely appreciate them much much more than before. It’s quite long so I recommend navigating to different sections using the table of contents on the right.
I’ll showcase some of my favourite methods and new things I learnt.
LinkedList
Full Source: gist
Learnt: Taking in funcs and actions as method parameters
|
|
|
|
Obviously Linq already does this sorta thing but I always thought it was magic until I implemented it.
Stack and queue
Honestly nothing interesting here. It was all pretty basic. I just used a linked list under the hood to make my life easier.
Hashmap/Dictionary
Full Source: gist
Awwww snapp this one is my favourite. Its soo friggin cool 🤓, also lowkey the cheatcode to every leetcode problem.
I love this dude, he explained it so well.
There are quite a few approaches but I decided to go with how Java implements it with a starting size of 16 and a load factor of 0.75f because it’s quite a nice middleground. For resolving collisions I’m using seperate chaining.
Interestingly in Java, when the map reaches a certain threshold, it switches the linked list with a self balancing BST. So worst case O(n) key retrieval becomes O(log n). Smart mofos.
Learnt: Obvs how a hashmap works internally but also creating an indexer which I didn’t know I could do.
|
|
|
|
Architecture and Systems in the Game
Alright so I got tired of the singleton nonsense. Gave me so many issues all the damn time. I’m still lowkey using it, but in a different way using the service locator pattern.
So pretty much, I have these prefabs in the game which act as services. None of them care about persistent data, they sort of act like static helpers.
So I have:
- Scene Service for loading scenes
- Audio Service for manaing music
- Dialogue Service for dialogue stuff
The services themselves are responsible for registration, which allows them to be accessed elsewhere via their class type.
|
|
Accessing from another class is as follows:
|
|
So here’s the problem, these services are prefabs because I need them to act like a gameobject. For example the audio service has a bunch of audio sources as children, the dialogue has its own UI and the scene loader has its own UI.
So what I did was, I inject all of them into any scene in the project when im playtesting in the editor and they get put under DontDestroyOnLoad.
In the actual build, they all get dumped into a preload scene.
|
|
Aigh’t that’s all I wanna share for now :D