Page 1 of 3
Create items via code.
Posted: Fri Sep 18, 2020 8:36 am
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)
Re: Create items via code.
Posted: Fri Sep 18, 2020 9:56 am
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);
Re: Create items via code.
Posted: Fri Sep 18, 2020 1:11 pm
by megadok
I test
Code: Select all
Lua.Run("Item['Axe_of_fire'] = { Name = 'Axe of fire', IsItem = true }");
but
nothing happend.
my database is empty.
I try to use this in the "editor" for an "item creator" that I have made.
Re: Create items via code.
Posted: Fri Sep 18, 2020 1:18 pm
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);
Re: Create items via code.
Posted: Fri Sep 18, 2020 1:44 pm
by megadok
https://ibb.co/5MHB3Vh <-----
I use EDITORWINDOW for my items creator.
I can't access to templatetools...
Re: Create items via code.
Posted: Fri Sep 18, 2020 2:02 pm
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();
Re: Create items via code.
Posted: Fri Sep 18, 2020 2:11 pm
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.
Re: Create items via code.
Posted: Fri Sep 18, 2020 2:26 pm
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;
Re: Create items via code.
Posted: Fri Sep 18, 2020 2:37 pm
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 <-----
Re: Create items via code.
Posted: Fri Sep 18, 2020 2:52 pm
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.)