When making a game were some values are going up constantly, even when the player is in unrelated scenes (could be a manabar or a game-resource), it is my impression that a singleton is a good solution.
At least I’ve managed to make that work.
I have made a singleton dictating that the mana can go from 0-1000 and that you gain 1 mana per second, but what if I then want to add two buildings to my main scene, that each can be upgraded further and each give the player an additional 0.5 mana per sec per building level/upgrade?
At first I just made the above project in one scene and it was unproblematic to add a code that took an array of buildings (the two I created) and added their “boost * count” to the “mana per sec”.
But I am unsure how to proceed when multiple scenes is available.
If i make a buildingManager script with a public variable for the two buildings and put them in my main scene, and have my singleton refere to them, having them boost the singletons “mana per second”, will that boost then persist when changing scene or will the object reference stop working?
And if so how do I fix that?
By making the building variables static or into singletons?
If this is the case wont most values end up being static? I’ve read on several occasions that I should try to limit my static use, so I’m a bit confused on how to prevent that.
DontDestroyOnLoad is appear problematic to me, since I dont want the buildings visually represented in all scenes. If I by some code make them concealed, wont them constantly running just draw more CPU than a static value?
I’m not sure I really understand. But if you are adding “buildings” in multiple scenes and each one adds a boost to mana gained, you just need to add a value that you use to increment. This can be part of your singleton if you are already using it. So, if I have a variable manaPerSec and it starts at 1f. Then I build a building that adds .5. So I change manaPerSec to 1.5f. Now, if I destroy that building, manaPerSec is set back to 1f.
If I build a building in one scene and another building in another scene, it would be 2f. Now, if this is not the desired result and the scenes shouldn’t increase the mana gained on each other, then manaPerSec should be tracked in the scene and probably not on the singleton.
Even if your buildings multiply the amount, you just set the amount. And you handle it pretty much the same way.
It’s starting to look like you should just not be unloading the previous scene. You can load scenes additively, and just deactivate the old scenes’ objects if needed. If they’re deactivated, they still can be referenced by your resource manager.
A deactivated GameObject costs you nothing but memory.
It’s the other way around. I have two (could be more) buildings/upgrades in my main scene, that boost a the gain per second of a resource that I want available in my other scene (in the example mana).
When just having one scene I managed to do so, by having a manaManager script, refering to a buildingManager script array, and then I just added the relevant buildings and added the “building count/level * boostingEfficiancy” per building to my mana per second.
Wow I didn’t know that was actually something you could do in unity.
That does indeed sound like the best solution, for my current project. I already tried to do so by having multiple canvases but found it less appealing that multiple scenes.
Reading up on it, it sounds like loading a new scene additively adds the new scenes objects to the old ones though.
To describe my scenario more precisely.
In my game I have my main scene - the town scene. In this scene you do the following:
spend and regenerate a mana-like source called power (I use actual mana in another scene).
Harvest the resource wood and stone, which can be spend on various building that in turn increase the wood/sec and stone/sec gain. This resource can also be spend on other things in other scenes (which is why I need both the amount of resources and the modified resource/sec to be constantly ticking.
I also have a combat scene. In this scene you can do the following:
spend and regenerate mana.
spend resources wood/stone on various upgrades/buildings.
Gather experience by doing combat. Now experience can be spend in a third scene, that like the other two scenes grant a 4th type of resource, that in turn can be spend on upgrades for both the town and combat scene.
I need all the numbers and their counters to remain active in all scenes, even though I dont necessarily want them represented, but I’m am unsure which way is the best to do so.
Sure does. But you can then deactivate your “town scene” objects, and it’s as if the scene was unloaded - but your mana counter can still see them, and when you reactivate them, your buildings will be right where you left them.
Which is why I mentioned you have a manaPerSec variable. If you are adding buildings in one scene that increment this value based on what is built (or subtract if buildings are destroyed) then you have a value that you can use to add to your base mana every second. You don’t have to care what buildings exist in your town scene in your combat scene. You only have to know that you are adding 4 manaPerSec to keep it ticking.
Unless there is a special reason you need to know what buildings exist and I’m just overlooking it.
yeah okay
I will look into that then!
Why would you not always use this function if the scenes are somewhat related? (perhaps you would always. Just never seen it mentioned before)
No I think we’re talking about the same thing
The problem I’ve been having - undoubtedly cause by a lack of programming-insight/skills - is that when I’ve made the manaPerSec script it still has to get the values from somewhere, and in this case it gets them from the buildings…
So even though the manaPerSec script is still there when changing scenes the buildings it is gettings it’s numbers from have been destroyed.
And if I just set the manaPerSec to 4, then building the buildings would have no effect
Memory. If you’re on platform with unforgiving memory constraints (WebGL and mobile), then you want to unload scenes as aggressively as you can, and find other ways to store the data you need.
Which approach would use most energy then? Having the seperate scenes, but running them all at once (while only one visible at a time) or making every form of resource and every object boosting that resource into singletons?
Seems like the last option will require fewer objects to be running constantly (although still alot) but I don’t know if singletons for some reason require more memory?
If your manaPerSec has a base of 1 and you build 3 buildings that each add 1 manaPerSec, you now have 4 manaPerSec. You have no reason to care what the buildings are. You have a manaPerSec of 4. If you leave the scene with your buildings, you still have a manaPerSec of 4. You don’t get the values from the buildings each second. You just have one value that you add or subtract from based on what your buildings should provide. You know what they provide when they are built, so you add that amount to a total per sec value.
Either I’m not explaining it well or I’m confused as to why you need to track your buildings that way.
Ah… Think I might understand you now
So as long as I dont use “void update” to keep track of my mana per sec, the boost from the buildings will remain even after destroyed?
If I use void update it would revert to 1 right? I think I might have done that -_-