Create items via code.

Announcements, support questions, and discussion for the Dialogue System.
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post 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")
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post 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";
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post by megadok »

Perfectly Tony, Thank you so much. I can continue working now. :D
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post by Tony Li »

Glad to help!
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post 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!
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post 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;
    }
}
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post 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! :D
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post by Tony Li »

Glad to help! :-)
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post 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);
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post 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);
Post Reply