Page 2 of 3
Re: Create items via code.
Posted: Fri Sep 18, 2020 3:05 pm
by megadok
And if I want to modify the value of a field of a specific item?
Example:
Code: Select all
mydatabase.item("Axe_of_fire", "Name", "Axe_of_Ice")
Re: Create items via code.
Posted: Fri Sep 18, 2020 3:20 pm
by Tony Li
Get the Field and set its value:
Code: Select all
var myItem = database.GetItem("Axe of fire");
var field = myItem.AssignedField("Name");
if (field != null) field.value = "Axe of Ice";
Re: Create items via code.
Posted: Fri Sep 18, 2020 3:28 pm
by megadok
Perfectly Tony, Thank you so much. I can continue working now.
Re: Create items via code.
Posted: Fri Sep 18, 2020 3:49 pm
by Tony Li
Glad to help!
Re: Create items via code.
Posted: Thu Oct 15, 2020 3:53 pm
by megadok
Hey Tony!
How could I set the value of a item field which is blank?
AssignedField() only works if the field is not empty.
Code: Select all
var field = myItem.AssignedField("name field");
if (field != null) { field.value = "a value"; }
Thank you!
Re: Create items via code.
Posted: Thu Oct 15, 2020 4:33 pm
by Tony Li
Hi,
The unhelpful answer is "don't give fields blank names."
However, if you have a field with a blank name, you won't be able to look it up by name. Instead, you'll need to iterate through the fields and find the field with the blank name:
Code: Select all
foreach (var field in myItem.fields)
{
if (string.IsNullOrEmpty(field.name))
{
field.name = "a name"; //<-- Probably a good idea to give it a name.
field.value = "a value";
break;
}
}
Re: Create items via code.
Posted: Thu Oct 15, 2020 4:49 pm
by megadok
Works perfectly with "for" not wich "foreach".
DialogueDatabase db = Resources.Load<DialogueDatabase>("Dialogo_data/item_database");
var myItem = db.GetItem("Axe of Ice");
var _fields= myItem.fields;
for(int i = 0; i < _fields.Count; i++)
{
if(_fields.title == "Descripcion")
{
_fields.value = "Is a powerfull weapon.";
}
}
Thank you Tony!
Re: Create items via code.
Posted: Thu Oct 15, 2020 5:17 pm
by Tony Li
Glad to help!
Re: Create items via code.
Posted: Fri Oct 16, 2020 7:57 am
by megadok
Hey again!
How can I reference my "custom template" to my code to create an object?, since I don't use the default template.
Code: Select all
DialogueDatabase db = Resources.Load<DialogueDatabase>("Dialogo_data/item_database");
var template = Template.mycustomtemplate(); <<------ i wan to use my custom item template
var item = template.CreateItem(template.GetNextItemID(db), "name");
db.items.Add(item);
EditorUtility.SetDirty(db);
Re: Create items via code.
Posted: Fri Oct 16, 2020 10:00 am
by Tony Li
Hi,
The database's template is stored in JSON format in the database. You can get it like this:
Code: Select all
var template = JsonUtility.FromJson<Template>(db.templateJson);