FlowCanvas Forums › Support › Noobie question :) Collection Implementations
Tagged: Dictionary
Hello everyone! 🙂 And especially the awesome dev.
So I’ve created my macros, scripts, played with some functions, even figured out how to do a UnityWebRequest coroutine to connect to my PHP webpages. But I’ve got stuck on a few things here and there.
Right now the most important one is this;
How do I go about populating a dictionary with a for loop? I just seem to be missing some heavily simple gimmick somewhere… I logged the values I get from (Get List Item) nodes and they are correct but Add Dictionary Item’s Value output always returns null. Both of them. Keys are added but values are null as well.
In short, I guess I need some minor tutorial about manipulating collection variables 🙂
thanks in advance
Ok solved my own problem by creating a custom node 🙂
node code included below;
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
using System.Collections.Generic; using System.Linq; using UnityEngine; using ParadoxNotion.Design; namespace FlowCanvas.Nodes { [Description("Create a Dictionary of <string, > from lists as keys and values")] [ContextDefinedInputs(typeof(string), typeof(Wild))] public class GenerateDictionary<T> : VariableNode { [SerializeField] [ExposeField] [GatherPortsCallback] [DelayedField] private bool EncryptStringValues = false; [SerializeField] [ExposeField] [GatherPortsCallback] [DelayedField] private bool EncryptKeys = false; public override void SetVariable(object o) { //... } protected override void RegisterPorts() { var keys = new ValueInput<List<string>>(); var values = new ValueInput<List<T>>(); keys = AddValueInput<List<string>>("KeysList"); values = AddValueInput<List<T>>("valuesList"); AddValueOutput<IDictionary<string, T>>("Dictionary", () => { var d = new Dictionary<string, T>(); for (int i = 0; i < keys.value.Count; i++) { if (values.value<em class="d4pbbc-italic"></em>.GetType() == typeof(string)) { var d2 = new Dictionary<string, object>(); var valueToAdd = EncryptStringValues ? Encrypt1.EncryptRJ256(values.value<em class="d4pbbc-italic"></em>.ToString()) : values.value<em class="d4pbbc-italic"></em>.ToString(); string keyToAdd = EncryptKeys ? Encrypt1.EncryptRJ256(keys.value<em class="d4pbbc-italic"></em>) : keys.value<em class="d4pbbc-italic"></em>; d2.Add(keyToAdd, valueToAdd); d = d2.ToDictionary(p => p.Key, p => (T)p.Value); } else { d = new Dictionary<string, T>(); var valueToAdd = values.value<em class="d4pbbc-italic"></em>; string keyToAdd = EncryptKeys ? Encrypt1.EncryptRJ256(keys.value<em class="d4pbbc-italic"></em>) : keys.value<em class="d4pbbc-italic"></em>; d.Add(keyToAdd, valueToAdd); } } return d; }); } } } |
Encryption code (required for my own secure connection, feel free to not-use it 🙂
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Collections; using UnityEngine; public class Encrypt1 : MonoBehaviour { public static string DecryptRJ256(string prm_text_to_decrypt) { var prm_key = "USE YOUR OWN KEY"; //32 chr shared ascii string (32 * 8 = 256 bit) var prm_iv = "USE YOUR OWN KEY II"; //32 chr shared ascii string (32 * 8 = 256 bit) var sEncryptedString = prm_text_to_decrypt; var myRijndael = new RijndaelManaged() { Padding = PaddingMode.Zeros, Mode = CipherMode.CBC, KeySize = 256, BlockSize = 256 }; var key = Encoding.UTF8.GetBytes(prm_key); var IV = Encoding.UTF8.GetBytes(prm_iv); var decryptor = myRijndael.CreateDecryptor(key, IV); var sEncrypted = Convert.FromBase64String(sEncryptedString); var fromEncrypt = new byte[sEncrypted.Length]; var msDecrypt = new MemoryStream(sEncrypted); var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); return (Encoding.UTF8.GetString(fromEncrypt)); } public static string EncryptRJ256(string prm_text_to_encrypt) { var prm_key = "USE YOUR OWN KEY"; //32 chr shared ascii string (32 * 8 = 256 bit) var prm_iv = "USE YOUR OWN KEY II"; //32 chr shared ascii string (32 * 8 = 256 bit) var sToEncrypt = prm_text_to_encrypt; var myRijndael = new RijndaelManaged() { Padding = PaddingMode.Zeros, Mode = CipherMode.CBC, KeySize = 256, BlockSize = 256 }; var key = Encoding.UTF8.GetBytes(prm_key); var IV = Encoding.UTF8.GetBytes(prm_iv); var encryptor = myRijndael.CreateEncryptor(key, IV); var msEncrypt = new MemoryStream(); var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write); var toEncrypt = Encoding.UTF8.GetBytes(sToEncrypt); csEncrypt.Write(toEncrypt, 0, toEncrypt.Length); csEncrypt.FlushFinalBlock(); var encrypted = msEncrypt.ToArray(); return (Convert.ToBase64String(encrypted)); } } |
Hello and welcome.
Thank you as well! 🙂
After further digging into this, I realized that there is indeed a bug here, that results in the AddDictionaryItem node (as well as other similar nodes) to return a NULL. I am investigating this bug right now and hope to fix it as soon as possible and send it on an asset store update.
If the fix is relevantly easy, I will also let you know here of the required changes to make in the source as well, so that you don’t have to wait for the new version to go live!
Thank you!
Join us on Discord: https://discord.gg/97q2Rjh