Starship power core help needed please.

Hi everyone,

I need a little help with a scripting problem. I have been searching for answers on the forums and here but with little luck.

What i’m attempting to do is create a Power source system for example a starship has a power core which produces X amount of total power and then ship system’s draw power from the power grid which drains power from the powercore and then be able to display this information on a computer console in game that the player can access to adjust power settings depending on the situation.

As i understanding it (I am rather new to scripting but slowly getting there :slight_smile: i’ll need a script that controls the power core itself and a script that represents various power nodes around the ship (think of a star trek ship you have the warp core and the power grid with various eps junctions through out the ship) and then a script which can be used for the various ships systems (life support, lights, engines, weapons, shields etc)

If anyone can point me in the right direction i would be grateful.

Thanks Pete

That’s very easy to do, and you don’t need more than one script. It just comes down to arithmetic.

Have the max power consumption of each of the systems as a variable. Then multiply each by what percent of capacity it is currently running at. Add them all up. Subtract the total from the generator output multiplied by what percent of its capacity it is running at. Got a positive number?
Great, all systems go. No? Then scale down the power available to each system by the ratio of what you’ve got to what you need.

For weapons, they should charge up. The bigger the guns and the faster they charge, the more power they will use, and when they are done charging, they don’t consume any more power until they are fired.

Using InvokeRepeating can just add some amount to the “weapon battery” variable a few times a second until it reaches the maximum value, then CancelInvoke on that, set the “charge weapon” system’s draw to 0, and then either wait for the player to fire or automatically fire, then you turn back on the charge weapon system.

fwiw, if I were designing a starship, I wouldn’t make the power distribution something that needs to be manually controlled; it should adapt automatically to circumstances (more scripting…) until you override it.

Thanks for your answer Kilo,

Below is the script as it sits atm it displays what will be my ships power bar on screen but i’m lost as to what to do now. For example say i want life support to use 5% of available power and display that on screen? Is there any script examples you know of that would help me to understand how to impalement that?

Thanks Pete

using UnityEngine;
using System.Collections;

public class Warpcore : MonoBehaviour {
	public int maxPower = 100;
	public int curPower = 100;
	
	public float PowerBarLength;
	
	

	// Use this for initialization
	void Start () {
		PowerBarLength = Screen.width / 3;
	
	}
	
	// Update is called once per frame
	void Update () {
		AdjustCurrentPower(0);
	
	}
	
	void OnGUI(){
		GUI.Box(new Rect(10, 590, PowerBarLength, 20), curPower + "/" + maxPower);
	}
	
	public void AdjustCurrentPower(int adj){
		curPower += adj;
		
		if(curPower < 0)
			curPower =0;
		
		if(curPower > maxPower)
			curPower = maxPower;
		
		if (maxPower < 1)
			maxPower = 1;
		
		PowerBarLength = (Screen.width / 3) *(curPower / (float)maxPower);
	}
}