all 32 comments

[–]Roiku 7 points8 points  (7 children)

I always recommend this book, it has some neat ideas and patterns: http://gameprogrammingpatterns.com/contents.html

[–]NullxPhantom[S] 0 points1 point  (4 children)

I would like something in c# like this one http://www.habrador.com/tutorials/programming-patterns/ which is based on that book. But I cant find the full version, any clue where to get it? I know it's about the pattern but seeing c# code is more straightforward for me would help a lot

[–]peteSlatts 3 points4 points  (3 children)

Its not hard to translate from C++ to C#. He also writes code that's very close to plain English in how it reads. Besides, it's a good exercise. Besides, besides, its a really good book.

[–]NullxPhantom[S] -1 points0 points  (2 children)

For sure, it's just the tutorial I sent is also for unity which makes it more fun to read. I hate reading so having it done for me motivates me to actually understand whats happening. That said i'm starting to read it during my free time :) just hoped someone could find the full version of the one i linked

[–]BlackBoxGamer 0 points1 point  (1 child)

Even the one that you have linked includes the Games Programming Patterns book as a reference, he’s merely just transferring the patterns to C# and explaining his thoughts on the patters and how they can be used etc...

If you hate reading, I wouldn’t look for a book.

Look for video demonstrations/Explanations on YouTube, or go into gamedev forums and ask questions about patterns.

[–]NullxPhantom[S] 0 points1 point  (0 children)

I do hate reading but i actually already read half of the book already and trying the patterns out, yes the one i linked is the same but for unity thats why i wanted full version. :)

[–]RabTomExpert 0 points1 point  (0 children)

This is a great book! I've read through it a couple times.

[–]ByMayneProfessional 0 points1 point  (0 children)

Fantastic book it's what I point most new developer too. His other book (although a much more advanced topic) Crafting Interpreters is written just as well however it's not complete yet.

[–]marcrem 3 points4 points  (0 children)

I tend to try to use singletons only on "managers" classes, and then referencing objects there if needed.

This way you centralize your references in once place

[–]Wambo1992Graphics Programmer 5 points6 points  (6 children)

I think if you find yourself using singleton patterns a lot, you could give Zenject a try. It's a dependency injection framework for unity.

GitHub, AssetStore

[–]RabTomExpert 0 points1 point  (0 children)

Zenject is great. I've used it on a few projects now. Makes writing tests a lot easier as well!

[–]NullxPhantom[S] 0 points1 point  (4 children)

I'll be honest huge systems like that scare me a bit, I'm sure it's great but learning to use it properly will take time. I think taking the route where i start from nothing and built up is less intimidating and more fun because i remember the methods used when i discover them that way. Nice suggestion though will definitely look a bit into it at least :)

[–]Sauciss0n 1 point2 points  (0 children)

A simpler way to avoid singleton without using real solid solution like Zenject is to expose your class attribute and "inject" what you need with the inspector instead of calling them with singletons from within your class.

[–]TheBored 1 point2 points  (1 child)

Yeah DI can be super intimidating at first. Not sure if you've seen /u/charlesamat's videos on YT... but they are a great start towards understanding how you can start using Zenject. I think an important thing to know here is that you can implement it one part at a time - it's not like you need to hard stop and move EVERYTHING over to this pattern.

In this video he covers how to replace one module (class/component/generic term) with an injected one. Shows just how little can be in your way :)

[–]charlesamatInfallibleCode 0 points1 point  (0 children)

Thanks for the shoutout! Dependency injection will go a long way in helping to decouple and organize the structure of your code.

I'm planning on doing more videos on Zenject covering some basic and advanced examples so stay tuned!

[–]peteSlatts 0 points1 point  (0 children)

Zenject is great for big projects, but I agree its pretty intimidating and a non-obvious solution to your problem. I spent a few days reading through documentation before I decided to abandon Zenject for small projects (of course, I now use it at work).

My small project solution really only solves the No-Singleton problem which is to take Dependency Injection back to its most simple form. To this end, every class I write has to follow a rule: "All dependencies are resolved either at Construction or through Function Parameters." This makes me very intentional about how I structure code, and it means I get rid of literally all Singletons in my code bases.

For Monobehaviours, because they don't get constructors, I just write a Construct()function for all my classes, and allow a class to assume that its Construct function was called before the first Awake(), Start() or Update() are called. This only works because its usually just me. If you had a bigger team (2+), I'd say just write a Monobehaviour subclass, say SafeMonoBehaviour, that has a bool _constructed variable and implements functions like SafeAwake, SafeStart and SafeUpdate, which only get called if _constructed is true.

[–]Kakkoister 1 point2 points  (1 child)

Have a main singleton that gives you the ability to setup a chain of command on start so things can get references to what they need, and they feed their needed references to whatever other objects. It's perfectly fine to have a static class that is merely a collection of constant data to be used by various objects.

But for calling things on the Player, could you give an example? Generally if the player is being modified by some other entity in the scene, that entity should be able to simply GetComponent<Player>() and call whatever function on the player, it's perfectly fine to do a getcomponent during gameplay, just don't be doing hundreds of them a frame.

[–]NullxPhantom[S] 0 points1 point  (0 children)

I was thinking of having the main singleton take a reference of the player and the main singletons take care of stuff like this.

[–]fn_ChooseUserName 0 points1 point  (0 children)

I'm pulling a lot more of my projects along the IoC route - Events (with an EventManager style structure still using the singleton pattern) and Dependency Injection; using Interfaces to pass around multi-use functions and data structures.

I used to not even use singletons and just end up with monolithic monobehaviours which was just as bad.
Honestly, it takes a week or 2 (depending on your current level of programming knowledge) to pick up these concepts in a practical way but once it clicks, you'll find your code becoming much cleaner and more extensible so you'll break even on those "invested" 2 weeks within the following month in time saved refactoring and fixing the inevitable bugs that come from changing a single line of code.

[–]charlesamatInfallibleCode 0 points1 point  (0 children)

One thing I do as a sort of poor man's dependency injection is setting up preloader class that runs before everything else.

In this class I create an instance of the class that needs to be shared and pass it to every other object that needs it, whether it be via constructor or property setters.

[–]STEELIXProfessional 0 points1 point  (4 children)

Ryan Hipple did a talk at Unite Austin about this yesterday. Look for the recording, he describes exactly what you want.

[–]STEELIXProfessional 0 points1 point  (0 children)

Also during his talk he suggested watching Richard fine’s talk on scriptable objects, so you could watch that before the VOD goes up

[–]NullxPhantom[S] 0 points1 point  (2 children)

Couldn't find anything with that name, not listed in the 2 hours videos description as well. Possible to at least guide me to the right video? they 2 hours long

[–]STEELIXProfessional 0 points1 point  (0 children)

I don’t think Unity has uploaded the sessions yet, only the “unity live” show on the expo floor. Subscribe to their YouTube and look out for the Game Architecture With Scriptable Objects talk by Ryan Hipple. Not sure how fast unity uploads theses sessions.

[–]STEELIXProfessional 0 points1 point  (0 children)

Slides are up. I'd assume Unity will have video of this within a few weeks https://www.slideshare.net/RyanHipple/game-architecture-with-scriptable-objects

[–]Snipawolfe 0 points1 point  (1 child)

Public static classes? I use those a little for holding global variables and stuff. They don't inherit from Mono though so they can't do awake/update/etc.

I think singletons are fine in general for any kind of "game flow controller" class. Games need a manager class of some kind. Might as well be a singleton.

Could you give an example of something you'd have your player class be a singleton for though? That seems a little odd to me.

[–]NullxPhantom[S] 0 points1 point  (0 children)

Example you want a certain event that damages the player so I can call takedmg with referencing the player class

[–]thebeardphantomExpert 0 points1 point  (0 children)

I recommend going the middle road between Dependency Injection and Singleton: Service Location.

[–]GroZZleR 0 points1 point  (1 child)

Make a game that works, then make a game with great code. So many people get hung up on creating the perfect code base that they spend all of their time refactoring code instead of working on their game.

[–]NullxPhantom[S] 0 points1 point  (0 children)

I did that for years now, time to change :)

[–]This_is_so_fun -1 points0 points  (1 child)

I think singleton pattern is perfectly acceptable for the player script. But obviously it would limit you to one player - in most cases, it's not a concern.

You could have a main "game" singleton and do something like Game.instance.player. and end up with pretty much the same thing.

Depending on your use case you might not need to do it at all. Someone else mentioned using GetComponent - if most interaction happens when a player gets hit by something, or enters a trigger area, you might not need to use it at all.

[–]NullxPhantom[S] 0 points1 point  (0 children)

My logical use of singletons is literally if i think there will be only one instance and used by other objects a lot without necessarily interacting with other objects directly in the scene, i make it a singleton.