- Home /
Simplifying code - similar array names
I would like to know if there is a good way to simplify my two routines into one. They both use exactly the same code apart from differently named arrays and gameobjects. It would be good to be able to specify a name to use as the array name such as 'logs' or 'sticks' when the routine is run so that I can have the option to add different values. This would really help with the efficiency of my code. Hope someone can help. Thanks.
Here is my code:
The two routines are called AddToWood & AddToSticks
public GameObject logsStackPrefab;
public GameObject[] logsStacks;
public GameObject sticksStackPrefab;
public GameObject[] sticksStacks;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
for (int i = 0; i < invSlots.Length; i++) {
if (invSlots[i].transform.childCount < 1) {
slotBools [i] = false;
} else {
slotBools [i] = true;
}
}
if (Input.GetKeyDown (KeyCode.Q)) {
AddToWood (value: 40);
}
if (Input.GetKeyDown (KeyCode.R)) {
AddToSticks (value: 10);
}
}
public void AddToWood(int value) {
bool needNewStack = false;
int remainder = 0;
for (int s = 0; s < logsStacks.Length; s++) {
Debug.Log ("s = " + s);
if (logsStacks[s] == null) {
if (s == 0) {
needNewStack = true;
} else if (s != 0 && logsStacks[s - 1] != null && logsStacks[s - 1].gameObject.GetComponent<InventorySlot>().value == 50) {
needNewStack = true;
}
} else {
needNewStack = false;
}
if (needNewStack) {
for (int d = 0; d < invSlots.Length; d++) {
if (!slotBools[d]) {
GameObject newStack = Instantiate (logsStackPrefab, invSlots[d].transform.position, invSlots[d].transform.rotation) as GameObject;
logsStacks[s] = newStack;
newStack.transform.SetParent (invSlots[d].transform);
newStack.transform.localScale = invSlots[d].transform.localScale;
d = invSlots.Length;
}
}
}
Debug.Log ("woodStacks[s] = " + logsStacks[s]);
if (logsStacks[s] == null)
s = logsStacks.Length;
if (remainder > 0) {
Debug.Log ("Recognised that remainder is more than 0");
logsStacks[s].gameObject.GetComponent<InventorySlot>().value += remainder;
remainder = 0;
s = logsStacks.Length;
} else if (logsStacks[s].gameObject.GetComponent<InventorySlot>().value < 50) {
Debug.Log ("Less than 50");
logsStacks[s].gameObject.GetComponent<InventorySlot>().value += value;
Debug.Log ("Added");
if (logsStacks[s].gameObject.GetComponent<InventorySlot>().value > 50) {
Debug.Log ("Now Over 50");
remainder = logsStacks[s].gameObject.GetComponent<InventorySlot>().value - 50;
logsStacks[s].gameObject.GetComponent<InventorySlot>().value = 50;
Debug.Log ("Remainder = " + remainder);
} else {
s = logsStacks.Length;
}
}
}
}
public void AddToSticks(int value) {
bool needNewStack = false;
int remainder = 0;
for (int s = 0; s < sticksStacks.Length; s++) {
Debug.Log ("s = " + s);
if (sticksStacks[s] == null) {
if (s == 0) {
needNewStack = true;
} else if (s != 0 && sticksStacks[s - 1] != null && sticksStacks[s - 1].gameObject.GetComponent<InventorySlot>().value == 50) {
needNewStack = true;
}
} else {
needNewStack = false;
}
if (needNewStack) {
for (int d = 0; d < invSlots.Length; d++) {
if (!slotBools[d]) {
GameObject newStack = Instantiate (sticksStackPrefab, invSlots[d].transform.position, invSlots[d].transform.rotation) as GameObject;
sticksStacks[s] = newStack;
newStack.transform.SetParent (invSlots[d].transform);
newStack.transform.localScale = invSlots[d].transform.localScale;
d = invSlots.Length;
}
}
}
Debug.Log ("sticksStacks[s] = " + sticksStacks[s]);
if (sticksStacks[s] == null)
s = sticksStacks.Length;
if (remainder > 0) {
Debug.Log ("Recognised that remainder is more than 0");
sticksStacks[s].gameObject.GetComponent<InventorySlot>().value += remainder;
remainder = 0;
s = sticksStacks.Length;
} else if (sticksStacks[s].gameObject.GetComponent<InventorySlot>().value < 50) {
Debug.Log ("Less than 50");
sticksStacks[s].gameObject.GetComponent<InventorySlot>().value += value;
Debug.Log ("Added");
if (sticksStacks[s].gameObject.GetComponent<InventorySlot>().value > 50) {
Debug.Log ("Now Over 50");
remainder = sticksStacks[s].gameObject.GetComponent<InventorySlot>().value - 50;
sticksStacks[s].gameObject.GetComponent<InventorySlot>().value = 50;
Debug.Log ("Remainder = " + remainder);
} else {
s = sticksStacks.Length;
}
}
}
}
Answer by allenallenallen · Jul 30, 2015 at 11:17 AM
You can combine AddToWood and AddToSticks into one AddToThing.
void AddToThing(int value, GameObject stackPrefab, GameObject[] stacks){
// Same code here; just change the variable names to the 2 extra variables I added above.
}
And you can call this function using:
AddToThing(40, logsStackPrefab, logsStacks); // For your logs.
AddToThing(10, sticksStackPrefab, sticksStacks); // For your sticks.
Now both items are just using one single function.
Your answer
Follow this Question
Related Questions
Variable not accesible 2 Answers
How to convert a 'string' to the name of a 'void' function? 1 Answer
How to instantiate a random object from an array? 3 Answers
IndexOutOfRangeException for initial spawn of prefabs from an array 1 Answer
Implementing a 2D array as a field on a ScriptableObject? 1 Answer