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