Ambiguity and Null references. Oh my!

First of all, i followed your conversion more or less to the letter. However, i have to fully qualify the Array and Dictonary references to avoid Ambiguity between Godot and .NET.

private void SetRandomStory()
{
    var parseResult = GetJSONParseResult("stories.json");
    var stories = parseResult.Result as System.Array;

    var rnd = new Random();
    var storyIndex = rnd.Next(0, stories.Length);
    var randomStory = stories.GetValue(storyIndex) as System.Collections.Generic.Dictionary<System.Object, System.Object>;

    // TODO: currentStory.prompts = GetPrompts(randomStory)
    currentStory.story = randomStory["story"] as string;
    GD.Print(currentStory.story);
}

Also, up to “var parseResult = GetJSONParseResult(“stories.json”);” everything works fine. I get the JSON data and it prints just fine if i use “GD.Print(parseResult.Result)”. However, the following line “var stories = parseResult.Result as System.Array;” results in stories being NULL, which will result in a NullReference crash further down.

My stories.json seems fine…

[{
    "prompt":
    [
        "an animal",
        "an activity",
        "another activity",
        "a sound",
        "a feeling",
        "a thing",
        "a number"
    ],
    "story":
        "The %s was out on a %s for something to %s when suddenly a mighty %s was heard. He was so %s that he jumped up in a %s and was stuck there for %s days."
},
{
    "prompt":
    [
        "a name",
        "another name",
        "a profession",
        "a structure",
        "an animal",
        "an animal sound"
    ],
    "story":
       "%s and %s was playing %s in the %s. The %s ran in on them and %s."
}]

I have poured over the code for over 2 hours now, and i can’t find anything wrong with it. The “GetJSONParseResult()” method is more or less a verbatim version of yours.

Download: LoonyLips C-Sharp.zip

I stumbled also with this thing and thanks to Keith in Udemy QA in current lecture the better way was to use Godot’s own Array. The 3.0.6 update may have added Godot it’s own Array function. It’s also a little easier to read than C#'s own Arrays.

private void SetRandomStory()
{
    var parseResult = GetJSONParseResult("stories.json");
    var stories = parseResult.Result as Godot.Array;
        
    Random rnd = new Random();
    var storyIndex = rnd.Next(0, stories.Count - 1);
    var randomStory = stories[storyIndex] as Godot.Dictionary;
 
    currentStory.story = randomStory["story"] as String;
    GD.Print(currentStory.story);
}
1 Like

Thanks for your reply.

I tried it this way, and yes it does work. However, the reason i choose C# is that i am a C# developer at the core, and want to use something i am comfortable in. Using Godot’s Array and Dictionary feels a bit of a hack. I would prefer if i could use C# and .NET/Mono as much as possible.

So i guess i have to find some other way to get the JSON data into a System.Array object.

Anhyhow, thanks for pointing me towards a temporarly acceptable solution :slight_smile:

Privacy & Terms