Jump to content

OnNewYear GameEvent


Recommended Posts

Hi all,

I'dd like to setup an action on each new year, the 1st of January.
I've never moded KSP before, but have knowledge in C# and Unity.
I have not found any pre-existing events, but suppose I could use the 'GameEvents Extension' (below link) to create such an event.

Could someone point me in the right direction on how one would setup such an event?

Thanks!

Link to comment
Share on other sites

You can get the year from https://kerbalspaceprogram.com/api/interface_i_date_time_formatter.html#aaee82ba1c754418e36468db93bdbd0c3 and do a comparison every tick. Store the current year and compare it to the return value of that, and if they are different, the year has gone up, which you can use to call the method that you need to run. 

I can’t speak about the gameevents, maybe someone else has a different solution. 

Link to comment
Share on other sites

There is no OnNewYear even in KSP currently. So if you want to add one then you will have to check the year each frame to trigger your new event.

You may not be aware of that but doing something like "if (lastCheckYear != Time.Year) {stuff}" 60 time per seconds is the kind of things a 1978 8086 at 5 MHz could do fine without even noticing.If you think that it is too much for a "modern" PC then you will be surprised by the staggering amount of code that a game like KSP execute each frame.

The new event system you linked is when a mod want to create an even other mods may want to listen too. Not triggering something 

Link to comment
Share on other sites

15 hours ago, sarbian said:

There is no OnNewYear even in KSP currently. So if you want to add one then you will have to check the year each frame to trigger your new event.

You may not be aware of that but doing something like "if (lastCheckYear != Time.Year) {stuff}" 60 time per seconds is the kind of things a 1978 8086 at 5 MHz could do fine without even noticing.If you think that it is too much for a "modern" PC then you will be surprised by the staggering amount of code that a game like KSP execute each frame.

The new event system you linked is when a mod want to create an even other mods may want to listen too. Not triggering something 

Tanks for your input.

I thought that if that event system is usable to communicate between mods, it could probably work too from within the same mod too. Of course, this depends on how KSP devs implemented this event system, if it runs itself during Update calls, it is then not any more usefull in my case than Update (and would not be a real event system IMO).


Yes, I do know how the Update functions work, and of course this simple check is very small. I really just thought it be best practice/habit to leave the least possible things in Update as possible. Bad habits often pile up, seeing some other games code time to time on the Unity forums can show how much bad usage of Update is frequent... We agree of course my usage is minuscule in this case

bests!

Link to comment
Share on other sites

If you want to check for something less frequently than in Update () you can use a coroutine, set it to run each x second.

Pro tip : it may be necessary to change the coroutine timing based on Time-Wrap so you sure you don't miss the new year.

Link to comment
Share on other sites

That depends on how the game is actually paused. If the Unity Time is just set to 0 then WaitForSeconds will stop running.

If you use something different, like your own timer, or a frame counter, or maybe WaitForSecondsRealTime then the coroutine will continue.

And if you're worried about unnecessary performance overhead make sure to cache the WaitForSeconds if you use it. So:

private IEnumerator NewYears()
{
	WaitForSeconds wait = new WaitForSeconds(0.3f);

	while (whatever)
	{
		//Do stuff

		yield return wait;
	}
}

Instead of:

private IEnumerator NewYears()
{
	while (whatever)
	{
		//Do stuff

		yield return new WaitForSeconds(0.3f);
	}
}

But really, all of this is probably either not worth the trouble or more overhead than just checking in Update.

Link to comment
Share on other sites

 

11 minutes ago, DMagic said:

But really, all of this is probably either not worth the trouble or more overhead than just checking in Update.

Yep. I don't see how a coroutine that will check its condition each frame and then run the year check would be faster than actually doing the year check. And that "yield return new WaitForSeconds(0.3f);" allocate memory each time it is run, which makes that use case even worse.

Link to comment
Share on other sites

1 hour ago, sarbian said:

Yep. I don't see how a coroutine that will check its condition each frame and then run the year check would be faster than actually doing the year check. And that "yield return new WaitForSeconds(0.3f);" allocate memory each time it is run, which makes that use case even worse.

I think one of the classic programming quotes applies here:

Quote

“ Rules of Optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet.
 ” - Michael A. Jackson

 

Link to comment
Share on other sites

Thanks to all,
You all certainly are correct, I'm trying to optimize something that I don't need to. Thanks.
Just have to find how to attack Contract Configurator now in my code, I think it will be easier just to do a new CC branch completely. But that is a different problem.
Cheers!

Link to comment
Share on other sites

This was an interesting read and stuff there that I can use too. Thanks for putting the question out there.

As an aside: Because Sarbian, Dmagic, Li0N & Nightingale all responded in the one thread, does that count as Developer "Bingo!"? :D

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

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.

×
×
  • Create New...