- Home /
How to seperate game calculations / mechanics from Graphic/UI?
Hello Community!
First to say, I am a beginner to Unity.
Let's say we have game values that have to be calculated in the background and have nothing to do directly with a game object except of showing the value (for example date calculations in simulation or tycoon games).
In my understanding the date has to be calculated in the background, but should not be attached to any game object (except of the UI-object showing it).
How do I seperate those tiers? I don't want my UI to know anything about the background calculations. In an extreme case I just want to retrieve the values and show them on a console if want to.
UI elements don't know anything. The just do what you tell them to. Why do you think this would even be an issue? You tell the UI what to display. It doesn't matter what gameobject that code is running on.
Yes, you are right. But where do I do those calculations if I don't want to attach the scripts to the gameobjects?
I can of course attach the "dateCalculation.cs" to the DateUI object. But when I need the date at any other place (for trigger events or something) I have to know, that I attached it to the DateUI so I can retrieve it from there, don't I?
hello not sure if you are talking about managers, for example an empty gameobject that can be used as a manager, and can have a Dat$$anonymous$$anager script, so that every object can retrieve the information needed, or you are talking about frontend-backend segregation? then we would be talking about code structure, there is no one way of doing this, but for that example i would have a Date$$anonymous$$anager class, with inside a DateCalculation non-monobehaviour class that only does the date calculation nothing else. i usually have external classes that vinculate the frontend and backend, for example i have a method called UpdateTimeUI so i update at the time the UI text, images or whatever UI elemts you prefer, but ofc this is only aa solution and i use it because it makes my workarround with databases and socket connection easier. but i have seen really well done games that just mix everything in one big class as it was Celeste thats a great game with a lot of prizes. thats just up to you.