Notifications
bg
Lucas
Self taught hobbyist - Programmer
Porto Alegre, Brazil
23
Likes
29
Followers
14
Connections
All
Articles3
Games0
Showcases0
Column3
Jobs0
Lucas
published the article
Aug 2, 2018
Bitmasks and enum flags
Bitmasks and enum flags
Very useful (and simple) binary tricks
LN
4 months ago
The control system in fps sample use this way too, can you write more about this topic
Lucas
published the article
Jul 30, 2018
Singleton ScriptableObjects
Singleton ScriptableObjects
How to set them up
Garrett Bates
Programmer
3 months ago
Is there a good way to abstract away some of the boiler plate of creating the singleton class and setting up the instance reference? Using a MonoBehaviour based approach I was able to have a type-generic Singleton base class that would do all the setup for the inheriting class. But applying a similar approach here, the method marked with [RuntimeInitializeOnLoad] never runs.
OJ
4 months ago
Reply@Funbites
I didn't know about the "[RuntimeInitializeOnLoadMethod]" attribute and I will start using it. Thanks! What I did before was to have a MonoBehaviour that initializes the Scriptable Objects in the Awake, but if you forget to add the references, you will get a lot of null exceptions. I suggest you to not use the singleton pattern at all. I have a Scriptable Object that shares memory for all AI agents (AISharedMemory). It's similar to your ItemDB singleton. I could have used a singleton because only the enemies had references to it, but I decided to use a reference to AISharedMemory in all AI related Components. When I needed to create a AI companion to help players, it was super simple. I made another instance of the AISharedMemory and add that instance to the AI companion Components in their prefabs. And it was simple as that. If I had used the singleton pattern, I would have to fix a lot of stuff in the code. If you think that setting up references for the most common case can be tedious, there's a simple way that Unity can help you with that. You need just to select the script that makes references and set the instance of the Scriptable Object in the inspector. Every time you create a new instance of the Component, it will make reference to that specific Scriptable Object.
I did not know that you could assign default references to scripts. That is very cool, thanks! When trying to access ScriptableObjects from pure c# classes you cannot work with such references, though. By the way, you do not need to initialize ScriptableObject in Awake. Just referencing it is enough to enable it.
OJ
4 months ago
Reply@old_pilgrim
ScriptableObjects have OnEnable() callback, so no need for RuntimeInitializeOnLoadMethod attribute.
OnEnable is only called when the ScriptableObject is referenced somewhere and, well, enabled. You can achieve this by selecting the asset in the project view or by adding a reference to it in some scene object.
o
5 months ago
ScriptableObjects have OnEnable() callback, so no need for RuntimeInitializeOnLoadMethod attribute.
View More Comments
Lucas
updated the article
Jul 16, 2018
Making a 2D movement script
How to write a simple 2D player controller
Lucas
published the article
Jul 16, 2018
Making a 2D movement script
How to write a simple 2D player controller
About Me
Coding is my hobby and I do it for fun. I have 1 year of experience in Unity and 1 year in UE4 ...
See more
Skills
Certifications (0)
Import
See all
Lucas's liked projects (3)
Following (18)
Followers (29)
Following Companies (0)
Not following anyone yet
Groups (0)
No groups