ENIGMA Forums

Outsourcing saves money => Programming Help => Topic started by: ojars on June 10, 2019, 05:22:01 AM

Title: How to access arrays stored in a ds_list ?
Post by: ojars on June 10, 2019, 05:22:01 AM
Hi!
I wrote a simple test program. In a o_game create event
Code: [Select]
var arr[3];// = array_create(3);
arr[2] = 52;
arr[1] = 4;
arr[0] = 16
list = ds_list_create();
ds_list_add(list, arr);
then in a o_game draw event
Code: [Select]
draw_set_color(c_black);
draw_set_font(font_0);

var arr = ds_list_find_value(list, 0);// expected to get an array
draw_text(200, 200, "list size = " + string(ds_list_size(list)));
draw_text(200, 220, "array length = " + string(array_length_1d(arr)));
for (var n = 0; n < array_length_1d(arr); n++)
draw_text(100, 100 + n * 20, string(arr[n]));

The only thing I got was:
list size = 1
array length = 0
Perhaps I'm doing something wrong? Is there a way to get access to arrays stored in ds_list?
Title: Re: How to access arrays stored in a ds_list ?
Post by: Goombert on June 10, 2019, 07:03:42 AM
We don't have that list function yet, but i can actually add that for you, that's doable now! I'll see if I can get to it tonight.
Title: Re: How to access arrays stored in a ds_list ?
Post by: hpg678 on June 10, 2019, 07:35:12 AM
Greetings and salutations!


i mentioned your problem to one of the developers, so he's going to fixed the problem very soon. Apparently it has do with one of the functions. So please be patient until then.
Title: Re: How to access arrays stored in a ds_list ?
Post by: ojars on June 10, 2019, 08:06:44 AM
Thank you both! I'll be waiting and I''ll be patient...and let me know.
Title: Re: How to access arrays stored in a ds_list ?
Post by: hpg678 on June 10, 2019, 08:07:38 AM
ok here's a little update to your problem relayed to me by Goombert....one of the major developers



Code: [Select]

arr = array_create(3);
arr[2] = 52;
arr[1] = 4;
arr[0] = 16
list = ds_list_create();


and in your draw_event
Code: [Select]

for (var i = 0; i < array_length_1d(arr); ++i) {
    ds_list_add(list, arr[i]);
}


for (var i = 0; i < ds_list_size(list); ++i) {
    show_message(string(ds_list_find_value(list, i)));
}

this should work now.





Title: Re: How to access arrays stored in a ds_list ?
Post by: ojars on June 10, 2019, 08:29:04 AM
Well, I could use this, but arr must be local, so it terminates when the program leaves event or script. List have to contain an array as such, not individual array entries. List may contain tens of arrays and is created dynamically. I use such a lists for lightweight objects.
Where is that code located, perhaps I could have a look at that?
Title: Re: How to access arrays stored in a ds_list ?
Post by: Goombert on June 10, 2019, 08:51:41 AM
Everything hugar said is correct. The following part of the code I gave him can actually go wherever you want it, in the create too. It is copying the array to the list, element by element.
Code: (EDL) [Select]
for (var i = 0; i < array_length_1d(arr); ++i) {
    ds_list_add(list, arr[i]);
}

So the difference between ENIGMA/GM var arrays and data structure lists is subtle but important. In ENIGMA the var arrays are actually Lua tables of variants. The data structure lists are C++ std::vector of variant. Variant is the light weight type which is single dimensional in the case of reals. It uses NaN Boxing to store floating point and scalar values.

Var array functions:
https://github.com/enigma-dev/enigma-dev/blob/master/ENIGMAsystem/SHELL/Universal_System/var_array.cpp

Data struct list functions:
https://github.com/enigma-dev/enigma-dev/blob/eabb1b474d5abf7e2d58ae3eccf4ba875208a9c9/ENIGMAsystem/SHELL/Universal_System/Extensions/DataStructures/data_structures.cpp#L1203

In general, you probably want to use lists exclusively over arrays since they can not only be passed around by id, but their operations are more efficient. They won't be much different from var arrays, they just don't have to do as much work and are written in C++. And ENIGMA again is actually implemented close to GM so you should expect similar results going in the direction of any of the above.
Title: Re: How to access arrays stored in a ds_list ?
Post by: ojars on June 10, 2019, 09:28:53 AM
Yes, I can use lists exclusively. I wonder why I didn't imagine that sooner... lists stored into list...  Anyway thank you.

EDITED.
As I wrote in my post https://enigma-dev.org/forums/index.php?topic=2992.0 (https://enigma-dev.org/forums/index.php?topic=2992.0), I just found a way how to implement structs. I even managed to store struct into list and access it. circle is a struct
Code: [Select]
str = ds_list_create();
circle.x = 300;
circle.y = 400;
ds_list_add(str, circle);
That code was in o_game create event, and this is in a Draw event:
Code: [Select]
var data = ds_list_find_value(str, 0);
draw_text(data.x, data.y, string(data.x));
and the result was seen on the screen, as expected - 300;


Title: Re: How to access arrays stored in a ds_list ?
Post by: Goombert on June 10, 2019, 02:48:39 PM
Hey, that's great! I will still look into fixing the old EGM format saving/loading from the lgmplugin for you. It just makes it easier to save that information.

PS: Also my recommendation about using lists is actually the same as Mark Overmars in the GM8 manual. It's true for scientific reasons that the data structures are faster when you consider operating on lots of data.
Title: Re: How to access arrays stored in a ds_list ?
Post by: ojars on June 10, 2019, 03:10:41 PM
I do believe, that data structures are fast.
I wonder why it's not possible to use struct member functions. As I wrote in my previous post, when I tried to use member function, I got an error
Quote
Event[8, 0] Check `o_game::draw...Syntax error in object `o_game', Draw event:0:
Line 24, position 30 (absolute 770): Unknown function or script `get_area'

I consider to use structs as lightweight objects that don't move.
Title: Re: How to access arrays stored in a ds_list ?
Post by: Goombert on June 10, 2019, 05:39:03 PM
That's just the syntax checker being greedy about what's a script. Needs a fix and then a CI test to keep it working.
Title: Re: How to access arrays stored in a ds_list ?
Post by: Dragonite on June 13, 2019, 09:56:08 PM
Goombert, isn't the code below supposed to work?
Code: [Select]
var arr = array_create(3);
arr[2] = 52;
arr[1] = 4;
arr[0] = 16
list = ds_list_create();
ds_list_add(list, arr);
In GM, you should be able to get back the array using ds_list_find_value(list, 0) whether it was called with var or not.
Title: Re: How to access arrays stored in a ds_list ?
Post by: Goombert on June 15, 2019, 01:05:39 PM
That's correct, yeah, what's it doing in ENIGMA? I understood you, you want to store the reference/pointer to the var array inside the ds list.
Title: Re: How to access arrays stored in a ds_list ?
Post by: Dragonite on June 16, 2019, 12:38:32 PM
I put the following in the create event:
Code: [Select]
var arr[3];// = array_create(3);
arr[2] = 52;
arr[1] = 4;
arr[0] = 16
list = ds_list_create();
ds_list_add(list, arr);

Then, the following in the Step Event:
Code: [Select]
var t = ds_list_find_value(list, 0);
if (is_array(t))
{
show_message("t is an array.");
for (var i = 0; i < array_length_1d(t); ++i)
show_debug_message(string(t[i]));
}
else
show_message("t is not an array,");
game_end();

The output is that "t is not an array".

Edit: just re-read the thread, so ds_list_find_value isn't implemented yet?
Title: Re: How to access arrays stored in a ds_list ?
Post by: Goombert on June 16, 2019, 05:29:34 PM
Just going to make a note here for everybody else. We got this mostly squared away.

He contributed the array_set function which I've approved.
https://github.com/enigma-dev/enigma-dev/commit/ed696c743a117d9648353268cff6b2dac5fbb842

We still have some things to work out with var arrays and accessors though.
https://github.com/enigma-dev/enigma-dev/issues/1751