Pages: 1
  Print  
Author Topic: How to access arrays stored in a ds_list ?  (Read 28637 times)
Offline (Unknown gender) ojars
Posted on: June 10, 2019, 05:22:01 am
Member
Joined: May 2019
Posts: 16

View Profile
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?
« Last Edit: June 10, 2019, 05:29:45 am by ojars » Logged
Offline (Male) Goombert
Reply #1 Posted on: June 10, 2019, 07:03:42 am

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2993

View Profile
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.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Male) hpg678
Reply #2 Posted on: June 10, 2019, 07:35:12 am

Member
Location: Barbados
Joined: Mar 2017
Posts: 283

View Profile Email
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.
« Last Edit: June 10, 2019, 07:55:11 am by hpg678 » Logged
[compromised account]
Offline (Unknown gender) ojars
Reply #3 Posted on: June 10, 2019, 08:06:44 am
Member
Joined: May 2019
Posts: 16

View Profile
Thank you both! I'll be waiting and I''ll be patient...and let me know.
Logged
Offline (Male) hpg678
Reply #4 Posted on: June 10, 2019, 08:07:38 am

Member
Location: Barbados
Joined: Mar 2017
Posts: 283

View Profile Email
ok here's a little update to your problem relayed to me by Goombert....one of the major developers


  • Don't declare the array with 'var'
  • use array_create(<size>) function to allocate memory for the ds_list......it'll be much faster

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.





Logged
[compromised account]
Offline (Unknown gender) ojars
Reply #5 Posted on: June 10, 2019, 08:29:04 am
Member
Joined: May 2019
Posts: 16

View Profile
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?
« Last Edit: June 10, 2019, 08:43:14 am by ojars » Logged
Offline (Male) Goombert
Reply #6 Posted on: June 10, 2019, 08:51:41 am

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2993

View Profile
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.
« Last Edit: June 10, 2019, 08:54:14 am by Goombert » Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) ojars
Reply #7 Posted on: June 10, 2019, 09:28:53 am
Member
Joined: May 2019
Posts: 16

View Profile
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, 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;


« Last Edit: June 10, 2019, 02:37:24 pm by ojars » Logged
Offline (Male) Goombert
Reply #8 Posted on: June 10, 2019, 02:48:39 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2993

View Profile
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.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) ojars
Reply #9 Posted on: June 10, 2019, 03:10:41 pm
Member
Joined: May 2019
Posts: 16

View Profile
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.
Logged
Offline (Male) Goombert
Reply #10 Posted on: June 10, 2019, 05:39:03 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2993

View Profile
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.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Dragonite
Reply #11 Posted on: June 13, 2019, 09:56:08 pm
Member
Joined: Mar 2017
Posts: 20

View Profile
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.
Logged
Offline (Male) Goombert
Reply #12 Posted on: June 15, 2019, 01:05:39 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2993

View Profile
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.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Dragonite
Reply #13 Posted on: June 16, 2019, 12:38:32 pm
Member
Joined: Mar 2017
Posts: 20

View Profile
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?
« Last Edit: June 16, 2019, 12:54:44 pm by Dragonite » Logged
Offline (Male) Goombert
Reply #14 Posted on: June 16, 2019, 05:29:34 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2993

View Profile
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
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Pages: 1
  Print