#help with getting ds_map_find_value

1 messages · Page 1 of 1 (latest)

west barn
#

i have that ds_map on the screan shot and that code to:
function scr_player_res_data_send(_username, _buffer) {
var _list_id = ds_map_find_value(obj_server._players_eq_stats, _username);
// Sprawdzamy, czy _list_id to faktyczna lista
if (ds_exists(_list_id, ds_type_list)) {
var _player_res_json = json_stringify(_list_id);
buffer_write(_buffer, buffer_string, _player_res_json);
show_message(_player_res_json);
} else {
show_debug_message("Błąd: Brak listy dla gracza " + string(_username));
}
}
but only thing that i got from this code is 74 that is id of "aaa". How can i get full ds_list that is on this key "aaa"?

#

*ds_list_find_index() have no use in this code, i forgot to delete when posting

limber pebble
# west barn i have that ds_map on the screan shot and that code to: function scr_player_res_...

By "only thing that i got from this code" you mean what you got from
show_message(_player_res_json); ?
Lists (and other ds_ stuff I believe) hold a pointer to a resource, so you would have to loop through it

var list_contents = "";
for (var i = 0; i < ds_list_size(_list_id); i++) {
    list_contents += string(_list_id[| i]) + ", ";
}
show_message(list_contents);

Additionally, you can't json_stringify data structures, you need json_encode for that

Any reason you're not using structs and arrays?

west barn
#

I have to save it to a json file (so I don't lose it after a reset) for that I use ds_map because it's easier to convert and save to .json. If I used regular arrays I wouldn't have any problem except that I would have to assign numbers to users to refer to specific data by.

#

that message i got when run your script (i have 74 list in this ds_map and all i got is just their id-s

#

i make this _player_eq_stats decoded and got _player_res_decoded

limber pebble
#

I have to save it to a json file (so I don't lose it after a reset) for that I use ds_map because it's easier to convert and save to .json.
How is it easier?
If I used regular arrays I wouldn't have any problem except that I would have to assign numbers to users to refer to specific data by.
I'm not proposing to use arrays in place of map
Structs instead of maps, arrays instead of lists
Both are easier to use, have more functions, are automatically garbage collected

limber pebble
west barn
#

I read this article and wonder why I haven't used it before (:

limber pebble
#

Yes, and they are quite similar to maps
Data structures (ds_something) are rarely used nowdays, usually only when you're forced by some built-in function using them.
Structs and arrays are the way

Haven't watched it, but seen SamSpade recommended a bunch
so may look at these if manual won't be enough
https://www.youtube.com/playlist?list=PLwgH1hDD0q1FLsKwe8jZ_08YZa5yuMe6E

west barn
#

but only question i have now, how to convert this and save as a file?

limber pebble
west barn
#

thx a lot

#

my last question about this:?
if i make something like this
_res_data =
{
aaa : [
[0,0,0,0],
[1,1,1,1],
[2,2,2,2]]
}
it will normal work with 2d arrays?

limber pebble
#

Aye, this is correct
2d array inside a struct

west barn
#

i see that my .json with this data is already format to this (:

west barn
#

next question (:
how can i make that this key _username will change depend of what i set it?

#

because now when i set "aaa" it make me "_username"

brazen nova
#

_shop_res_data[$ _username] = ..., can be used to both set and get

#

$ for $truct

#

ds_maps have something similar, ds_map[? _username]

west barn
#

but this is to find something, i need to make new struct with this

#

i know that i do something wrong but don't know how to make it right

#

i read that GameMaker does not allow you to dynamically add new keys to regular JSON structures

limber pebble
west barn
#

how?

limber pebble
# west barn

Here, if you're trying to do roughly the same as in picture above (have an array named _username inside _ship_res_data struct) you just gotta drop the curly braces encasing your array

#
_ship_res_data[$ _username] = [
  [0, ...
west barn
#

i read how can i add value of key from struct but i can't find how to add key to existing struct

west barn
#

and when i do egzacly like you its says that is not set before reading

limber pebble
#

Would have to see what exactly you're doing
Also the full error message

west barn
#

and its need to be some _ship_res_data[$ _username] = [
data: [0, ...

limber pebble
#
struct[$ newkey] = some_value;

It's the same as using struct_set

weary prismBOT
#

With this function you can set the value of a given variable in a struct. You supply the struct reference as well as the name of the variable to set the value of as a string (see example code below), and then finally the value to set (can be any valid data type). If the variable does not exist already in the struct it will be created and then assigned the value.

Arguments
struct: The struct reference to set
name: The name of the variable to set (as a string)
val: The value to set the variable to

west barn
limber pebble
#

_ship_res_data does not exist
Where do you define it?

brazen nova
#

_ship_res_data must exist first. it's not a new struct

if you want it to make a new struct you gotta do _ship_res_data = {} first

#

and then you can modify whatever key you want

#

(or perhaps check if it exists first, or something)

#

idk if this is like a global struct or what