- Home /
How can I improve my trading script?
I am making a simple traiding script, were the player can buy and sell products in different towns. I am using UI buttons and elements for most aktions, but the script is getting really long and it is a lit if work to write all the wariables again and again for every city. Is there a way to improve the code?
Here is my script so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryManager : MonoBehaviour {
//textelements
public Text Cointext;
//playergoods
public Text sakeplayer;
public Text fishplayer;
public Text toolsplayer;
public Text ironoreplayer;
public Text woodplayer;
//Player
public float sake = 0f;
public float fish = 0f;
public float tools = 0f;
public float ironore = 0f;
public float wood = 0f;
public float coins = 100;
//Town1
//Produces Sake and Tools needs Wood and Iron
public float sakeT1; //p
public float fishT1;
public float toolsT1; //p
public float ironoreT1; //n
public float woodT1; //n
public float sakepriceT1 = 5f;
public float fishpriceT1 = 10f;
public float toolspriceT1 = 25f;
public float ironorepriceT1 = 15f;
public float woodpriceT1 = 15f;
public float sakesellpriceT1 = 3f;
public float fishsellpriceT1 = 7f;
public float toolssellpriceT1 = 15f;
public float ironoresellpriceT1 = 10f;
public float woodsellpriceT1 = 10f;
void Start() {
//playertext
Cointext.GetComponent<Text>();
sakeplayer.GetComponent<Text>();
fishplayer.GetComponent<Text>();
toolsplayer.GetComponent<Text>();
ironoreplayer.GetComponent<Text>();
woodplayer.GetComponent<Text>();
//town1
}
void Update () {
//playertext
Cointext.text = coins.ToString();
sakeplayer.text = sake.ToString();
fishplayer.text = fish.ToString();
toolsplayer.text = tools.ToString();
ironoreplayer.text = ironore.ToString();
woodplayer.text = wood.ToString();
}
//Town1
public void BsakeT1()
{
if (sakeT1 >= 1)
{
sakeT1 -= 1;
sake += 1;
coins -= sakepriceT1;
}
}
public void SsakeT1()
{
if (sake >= 1)
{
sakeT1 += 1;
sake -= 1;
coins += sakesellpriceT1;
}
}
public void BfishT1()
{
if (fishT1 >= 1)
{
fishT1 -= 1;
fish += 1;
coins -= fishpriceT1;
}
}
public void SfishT1()
{
if (fish >= 1)
{
fishT1 += 1;
fish -= 1;
coins += fishsellpriceT1;
}
}
public void BtoolsT1()
{
if (toolsT1 >= 1)
{
toolsT1 -= 1;
tools += 1;
coins -= toolspriceT1;
}
}
public void StoolsT1()
{
if (tools >= 1)
{
toolsT1 += 1;
tools -= 1;
coins += toolssellpriceT1;
}
}
public void BironoreT1()
{
if (ironoreT1 >= 1)
{
ironoreT1 -= 1;
ironore += 1;
coins -= ironorepriceT1;
}
}
public void SironoreT1()
{
if (ironore >= 1)
{
ironoreT1 += 1;
ironore -= 1;
coins += ironoresellpriceT1;
}
}
public void BwoodT1()
{
if (woodT1 >= 1)
{
woodT1 -= 1;
wood += 1;
coins -= woodpriceT1;
}
}
public void SwoodT1()
{
if (wood >= 1)
{
woodT1 += 1;
wood -= 1;
coins += woodsellpriceT1;
}
}
}
`
@ Leon123442 - Very few have eyes to read that... maybe format your code with code block?
Answer by tormentoarmagedoom · Aug 25, 2018 at 04:18 PM
Good day.
You should make a general script to make all transactions. Create a general method to do it, using strings to access the variable you need:
if...
string something = "Wood";
float someValue = 21;
methodName(something, someValue);
void methodName (string Resource, float Cost)
{
if (GetType().GetField(Resource).GetValue() >= Cost)
{
GetType().GetField(Resource+"T1").GetValue() += Cost;
GetType().GetField(Resource).GetValue() -= Cost;
coins += woodsellpriceT1;
}
}
I did not check the logic of the code, is just an example to show you how to replace each variable for a general variable that reads / edit (with SetValue) the values of other variables.
Go read, wach and learn about it.
Good luck!
Your answer
Follow this Question
Related Questions
non-static Variable in script on multiple gameObjects reset. Why? 1 Answer
How to READ a variable VALUE from other Object Script? 1 Answer
Changing a public varible from another script 3 Answers
C# taking variable from other script 2 Answers
Variables modified on other scripts through a Editor Script reset on Play? 1 Answer