Sign in to follow this  
Bodymenn

C# GetTime bug?

Recommended Posts

Hey guys, i have setup a RealTime script to sync the server world time with the realtime, but if i want to check the Time the time will return wrong numbers.
 

        [Command("settime")]
        public void Settime(Client player, int hrs, int min)
        {
            NAPI.World.SetTime(hrs, min, 0);
            NAPI.Chat.SendChatMessageToPlayer(player, $"~g~[WELT ZEIT]~w~ Du hast die Zeit auf: ~y~{hrs}:{min}~w~Uhr gestellt.");
        }
        [Command("time")]
        public void Time(Client player)
        {
            NAPI.Chat.SendChatMessageToPlayer(player, $"~g~[SERVER]~w~ Aktuell ist es {NAPI.World.GetTime().ToString()} Uhr");
        }
    }

6efc7edd8e66f8774de47d50d7d832c6.png

btw. every minute the server will set the world time to real time.

NAPI.World.SetTime(DateTime.Now.Hour, DateTime.Now.Minute, 0);

 

Edited by Bodymenn
  • Confused 1

Share this post


Link to post
Share on other sites

You need to create global variables, or data with custom hour, minute and second.

 

int serverHour = 12;
int serverMin = 0;
int serverSec = 0;

private void C_SetTime(int hour, int minute, int second)
{
    NAPI.World.SetTime(hour, minute, second);
    serverHour = hour;
    serverMin = minute;
    serverSec = second;
}

 

PS: I know this thread is old, but it was unanswered and could still help for others looking to reference from it. If I am in the wrong, then please delete this and lock the thread.

Share this post


Link to post
Share on other sites
Posted (edited)

Thread isn't that old, I had this problem the other day as well but only just seen this thread. Pretty fkin annoying until I booted the game up.

I was trying to build the script without loading the game and viewing the consoles SetTime > GetTime.

GetTime is random on first call, could be 12:00, 13:00, 14:00 etc, whatever it just picks a time it fancies.

 

		//Store time here
        private TimeSpan _startTime;        

		/// <summary>
        /// External method to get the time because <see cref="NAPI.World.GetTime"/> is no good
        /// </summary>
        /// <returns></returns>
        public TimeSpan GetTime() => _startTime;
        
        #region Private methods

        private void OnTimerCallback(object state)
        {
            _startTime = _startTime.Add(new TimeSpan(0, 1, 0));
            NAPI.World.SetTime(_startTime.Hours, _startTime.Minutes, _startTime.Seconds);
        }

 

 

Edited by horseyhorsey

Share this post


Link to post
Share on other sites
On 1/6/2019 at 4:58 AM, Marky said:

You need to create global variables, or data with custom hour, minute and second.

 

int serverHour = 12;
int serverMin = 0;
int serverSec = 0;

private void C_SetTime(int hour, int minute, int second)
{
    NAPI.World.SetTime(hour, minute, second);
    serverHour = hour;
    serverMin = minute;
    serverSec = second;
}

 

PS: I know this thread is old, but it was unanswered and could still help for others looking to reference from it. If I am in the wrong, then please delete this and lock the thread.

To my knowledge, Marky's code seems legit. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.