Executing variables as code?
So i have this piece of code that is working like i want it to but its rather cumbersome. I wonder if there is a way to do this with a loop since al the lines are kinda the same except for some minor differences. If there is a way to use variables for these minor differences so i dont have to copy them manually it would be very nice.
Here is the code:
public void UpdateMenu(){
Delay = 0;
if (CurrentScroll > (Player.Inventory.Count-1)){
Line1.text = "Nothing here scrub.....";
}
else {
Line1.text = Player.Inventory[0+CurrentScroll].amount + " units of " + Player.Inventory[0+CurrentScroll].name;
ResSp1.sprite = IDB.itemDB[0 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-2)){
Line2.text = "-----------------------";
}
else {
Line2.text = Player.Inventory[1+CurrentScroll].amount + " units of " + Player.Inventory[1+CurrentScroll].name;
ResSp2.sprite = IDB.itemDB[1 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-3)){
Line3.text = "-----------------------";
}
else {
Line3.text = Player.Inventory[2+CurrentScroll].amount + " units of " + Player.Inventory[2+CurrentScroll].name;
ResSp3.sprite = IDB.itemDB[2 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-4)){
Line4.text = "-----------------------";
}
else {
Line4.text = Player.Inventory[3+CurrentScroll].amount + " units of " + Player.Inventory[3+CurrentScroll].name;
ResSp4.sprite = IDB.itemDB[3 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-5)){
Line5.text = "-----------------------";
}
else {
Line5.text = Player.Inventory[4+CurrentScroll].amount + " units of " + Player.Inventory[4+CurrentScroll].name;
ResSp5.sprite = IDB.itemDB[4 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-6)){
Line6.text = "-----------------------";
}
else {
Line6.text = Player.Inventory[5+CurrentScroll].amount + " units of " + Player.Inventory[5+CurrentScroll].name;
ResSp6.sprite = IDB.itemDB[5 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-7)){
Line7.text = "-----------------------";
}
else {
Line7.text = Player.Inventory[6+CurrentScroll].amount + " units of " + Player.Inventory[6+CurrentScroll].name;
ResSp7.sprite = IDB.itemDB[6 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-8)){
Line8.text = "-----------------------";
}
else {
Line8.text = Player.Inventory[7+CurrentScroll].amount + " units of " + Player.Inventory[7+CurrentScroll].name;
ResSp8.sprite = IDB.itemDB[7 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-9)){
Line9.text = "-----------------------";
}
else {
Line9.text = Player.Inventory[8+CurrentScroll].amount + " units of " + Player.Inventory[8+CurrentScroll].name;
ResSp9.sprite = IDB.itemDB[8 + CurrentScroll].itemsprite;
}
if (CurrentScroll > (Player.Inventory.Count-10)){
Line10.text = "-----------------------";
}
else {
Line10.text = Player.Inventory[9 + CurrentScroll].amount + " units of " + Player.Inventory[9 + CurrentScroll].name;
ResSp10.sprite = IDB.itemDB[9 + CurrentScroll].itemsprite;
}
Comment
Best Answer
Answer by Statement · Oct 10, 2015 at 11:54 PM
// Remove variables Line1 .. Line10 and ResSp1 .. ResSp10
// Use lists instead.
public List<Text> Lines = new List<Text>();
public List<Image> ResSp = new List<Image>();
public void UpdateMenu()
{
for (int i = 0; i < 10; ++i)
UpdateMenuEntry(i);
}
private void UpdateMenuEntry(int i)
{
var line = Lines[i];
var resSp = ResSp[i];
var itemId = i + CurrentScroll;
var item = Player.Inventory[itemId];
if (CurrentScroll > (Player.Inventory.Count - (i + 1)))
{
line.text = "-----------------------";
}
else
{
line.text = item.amount + " units of " item.name;
resSp.sprite = IDB.itemDB[itemId].itemsprite;
}
}
And if you want, you could group up the UI objects...
// If a Text and an Image belong together, make a class for them
[System.Serializable]
public class InventoryItemUI
{
public Text line;
public Image res;
public string Text { get { return line.text; } set { line.text = value; } }
public Sprite Sprite { get { return res.sprite; } set { res.sprite = value; } }
public void Clear()
{
Text = "-----------------------";
// Sprite = blank?
}
public void Refresh(int itemId)
{
// I dont know if Player is a variable or a class.
// I dont know if IDB is a variable or a class.
var item = Player.Inventory[itemId];
Text = item.amount + " units of " item.name;
Sprite = IDB.itemDB[itemId].itemsprite;
}
}
public List<InventoryItemUI> inventoryItemUI = new List<InventoryItemUI>();
public void UpdateMenu()
{
for (int i = 0; i < inventoryItemUI.Count; ++i)
{
var item = inventoryItemUI[i];
if (CurrentScroll > (Player.Inventory.Count - (i + 1)))
item.Clear();
else
item.Refresh(i + CurrentScroll);}
}
}
Wow thanks this seem to be what iam looking for.
I have one question though. In this situation i know how many text objects there will be beforehand. Isnt it better in that case to use a normal array ins$$anonymous$$d of a list?