1
\$\begingroup\$

I have a NormalizedWidth AoT Dictionary with a Key of type String (basically, the alphabet)... and a Value of type Float. You can see it on the left side of this screenshot:

enter image description here

Here's what I'm doing: I have a string with five characters in a word. I'm splitting that string into a char array, and then looping through that array with a For Each Loop.

enter image description here

What I'd like to do is look up with the width of each of those characters, which is stored in a NormalizedWidth AoT Dictionary. Or at least I am trying to! I am currently using a Get Dictionary Item node to try and get values out of the. But as each character is sent into the node, the output is given as Null. (See picture above).

I must be making a mistake. The data appears to be there, but I am failing to access it. Should I be using a different node to get data out of the Dictionary... or is there another mistake I'm making? Thank you!

\$\endgroup\$
4
  • \$\begingroup\$ regardless that it does not work, why do you want to save the Data from a dictionary to another Array? If you need the value for computation of the width of a word later, you can ask directly your dictionary letter by letter \$\endgroup\$
    – Zibelas
    Jun 15 at 10:00
  • \$\begingroup\$ Either way, I will have to ask my dictionary letter by letter... just having a bit of trouble doing that here! \$\endgroup\$
    – kanamekun
    Jun 15 at 10:13
  • \$\begingroup\$ You don't need a dictionary for this, since letters form a dense, contiguous sequence of indices (say, using their ASCII codes). Unless you're trying to work with far-flung Unicode characters beyond the basic ASCII set you've shown here. \$\endgroup\$
    – DMGregory
    Jun 15 at 10:34
  • \$\begingroup\$ I have 2 data sets: first a dictionary of every glyph in my font (the key) and its width (the valie)... and then an array of letters in a particular word. Is a dictionary a good way to store the first data set? And how do I get data out of a dictionary? \$\endgroup\$
    – kanamekun
    Jun 15 at 10:39

1 Answer 1

2
\$\begingroup\$

The method ToCharArray will generate a char[], in which each element is of type char instead of string, which is confusing because some nodes on the bolt interface are generic, which prevents the display of the types. You can convert the key to string type through the ToString method:

enter image description here

You can see the difference in their colors on the runtime bolt interface(which is very obscure):

enter image description here

Edit:

What if I don't have an Object.ToString node? Visual Scripting allows you to define your own assemblies. You can decide which nodes are needed. This can save memory/search time/time of recompiling nodes.etc. For example, maybe your 2D project doesn't need the unity VR/AR modules, you can cancel them manually.

Maybe you deleted some assemblies manually causing the System assembly to be missing. And Object is in it.

Go to Edit > Project Settings > Visual Scripting > Node Library:

enter image description here

You can simply reset the configuration because the default configuration includes System (referenced by other assemblies). You can also manually click + to add System.

And dont forget to click Regenerate Nodes button.

If it doesn't work still, try to reset the configuration of Type Options, and regenerate it again.

Finally, search for object.tostring in the script interface:

enter image description here

\$\endgroup\$
9
  • \$\begingroup\$ Amazing, this is exactly what I was hoping for! I am using Unity Visual Scripting though, and the Object to String node doesn't work to convert a Char to a String. Any ideas on what node will convert a Char to a String in UVS? \$\endgroup\$
    – kanamekun
    Jun 15 at 12:19
  • \$\begingroup\$ This seems impossible, char inherits from ValueType, ultimately inherits from Object. This node works fine for me. \$\endgroup\$
    – Mangata
    Jun 15 at 12:28
  • \$\begingroup\$ I don't have an Object to String node... but do have a GameObject to String and Unity Object to String. When I try the latter, I get this: InvalidConversionException: Cannot convert from 'System.Char' to 'UnityEngine.Object'. When I try the former, I get this: InvalidConversionException: Cannot convert from 'System.Char' to 'UnityEngine.GameObject'. \$\endgroup\$
    – kanamekun
    Jun 15 at 12:39
  • \$\begingroup\$ @kanamekun Added a possible workaround. \$\endgroup\$
    – Mangata
    Jun 15 at 13:22
  • \$\begingroup\$ Thank you so much! I added System to the Visual Scripting > Node Library and Regenerated Nodes... but I am still not seeing the Object to String node appear when I do a search for "Object.ToString". I do see the Game Object: To String and the Unity Object: To String and the Scriptable Object: To String! \$\endgroup\$
    – kanamekun
    Jun 15 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .