c# - Unity game on iOS ArgumentOutOfRangeException in dictionary -
i have achievement module saving , loading dictionary name of achievement (key) , if it's been accomplished (bool value). i'm using gde (game data editor) plugin (this one).
my module works fine in unity editor, saving , loading everything. in ios crashes argument out of range exception. save , load methods:
public void load(){ gdeachievementsdata achievements; if (!gdedatamanager.datadictionary.trygetcustom(gdeitemkeys.achievements_achievements, out achievements)){ debug.logerror("error reading save data!"); } loadachievementsindictionary(achievements); debug.log("achievement loaded data"); } public void save(){ gdeachievementsdata achievements; if (!gdedatamanager.datadictionary.trygetcustom(gdeitemkeys.achievements_achievements, out achievements)){ debug.logerror("error reading save data!"); } savedictionaryinachievements(achievementscript.achievements, achievements); achievements.set_achievements(); debug.log("achievements saved data"); } void loadachievementsindictionary(gdeachievementsdata gdedata){ debug.log("achievements: "+gdedata.achievements); if(achievements==null){ achievements = new achievementlist(); } foreach(list<string> achievement in gdedata.achievements){ if(achievements.list.containskey(achievement[0])) { achievements.list[achievement[0]] = (achievement[1]=="true")||(achievement[1]=="true"); }else{ achievements.list.add(achievement[0], achievement[1]=="true"||achievement[1]=="true"); } } } void savedictionaryinachievements(achievementlist list, gdeachievementsdata gdedata){ for(int = 0; < gdedata.achievements.count; i++){ list<string> item = gdedata.achievements[i]; string achievementname = item[0]; item[1] = list.list[achievementname].tostring(); } } and errors generated in xcode debugger, saving:
argumentoutofrangeexception: argument out of range. parameter name: index @ system.collections.generic.list`1[system.string].get_item (int32 index) [0x00000] in :0 @ achievementscript.savedictionaryinachievements (.achievementlist list, gamedataeditor.gdeachievementsdata gdedata) [0x00000] in :0 @ achievementscript.save () [0x00000] in :0 @ achievementscript.onapplicationfocus (boolean focusstatus) [0x00000] in :0 @ achievementscript.onapplicationpause (boolean pausestate) [0x00000] in :0
and loading:
argumentoutofrangeexception: argument out of range. parameter name: index @ system.collections.generic.list`1[system.string].get_item (int32 index) [0x00000] in :0 @ achievementscript.loadachievementsindictionary (gamedataeditor.gdeachievementsdata gdedata) [0x00000] in :0 @ achievementscript.load () [0x00000] in :0 @ achievementscript.onapplicationfocus (boolean focusstatus) [0x00000] in :0
asking same question gde team, if has idea why happening, it'd great have enlightenment.
multiple time access achievement[0] , achievement[1], without checking size of achievement first - can result in out of index exception because there aren't enough elements in achievement. note accessing [0] can cause error if said list<> empty.
to solve this, make sure check list size. can done via linq
foreach(list<string> achievement in gdedata.achievements.where(l => l.count >= 2) or via if statement
foreach(list<string> achievement in gdedata.achievements) { if(achievment < 2) continue; // put rest of foreach logic here. }
Comments
Post a Comment