- Home /
how to make an upgrade interface
Hey I'm working on a game where you can upgrade the main weapons damage, fire rate, and the explosion radius of the bullet with skill points that you gain after every round and I'm not really sure where to start with the scripting I already have a menu for it just not sure how to fit everything together.
Answer by Cornelis-de-Jager · Nov 12, 2019 at 10:37 PM
Ok I haven't done one before, but lets brain storm this;
Firstly, you'll need a class to actually handle everything. So lets make a static class that you can use from anywhere.
// We'll make this static so it can be referenced from anywhere
public class UpgradeInterface : MonoBehaviour {
// Variables
private static UpgradeInterface _isntance;
public static UpgradeInterface UpgradeInterface { get { return _isntance; }}
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(this.gameObject);
} else {
_instance = this;
}
}
// Additional Code Goes here
}
usage:
UpgradeInterface.ExampleFunction();
Well whats the first thing you need to do when you want to upgrade? Open the menu ofcourse... When you open the menu you need to load the data you are going to use. So write a peice of code that will fetch and send it to a script that will control the upgrading. We'll just add it to the script above:
// More Variables
GunStats gunStats;
// Load function
public void loadMenu (GunStats gunStats) =>
this.gunStats = gunStats;
usage:
// Note gun stats are the current stats on your gun
UpgradeInterface.loadMenu(gunStats);
Now that stats are loaded. But you also need logic to decide how many points are available to spend. So lets add a variable and function to add points;
// More Variables
private int upgradePoints = 0;
// Add points function
public void AddPoints (int points) =>
upgradePoints == points;
Almost done with the script. The next thing we need now is the ability to actually increase and decrease the stats. You'll have to make a function for each attribute of the gun. Below we'll only add one attibute, the damage one. Currently it onyl increases, but you can later pass down a variable if you want it increase and decrease.
// Increase Damage Attibute - simply adding one point
public void AddDamage(){
if (upgradePoints > 0){
this.gunStats.Damage++;
this.upgradePoints--;
}
}
After this is done you need to link the UI to the scripts. At this stage you are able to upgrade your gun... but now thats its upgraded you need to return the gunStats and make it permanant; The following function should be called when you are done upgrading the stats.
// get the stats after done upgrading.
public GunStats GetCurrentGunStats () => gunStats;
Technically you are done now. But lets add some feedback functions so we can see how many points we have left to spend.
// Get Poitns Left
public float GetUpgradePointsLeft () => upgradePoints;
now the hardest park will be hooking up the UI since it is just tedious. But the basic flow of this class should look like;
// Click to open menu
==> loadMenu
// Click on buttons to add points if you have available
=> AddAttribute()
=> AddAttribute()
=> AddAttribute()
// Click done
==> GetUpgradePointsLeft
This is a very simple menu system, but can be expanded from here.
when i try public static UpgradeInterface UpgradeInterface { get { return _isntance; }} I get an 'UpgradeInterface': member names can not be the same as their enclosing type error
Remove ": $$anonymous$$onobehaviour"
I just tried and tested it. Plus this is a know pattern, can you please post your code here.
same person btw just changed my name