Adventure Game Studio

AGS Development => Engine Development => Topic started by: Monsieur OUXX on Thu 17/09/2020 19:11:02

Title: Dictionary "Get" when key not present
Post by: Monsieur OUXX on Thu 17/09/2020 19:11:02
AGS 3.5.0 24 (I think that's Patch 4 but I'm not 100% sure)

Try this in a custom script:
Code (ags) Select

void game_start()
{
  Dictionary* d = Dictionary.Create(eSorted);
  String v = d.Get("badKey");
}



The manual says that d.Get("badKey") should return null.
Instead it generates an engine error.
Title: Re: Dictionary "Get" when key not present
Post by: Crimson Wizard on Thu 17/09/2020 20:25:53
I pushed a fix for this, and few other things. This fix will be part of a next patch, also could be downloaded here when it's ready (may need to wait an hour or so): https://cirrus-ci.com/task/6244779275059200
EDIT fixed link

Meanwhile, the workaround is probably this:
Code (ags) Select

String SafeGet(this Dictionary*)
{
    if (this.Contains("badKey"))
        return this.Get("badKey");
    return null;
}
Title: Re: Dictionary "Get" when key not present
Post by: Monsieur OUXX on Mon 21/09/2020 07:52:59
Quote from: Crimson Wizard on Thu 17/09/2020 20:25:53
I pushed a fix for this, and few other things. This fix will be part of a next patch, also could be downloaded here when it's ready (may need to wait an hour or so): https://cirrus-ci.com/task/6244779275059200
EDIT fixed link

Meanwhile, the workaround is probably this:
Code (ags) Select

String SafeGet(this Dictionary*)
{
    if (this.Contains("badKey"))
        return this.Get("badKey");
    return null;
}



Look, this is the function  I had written ;-D ;-D ;-D There are some similarities lol
(also I don't know what I was thinking with that "else", off it goes!)
Code (ags) Select

String SafeGet(Dictionary* d,  String key)
{
  if (!d.Contains(key)) {
    return null;
  } else {
    return d.Get(key);
  }
}
Title: Re: Dictionary "Get" when key not present
Post by: Monsieur OUXX on Sun 01/11/2020 21:15:49
FYI, I don't know if it was fixed in the same patch, but "Set" also fails when the value is null (cannot insert null in Dictionary) and it's not written in the help of the function.
Title: Re: Dictionary "Get" when key not present
Post by: Crimson Wizard on Sun 01/11/2020 21:16:58
Quote from: Monsieur OUXX on Sun 01/11/2020 21:15:49
FYI, I don't know if it was fixed in the same patch, but "Set" also fails when the value is null (cannot insert null in Dictionary) and it's not written in the help of the function.

Null is key or value? Key cannot be null I think, but value could be allowed probably...


EDIT: Actually it does not allow null, because Set arguments are declared as "const string", and it fails at converting null String pointer to "const string".... You may be able to pass an empty string though ("").

So basically, Dictionaries and Sets must contain valid allocated strings.