Jump to content

Adding resource on vessel load


Recommended Posts

I think that the OnVesselUnpack game event will work. I'm using this and it runs on vessels that come into range during flight, but it also runs for the vessel that has focus when the flightscene loads.


//in a class that runs on during flight mode, either Scenario or KSPAddon.Flight work

public void Start()
{
GameEvents.onVesselGoOffRails.Add(VesselOffRails);
}

public void VesselOffRails(Vessel vsl) //this runs when Vessel vsl comes off rails and loads
{
//add code to add resource to Vessel vsl here
}

D.

Link to comment
Share on other sites

Erg, I've never dealt with resources myself.

Maybe something like this?

Warning: Pseudo-code ahead


public void VesselOffRails()
{
foreach(Part p in vsl.Parts) //cycle through vessel parts
{
if(p.hasResourceStorage == true && p.resource.current < p.resource.max)
{
p.resource.addToPart = Math.Max(p.resource.max - p.resource.current,resourceToAdd) //error trap so you never add more resources then you have left to add to vessel
resourceToAdd = resourceToAdd - (p.resource.max - p.resource.current) //resource amount to add to vessel is calculated elsewhere
if(resourceToAdd <= 0) //check if we have any resources left to add, if not we are done so stop cycling through vessel parts
{
goto BreakOut;
}
}
}
}
BreakOut:
print("Done Adding Resources");
//done cycling through all parts on vessel, if resourceToAdd > 0, those resource are lost due to lack of storage

I think that makes sense?

D.

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