Skip to main content
gamedev.net
🔒 Locked

[.net] Global access

Started by dabo 6 replies 1.3k views
Original Post
dabo
dabo
Hello,

In my current project I have a class that (among other things) handles time. Time is a central part and I need just about every other class to know about the time as well. How would you do in order to achieve this global access to time? I guess I could use a singleton but are there better ideas?

Thanks
dabo
dabo
Yeah a DateTime will do fine. Time is not related to the real time in any way it is just used for my simulator. For example by pressing a button you will proceed to the next day and depending on the time (and date) specific events will occur. That's why I need the current time to be accessible from almost anywhere.

Will have another look at the design, perhaps it is bad.
ChaosEngine
ChaosEngine
Quote:
Original post by dabo
That's why I need the current time to be accessible from almost anywhere.

emphasis mine.

When you say "current time" do you mean actual real world time of day or in game time?

For real world time, you can use DateTime.Now.

Otherwise as Telastyn said, a static property/method should be fine.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Hodgman
Hodgman
You could pass it as an argument to any function that needs it.
TheTroll
TheTroll
In this case because time is everywhere I would use a static.

But I would only allow a single class to make changes to time.

theTroll
dabo
dabo
Quote:
Original post by ChaosEngine
Quote:
Original post by dabo
That's why I need the current time to be accessible from almost anywhere.

emphasis mine.

When you say "current time" do you mean actual real world time of day or in game time?

For real world time, you can use DateTime.Now.

Otherwise as Telastyn said, a static property/method should be fine.


Sorry if I confuse you, I never care about real world time. When I wrote "current time" I meant current time in the simulator.

Quote:
Original post by TheTroll
In this case because time is everywhere I would use a static.

But I would only allow a single class to make changes to time.

theTroll


Yeah that is how I am using it, the other classes just read the time.
Wan
Wan
Quote:
Original post by Hodgman
You could pass it as an argument to any function that needs it.

I'd favor this, it doesn't feel right to have all your 'objects' depend on and know about some global time variable. I can imagine an instant replay situation in a racing game where you replay a crash that just happened before continuing the current state. Now you have to store the current time somewhere, replay the crash my rewinding and then incrementing the global time, and then resetting it again to 'now'.

YMMV though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.