• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED DoAdd error with ds_map

hey there!

I'm very new to data structures, so my code won't be great.

I'm trying to make a map system that I'm using as an inventory for my survival game. For some reason I don't understand, I keep getting this error.

___________________________________________
############################################################################################
ERROR in
action number 1
of Step Eventofirstaid
for object player:

DoAdd :1: undefined value
at gml_Object_player_Collision_pickupparent (line 10) - value += ds_map_find_value(global.inventory,key);
############################################################################################
gml_Object_player_Collision_pickupparent (line 10)

Here's the code in question. Its in a collision even with a parent to the pickups.

var key_pickup = keyboard_check_pressed(ord("E"))

if key_pickup {
if other.object_index != noone {
var key = other.object_index;
show_debug_message("keyis: "+string(key))
var value = 1;
show_debug_message("valueis: "+string(value))

value += ds_map_find_value(global.inventory,key);

ds_map_add(global.inventory,key,value);

show_message("Item: "+string(key)+" Value: "+string(value))

instance_destroy(other)
}
}

I did a little bit of research, and found that the DoAdd error is generally when the computer tries to add a value to something that can't have values added to it. To be honest, it should be a simple fix but I need a little help because I'm so darn new.

Thanks for stopping by, I can give you any other events and lines of code if you ask for them.
 

kburkhart84

Firehammer Games
I believe that your problem is that that there is no "key" key in the map, therefore the value it is returning is undefined, and likely you can't undefined to 1. Can you show us the code where you put that key into the map in the first place. Note that you are trying to use a key for the map that is the id of an object instance, so if that isn't the key that you have put into the map, then that is why you would be getting undefined when you try to pull the value of that key.
 
I believe that your problem is that that there is no "key" key in the map, therefore the value it is returning is undefined, and likely you can't undefined to 1. Can you show us the code where you put that key into the map in the first place. Note that you are trying to use a key for the map that is the id of an object instance, so if that isn't the key that you have put into the map, then that is why you would be getting undefined when you try to pull the value of that key.
do you mean checking to see if the key exists before calling it?
 

kburkhart84

Firehammer Games
do you mean checking to see if the key exists before calling it?
Yes, or checking if the value returned is undefined...even the manual recommends you check that. Like I said, post the code where you are putting the key/value stuff into the map in the first place, that would help figure things out.
 
Yes, or checking if the value returned is undefined...even the manual recommends you check that. Like I said, post the code where you are putting the key/value stuff into the map in the first place, that would help figure things out.
I thought defining the value and key in the var was enough... do I need to do more? the only other code I have is the ds_map_create code, what do I need to add?
 

kburkhart84

Firehammer Games
It seems you don't understand how this works, maybe I'm missing something. To pull a value from a data structure(like a map), you have to first put the value into said map. You can't just put numbers into variables and expect to use those to pull something out of an empty map. Either you have not added anything to the map(which is why you are having issues), you are putting something into the map, but not the key/value pair you think you are(which is something you haven't shown code for if you are), or I'm simply wrong(which we could prove if you showed code where you added key/value pairs to the map).

Let me clarify, you have a line of code where you are adding to the map, but you are trying to pull a value from the map BEFORE you do that.
 
It seems you don't understand how this works, maybe I'm missing something. To pull a value from a data structure(like a map), you have to first put the value into said map. You can't just put numbers into variables and expect to use those to pull something out of an empty map. Either you have not added anything to the map(which is why you are having issues), you are putting something into the map, but not the key/value pair you think you are(which is something you haven't shown code for if you are), or I'm simply wrong(which we could prove if you showed code where you added key/value pairs to the map).

Let me clarify, you have a line of code where you are adding to the map, but you are trying to pull a value from the map BEFORE you do that.
okay, I get it now, the map actually is empty. I've got no idea why I thought that would work. thanks for your help!
 
Top