• 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 ds_map doesn't change its content

A

alexfresh

Guest
Hi, I broke my game and can't figure out what I did wrong :/

So my game has entities (animals and plants) that have a lot of common variables to be able to access common scripts.

To define the default values of each species variables I use ds_maps that are set up in a game initialisation script, performed by a persistent control object, that also holds all kinds of ds_maps and other variables.

I use a ds_map for each entity type and had them set up seperately in the past, it looked like this:

// MOSS
moss_map = ds_map_create();
moss_map [? "does_procreate"] = true;
moss_map [? "procreate_cooldown"] = 3;
[...]
// POOP
poop_map = ds_map_create();
poop_map [? "does_procreate"] = false;
poop_map [? "procreate_cooldown"] = 0;
[...]


Everything was fine, but it was a ton of copying, pasting and replacing the map name, whenever I set up a new species, so I decided to use another map for initialisation, so I would just have to replace two lines for each object and then focus on the actual values instead of all that copypasting. Kinda like this:

// MOSS
init_map = ds_map_create();
moss_map = ds_map_create();
init_map [? "does_procreate"] = true;
init_map [? "procreate_cooldown"] = 3;
[...]
moss_map = init_map;
ds_map_destroy(init_map); // I tried this with and without destroying the init_map, same result

// POOP
init_map = ds_map_create();
poop_map = ds_map_create();
init_map [? "does_procreate"] = true;
init_map [? "procreate_cooldown"] = 3;
[...]
poop_map = init_map;
ds_map_destroy(init_map);


Now all entities apparently spawn with the default values for the last entity type I defined in the initialisation script. That's just wrong and I can't get behind it, I mean I made the init_map stored its content in the moss_map, then gave it different values and stored them in another map - how can the moss_map get the values from these other maps?

The script is only triggered once during a game start event, what is happening here?

Btw the game was already super cool before it broke :/
 

samspade

Member
Data structure create functions ( such as ds_map_create() ) return an index to that data structure. So:

GML:
// MOSS
init_map = ds_map_create(); //creates a map, lets say the index is 0
moss_map = ds_map_create(); //creates a map, lets say the index is 1

/*some code*/

moss_map = init_map; //the variable moss_map now equals 0
ds_map_destroy(init_map); //you just destroyed the map moss_map is equal too
Also since data structure indexes are reused once destroyed, you could be repeating that process for other data structures over writing them as you go.


For more on data structures in general you could watch:



You could turn this into a script though:

GML:
/// @function init_map(does_procreate, procreate_cooldown);
var _map = ds_map_create();

ds_map_add(_map, "does_procreate", argument0);
ds_map_add(_map, "procreate_cooldown 2", argument1);

return _map;

///use
moss_map = init_map(true, 3);
 

Nidoking

Member
If you're going to use a single initializer map that way, the function you want is ds_map_copy.

But you could also map object indices to map indices using yet another map, and use either inheritance or a common script to initialize all of the common elements in one place. Each object type would then just need to overwrite the specific elements it changes. So something like:

GML:
if (is_undefined(global.map_of_maps[? object_index]))
{
  global.map_of_maps[? object_index] ds_map_create();
  init_map(global.map_of_maps[? object_index]);
  // Set specific values here
}
 
A

alexfresh

Guest
Hi, thanks for your fast answers, wow this community is really great.

@samspade ohh ok, thanks, now I see what I did wrong. Unfortunately the maps I use have too many values to pass them as arguments to a script.

@Nidoking thanks! I will check and try the ds_map_copy command, that sounds great. The code you suggested even looks better, I just need to figure out how it works, I'm pretty new to code in general and it takes me a bit to understand this - for simplicity I go with the ds_map_copy command I think

I'll keep you updated

Thanks for your replies <3
 

samspade

Member
Hi, thanks for your fast answers, wow this community is really great.

@samspade ohh ok, thanks, now I see what I did wrong. Unfortunately the maps I use have too many values to pass them as arguments to a script.

@Nidoking thanks! I will check and try the ds_map_copy command, that sounds great. The code you suggested even looks better, I just need to figure out how it works, I'm pretty new to code in general and it takes me a bit to understand this - for simplicity I go with the ds_map_copy command I think

I'll keep you updated

Thanks for your replies <3
Scripts can accept an unlimited amount of values. Unless you mean it just isn't practical. Scripts can also accept arrays as arguments meaning you could put all values in an array and pass just one argument.
 
Top