Hello everyone, I’m trying to create a database made in javascript.
How could I create something that when the int ID of something was called it would return for example
Name, value, weight, desc
I’ve made a quest system and am trying to work on my inventory to allow rewards etc
You would have to make a system that would hold all your information for each id. You would have to have some lists to hold all the information, then you could just reference the index to pull the information from the lists. Something like this,
List<string> names;
List<int> values;
List<int> weights;
List<string> descriptions;
void Inventory(int index)
{
Debug.Log("Name: " + names[index]);
Debug.Log("Value: " + names[index]);
Debug.Log("Weight: " + names[index]);
Debug.Log("Description: " + names[index]);
}
void InsertItem(string name, int value, int weight, string description)
{
names.Add(name);
values.Add(value);
weights.Add(weight);
descriptions.Add(description);
}
You’d be better off creating a custom class or struct (whatever is appropriate) and using a list of them…
public class ExampleClass {
// Lazy and not going to encapsulate, but I would in real use...
public string name;
public int weight;
public string description;
public ExampleClass(string nname, int nweight, string ndescription){
name = nname;
weight = nweight;
description = ndescription;
}
}
public class Inventory {
List<ExampleClass> _inventory = new List<ExampleClass>();
}
Significantly cleaner / easier to encapsulate and control / easier to maintain than a bunch of linked lists.