SharedString Not Accepting Enum

willij

New member
I'm trying to put an enum name into a SharedString variable that is then passed to a behavior tree reference as a variable but it's not working.

In the same script, the SharedString could also be set to a regular string variable - when that happens, everything works fine but as soon as I try to set the SharedString to a [enum].ToString() it doesn't work. In the behavior tree, the SharedString remains empty / (null).

Edit: Just for more clarification, after I set the [SharedString].Value to the [enum].ToString() I did a debug.Log and printed out the value of the shared string to make sure it wasn't null and I got a value back it's just that it does not get transferred properly to the external tree. Like I said, the exact same code works when I put a regular string into the SharedString value instead of using enum ToString.

Edit: Ah, I put this in the wrong forum. This is for Behavior Designer
 
Last edited:
So other string values work? Such as "abc"? From the shared string perspective there isn't anything different from enum.ToString and a constant string.
 
After replacing that line with a string it has the same problem so it's not that it's a string. I realized the only time it was being replaced with an actual string was when it was in the first sequence. It seems like every sequence after the first doesn't work.

So basically I have selector choosing from multiple sequences all with behavior tree references that all have an action that writes a value to a shared string and all of them use the same name for the shared string variable. It seems like the only one that is successfully writing to the shared string is the first sequence in the list, all the others return null.

Idk, I've been doing a debug.log right after I set the sharedString to check and enum or string it prints out the correct value so I don't know why it's not getting passed to the external tree.
 
Last edited:
Changing it to a Dynamic SharedString each with it's own unique name seemed to work for one gameobject though I still have another object that uses the same system (with a few modifications) that's having problems. I'll have to comb through to see if there's any SharedStrings with the same name.

Edit: Well, I'm in a bit of a snatch now because this second object uses all of the same external trees as the first object but it has two sets of the same trees. On the first object, I could get away with giving each of the dynamic sharedStrings passed to their trees in the sequences their own unique names because I was certain there wouldn't be any duplicates but now I'm dealing with duplicates and I don't know what to do about it. :(

I had tried dynamic sharedstring before I posted this thread but it had the same problems as using a local variable so I guess the problem is definitely that they're sharing the same name since now I'm using different names for each sequence it's working but like I said, I now need two copies of the same sequence of sequences in the form of an external tree elsewhere so I cannot change the names of the dynamic variables between these copies. Using a local variable would have the same problem.

If I use a global variable in the main tree, I won't be able to reference it in the external trees and if I use a global variable in the external trees, isn't that just like the same as a local variable? I'm not sure if it will run into the same naming problems.
 
Last edited:
So you have a base tree with many Behavior Tree Reference tasks, and those External Trees are supposed to use the same SharedString. However, only one tree is using the correct reference?

If you output sharedString.GetHashCode() within your external trees it will output the instance of that shared string. They should all match. If they don't match then something is going wrong. Make sure on the Behavior Tree Reference variables you have the same type & name for that shared string.

Here's a sample scene of what I think you're trying to do. Notice how when I set the string value in one tree that value is propagated to the other trees. As an example the first external tree is setting the value to ExternalTree1 and then when that second external tree runs it outputs the value set by the first external tree, ExternalTree1. There are also some cases where it is set from the main tree.

 
I edited it so that it's a simplified version of what I've got.
I put a repeater in there to run the tree 5 times but please run play mode a few times because the results change every time.

I've seen that: Sometimes it runs fine. Sometimes just a few strings are off. Sometimes they're all just the first string. Sometimes they're all null.

I put a Debug.Log to print out the value of the SharedString right after I set it. This is the value I expect to be passed to the log but it doesn't always match up with what actually gets printed out. I put "STRING THAT'S SUPPOSED TO BE PRINTED:" before these expected values.
 

Attachments

  • SetString2.unitypackage
    8.8 KB · Views: 1
Thanks - I see it now. I will step through to see what is happening but have you tried using a SharedStringList instead of a List<SharedString>?

Code:
    [System.Serializable]
    public class SharedStringList : SharedVariable<List<string>>
    {
        public static implicit operator SharedStringList (List<string> value) { return new SharedStringList { mValue = value }; }
    }

It looks like the incorrect references are being set and by not having the variables within a list it should allow you to just need a single SharedVariable and make things easier to follow. You can then do something like:

Code:
mySharedVariableList.Value[0] = stringValue;
 
Ok, I have done it. I wanted to use SharedStrings because these are animation names so I can put them directly into the built in animator actions. I needed to make new animation actions that accept SharedStringList with an index and I have to be careful about putting in the correct index number from now on but it can't be helped. Anyway it's working now thank you. :) (y)
 
Top