Dictionary "Get" when key not present

Started by Monsieur OUXX, Thu 17/09/2020 19:11:02

Previous topic - Next topic

Monsieur OUXX

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

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.
 

Crimson Wizard

#1
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

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

Monsieur OUXX

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

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

String SafeGet(Dictionary* d,  String key)
{
  if (!d.Contains(key)) {
    return null;
  } else {
    return d.Get(key);
  }
}
 

Monsieur OUXX

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.
 

Crimson Wizard

#4
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.

SMF spam blocked by CleanTalk