GMS 2 [SOLVED] ds_map issue, creating a small database

Alloi

Member
I'm creating a simple system in which people are created, then they are added to a database which has their ID, Village_ID, and Job Title. As well as a method to create villages. The problem I'm running into is that I had a ds_map value of "people" starting equal to an empty array [ ]. Then when running the for loop that creates them, it should add them to that array inside the ds_map, although it is not.

Below is my Code and Output:

Code:
if (!variable_global_exists("World")) {
    global.World = ds_map_create();
}
var World = global.World;
//TO DO//

    //Add ds_list for people and village


if (!ds_map_exists(World,"people")) {
    ds_map_add(World,"people",[]);
}

if (!ds_map_exists(World,"villages")) {
    ds_map_add(World,"villages",[]);
}

//[
//    ["ID","Village","Job"],
//    ["ID","Village","Job"],
//    ["ID","Village","Job"],
//]

global.population = 1000;

for(var i=0;i<global.population; i++) {
    var x1 = random_range(64,room_width-64);
    var y1 = random_range(64,room_height-64);
    var newP = instance_create_layer(x1,y1,"Instances",oHuman);
    var people = ds_map_find_value(World,"people");
    var arrInd = array_length_1d(people);
    people[arrInd] = [newP.id, 0, "None"];
    show_debug_message(string(arrInd));
    show_debug_message(string(people));
    ds_map_add(World,"people",people);
}

var arr = json_encode(World);
show_debug_message("People List: "+string(arr));

........
0 //Index position
{ { { { 101000,0,None }, } }, } //value set
0 //Index position
{ { { { 101001,0,None }, } }, } //value set
0 //Index position
{ { { { 101002,0,None }, } }, } //value set
0 //Index position
{ { { { 101003,0,None }, } }, } //value set
People List: { "villages": [ ], "people": [ ] } //ds_map json value

I know my issue is inside the for loop, I'm having difficulty figuring out how to properly add to the array inside the ds_map

Thank you in advance for any help!
 
Last edited:

Slyddar

Member
You need to reference the array with the @ accessor, as you've saved the contents of the ds_map to a local variable, so the accessor will allow the original location to be updated.
Code:
    people[@arrInd] = [newP, 0, "None"];
Also creating an instance returns the id of the instance created, so newP.id is the same as newP in this case.
 
Top