- Home /
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 :) 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
Answer by Kiloblargh · Feb 24, 2013 at 12:15 AM
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.
Answer by Peewee · Feb 24, 2013 at 12:49 AM
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);
}
}
Sure. Just duplicate the power bar object you've already done and after you set PowerBarLength, you can say LSBarLength = (PowerBarLength * .05). Easy peasy pumpkinsqueezy. You should probably declare curPower and maxPower as floats rather than ints. I personally dislike OnGUI so if I were making this, would simply use a scaled cube parented to the camera with 3dText for the labels.
Actually I think your main problem is that you need to take a step back and not worry about the script yet; just close Unity, open a blank text document and write down in plain English everything that you're intending to do. Then break it into smaller and smaller problems of what needs to happen to accomplish that. Don't try to design your game in $$anonymous$$onoDevelop as you go, you'll paint yourself into a corner and sooner or later have to redo everything from scratch anyway.
Thank you for your advise i do have a design document from which i am working from and it seems constantly adding to as new ideas hit me. What i'm working on is more of a prove of concept then a full fleshed out game.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093256im_/https://answers.unity.com/themes/thub/images/avi.jpg)