Create items via code.

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

Create items via code.

Post by megadok »

Hello again. I am trying to create items via script.

Code: Select all

        PixelCrushers.DialogueSystem.Item nuevo_item = new PixelCrushers.DialogueSystem.Item(); 
                   nuevo_item.IsItem = true;
                   nuevo_item.Name = "Axe of fire";

                   db.items.Add(nuevo_item);
but dont work...

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
PixelCrushers.DialogueSystem.Field.SetValue (System.Collections.Generic.List`1[T] fields, System.String title, System.String value, PixelCrushers.DialogueSystem.FieldType type) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Data/Field.cs:351)
PixelCrushers.DialogueSystem.Field.SetValue (System.Collections.Generic.List`1[T] fields, System.String title, System.Boolean value) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Data/Field.cs:420)
PixelCrushers.DialogueSystem.Item.set_IsItem (System.Boolean value) (at Assets/Plugins/Pixel Crushers/Dialogue System/Scripts/MVC/Model/Data/Item.cs:27)
controlador_menus.Start () (at Assets/controlador_menus.cs:59)
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post by Tony Li »

Hi,

At runtime, treat the dialogue database as read-only.

At runtime, use the Lua environment for read-write access. You must use Lua.Run to create the item. Example:

Code: Select all

Lua.Run("Item['Axe_of_fire'] = { Name = 'Axe of fire', IsItem = true }");
But you can use DialogueLua.GetItemField and DialogueLua.SetItemField to get and set its field values. Example:

Code: Select all

DialogueLua.SetItemField("Axe of fire", "Damage", 50);
...
int actualDamage = DialogueLua.GetItemField("Axe of fire", "Damage").asInt + Random.Range(1, 10);
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post by megadok »

I test

Code: Select all

Lua.Run("Item['Axe_of_fire'] = { Name = 'Axe of fire', IsItem = true }");
but nothing happend. :o my database is empty.

I try to use this in the "editor" for an "item creator" that I have made.
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post by Tony Li »

Got it. I thought you were creating it at runtime. Use Lua at runtime. It will add the item to the Lua environment, not the database.

If you're writing an editor script and you want to add something to the database, you can add it to the database:

Code: Select all

var template = TemplateTools.LoadFromPlayerPrefs();
var item = template.CreateItem(template.GetNextItemID(database), "Axe of fire");
database.items.Add(item);
item.fields.Add(new Field("Damage", "50", FieldType.Number));
EditorUtility.SetDirty(database);
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post by megadok »

https://ibb.co/5MHB3Vh <-----

I use EDITORWINDOW for my items creator.

I can't access to templatetools...
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post by Tony Li »

It's in the PixelCrushers.DialogueSystem namespace.

API reference: PixelCrushers.DialogueSystem.TemplateTools

Alternatively, you can just use the default template. It won't have any customizations you may have added to the template. Example:

Code: Select all

var template = Template.FromDefault();
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post by megadok »

I already saw the api before, as you can see in this image https://ibb.co/5MHB3Vh , I access it through PixelCrushers.DialogueSystem namespace, but templatetools does not appear.

Does this work with EditorWindow? I need to create my item through EditoWindow.
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post by Tony Li »

Yes. The Dialogue Editor is an EditorWindow, and it uses TemplateTools.FromPlayerPrefs().

If your editor script in an Editor folder?

Try adding this to the using statements at the top of your script:

Code: Select all

using PixelCrushers.DialogueSystem;
megadok
Posts: 54
Joined: Sat Sep 05, 2020 12:04 pm

Re: Create items via code.

Post by megadok »

I used "using PixelCrushers.DialogueSystem", but i didn't see "templatetools" either, it's very strange. :(

Code: Select all

            var template = Template.FromDefault();
            var item = template.CreateItem(template.GetNextItemID(db), "Axe of fire");
            db.items.Add(item);
            item.fields.Add("Damage", 50);
            EditorUtility.SetDirty(db);
I have tested the code and it works perfectly with "Template.FromDefault();"
The object is created and saved in the database.

The next problem is

Code: Select all

 [b] item.fields.Add("Damage", 50)[/b];
,mark error. https://ibb.co/LPXDLbN <-----
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Create items via code.

Post by Tony Li »

Sorry, I typed that line all wrong. Please use this:

Code: Select all

item.fields.Add(new Field("Damage", "50", FieldType.Number));
(I just fixed it in my original post, too.)
Post Reply