Jump to content

Any simple way to perform logic per-tick on the active vessel?


Recommended Posts

I'm working on a project that I'd rather not share until I know it'll work as I want it to, but first I need to know if it's possible to have code run constantly (only needs to be in-flight) for the active vessel. Do I need to use a PartModule to achieve this, and if so is it possible to ensure the vessel only has one of that module on it so the code doesn't run repeatedly?

(apologies if it's stated somewhere, I've looked for at least an hour in a lot of places)

Link to comment
Share on other sites

That is what the KSPAddon.StartUp property gives you.

From my RCS Landing Assist mod: (Link removed)

That hook will instantiate a copy of the RCSLandingAid class every time the flight scene starts and then KSP will run the Update() or FixedUpdate() methods to update them every tick.

Update() runs every time the screen (GUI) refreshes, input and interface stuff goes here. This can be highly variable, from 60 times a real world second all the way down to 3 or 4 times a second when the computer is struggling and it is doing a "slide-show".

FixedUpdate() runs every physics update, or 30 times per in-game (not real-world) second. Put stuff affecting a vessel or the game world in here.

Note that there is no correlation between Update() and FixedUpdate(), a high end computer with a small vessel could run 5 or 6 Update() frames for every FixedUpdate() frame, where-as a low end computer running a huge vessel could run more FixedUpdate() frames then Update() frames.

Also note that this method offers no way to save data, you have to hook into a scenario module in that game save or a part module on that vessel to persist data.

Hope that helps,

D.

Edited by Diazo
Link to comment
Share on other sites

That helps, though that leaves a couple questions:

- Do I need to put this onto a stock part, or is it automatically attached to a part (such as the root node)?

- Is it possible to get the delta of the tick that passed, or is OnFixedUpdate() guaranteed to have a fixed step of 1/30?

Link to comment
Share on other sites

That helps, though that leaves a couple questions:

- Do I need to put this onto a stock part, or is it automatically attached to a part (such as the root node)?

- Is it possible to get the delta of the tick that passed, or is OnFixedUpdate() guaranteed to have a fixed step of 1/30?

No, this is not attached to a part. The entire point of KSPAddon is to run code attached to the scene, not the part/vessel. This is also why it can't save data, it is not actually attached to an object.

Every time the flight scene starts, a new instance of RCSLandAid is created and every time you leave the flight scene it is destroyed.

The delta of tick that has passed is {something}.fixedDeltaTime

I can't remember which class it is in, but search for fixedDeltaTime in your IDE and it should be easily findable.

D.

Link to comment
Share on other sites

The physics update rate is 50 per second as the timestep is 0.02, the 30 mentioned is incorrect. You may want to see THIS for a better explanation of the physics update system.

It is unlikely you will need to bother much with that stuff anyways, unless you are doing something really specific.

Link to comment
Share on other sites

Alright, got it to work now, the fact you don't override the Start/Update functions threw me off. I can probably look up the remaining things, but I may as well ask here:

- Is there a way to check if the game is paused? (Update() still called when paused, but my code will not affect the vessel)

- I assume that data will not persist over each flight? (I don't want it to, so will I need to clear it out each time?)

- I will need to load sounds at some stage, is there a convention for doing so?

[edit] Thanks for that link, saves me time finding out how to check the delta.

Link to comment
Share on other sites

FixedUpdate() is 50 times a second? Okay. Either way, my point about trying to link data back and forth between the two methods still stands in that the relation between them will vary from computer to computer. Recently ran into an issue where a guy was assuming they ran on a one-to-one basis, so Update, FixedUpdate, Update, FixedUpdate, etc. and I wanted to be clear that is not the case.

On not overriding the Start() and Update() functions, KSP is a bit of an odd duck in that regard. My limited (and probably wrong) understanding of the situation is that KSP finds those methods via reflection and runs them as is so the override keyword does not have it's typical C# definition here.

Try somewhere in the HighLogic class for checking if the game is paused, I think there is also an OnGamePause event in the GameEvents class.

No, data does not persist. The class is destroyed when you exit the flight scene. One caution however is that static variables seem to persist due to their nature. I had some weirdness with that and I had to add some "reset to default" code in the OnDisable() method so things would have the values I expected the next time the flight scene was entered.

And I have no clue about sounds, I've never touched them. Try searching for a sounds mod to see how they handled it maybe?

D.

Edited by Diazo
Link to comment
Share on other sites

  • 2 weeks later...

I just want to point out that the Awake, Start, Update, FixedUpdate, OnGUI, OnDestory functions are in fact called by the unity engine that KSP runs on instead of KSP it's self. Unity has a lot of documentation online so that would be a good place to start. The only down side is because KSP is a production build you are very limited with what you can do with the unity editor, for when I'm messing around with plugins I don't even use unity, only Visual Studio. http://docs.unity3d.com/ScriptReference

On the note of being able to save data from one instance to another there is the PluginConfiguration class (from KSP's KSP.IO name space). I'm not sure about the correct time to load the config but it seems to work find in the class constructor, and you can save the config in the OnDestroy method.

Hope this helps.

edit:

Another thing to note is about the time you spend in the various Update methods. For example taking too long in a FixedUpdate call could cause KSP to lag the physics causing KSP's performance to suffer. I'm sure everyone know of the clock in the upper left corner of the screen if it turns red bad things are bound to happen.

There is also InvokeRepeating if you'd like to create your own "FixedUpdate" like method. It will instruct Unity to call a method at a custom fixed rate

Edited by dazoe
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...