FlowCanvas Forums › Support › Nodes for Dictionaries
Hey,
I am trying to get some nodes in FC for Dictionaries. Some time ago, I asked you a favor to get couple of Tasks in NC for Dictinaries, and you really helped me 🙂
Now I am trying to create the same 2 nodes, “Add entries” and “Get entries by providing a key”. But I am stuck with this, I am trying but getting syntax errors,
can you please help me creating these 2 nodes? Based on those I will try to create more, like iterate, or sort, etc.
—–I am using a short code example to create the variable in the Balckboard… as you provided.(This works perfect!!)
using UnityEngine;
using System.Collections.Generic;
using NodeCanvas.Framework;
public class myDictionariesExample : MonoBehaviour {
[ContextMenu(“AddDictionaryVariable”)]
void AddDictionaryVariable(){
var bb = GetComponent<Blackboard>();
bb.AddVariable(“Enemies”, typeof(Dictionary<string, GameObject>));
}
}
—–And this is the code that I am trying to create the “Add entries” node, but no working 🙁
//Dictionaries
[Category(“Functions/Dictionaries”)]
public class AddDictionaryItem : CallableFunctionNode<Dictionary<string, T>{
public override Invoke(IDictionary<string, T> dictionary, string key, object item){
dictionary.Add(key, item);
return dictionary;
}
}
Thanks in advance!!
Hey,
Here you go 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System.Collections; using System.Collections.Generic; using ParadoxNotion.Design; namespace FlowCanvas.Nodes{ [Category("Functions/Dictionaries")] public class AddDictionaryItem<T> : CallableFunctionNode<IDictionary<string, T>, IDictionary<string, T>, string, T>{ public override IDictionary<string, T> Invoke(IDictionary<string, T> dict, string key, T item){ dict.Add(key, item); return dict; } } [Category("Functions/Dictionaries")] public class RemoveDictionaryKey<T> : CallableFunctionNode<IDictionary<string, T>, IDictionary<string, T>, string>{ public override IDictionary<string, T> Invoke(IDictionary<string, T> dict, string key){ dict.Remove(key); return dict; } } [Category("Functions/Dictionaries")] public class ClearDictionary : CallableFunctionNode<IDictionary, IDictionary>{ public override IDictionary Invoke(IDictionary dict){ dict.Clear(); return dict; } } } |
Please note that these work with IDictionary. If you want, you can change these to work with Dictionary instead (except the ClearDictionary, which is best to work with IDictionary to avoid yet another generic node).
Let me know if these work for you.
Cheers!
Join us on Discord: https://discord.gg/97q2Rjh
That is Great!!
this will give me a good start with Dictionaries
thanks so much for your help!!! 🙂
Hey Gavalakis,
I started using these new nodes and they worked well, except the “Remove” one, which I was expecting to get a boolean value as a return value, however I am receiving the Dictionary itself
Also I tried to create myself the “ContainsKey” node but I can not make it to work(In this one I am also expecting to have the return value as Boolean)
Could you please help me? 🙂
Thanks in advance!!
see my screens below
Hi there,
I am also trying to create a node for “Get Value” from Dictionary and also got stuck there
Can you please also help me with such node?
Thanks in advance!
Hey,
Here are the nodes you asked for.
Both ContainsKey and GetElement can be substituted with TryGetValue though, which does both in one go. 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[Category("Functions/Dictionaries")] public class TryGetValue<T> : CallableFunctionNode<T, IDictionary<string, T>, string>{ public bool exists{get; private set;} public override T Invoke(IDictionary<string, T> dict, string key){ T value = default(T); exists = dict.TryGetValue(key, out value); return value; } } [Category("Functions/Dictionaries")] public class DictionaryContainsKey<T> : CallableFunctionNode<bool, IDictionary<string, T>, string>{ public override bool Invoke(IDictionary<string, T> dict, string key){ return dict.ContainsKey(key); } } [Category("Functions/Dictionaries")] public class GetDictionaryElement<T> : CallableFunctionNode<T, IDictionary<string, T>, string>{ public override T Invoke(IDictionary<string, T> dict, string key){ return dict[key]; } } |
Cheers!
Join us on Discord: https://discord.gg/97q2Rjh
Thank you!!!
You are very welcome! 🙂
Join us on Discord: https://discord.gg/97q2Rjh