GameMaker (solved)Big data structure, easy to handle

S

Smenkare

Guest
I need data structure with about 700 objects, every object needs to store 6 properties. I need to be able to change, get these values, and look for values through this whole data structure. What is the best way to make it? Ds_list, grid or simple array? If someone has time, please show me some example. Thank you for your time.

ps. if i won't store this data but i make 700 objects, but there will be only one of them at the time on screen, will this affect game performance?
 
Last edited by a moderator:
H

Homunculus

Guest
You can store the data in different ways, but in the end it all comes down to in what way and how frequently you need to access this data, since every structure has its own tradeoffs. I see two main options here you can consider:

1. ds_maps
Model every object as a ds_map holding 6 key / value pairs, and store those in a container ds_map. Every object should have a unique ID so you can access it directly starting from the container map.

2. ds_grid
Create a ds_grid with 6 columns, where every row represents an object and every column a property (consider using enums to label the columns).

Solution 1 has the advantage of letting you access a specific object properties in a direct way, without having to loop through every entry. If you need to loop every object frequently though, you are out of luck with ds_maps since it's a very slow process (although there are workarounds for this). Accessing an object properties is also extremely easy and descriptive, it's simply something like var prop = obj[? "some_prop"];

Solution 2 is better if you need to sort, search or frequently loop through all the data, and you can take advantage of the built in ds_grid functions to quickly find an object having a specific property. It's a bit more inconvenient though to manage the whole data set as a whole, since deleting objects or creating new ones requires resizing the grid and moving the data manually. You also don't have the same "self contained" approach to objects you get with ds_maps, it's just a row in a grid.

You may also consider arrays, considering though that you can't really delete entries in array in GM and you don't get as much functionality out of the box as other data structures.

This is a very general explanation, but there's no right or wrong choice here, as said above the ideal solution depends on the details.
 
Last edited by a moderator:
S

Smenkare

Guest
It was helpfull. And my last question, will 700 objects but only one at the time on screen affect game performance?
 
C

Catastrophe

Guest
Regarding objects, objects run their code offscreen, so having 699 offscreen will affect performance. You can deactivate objects that aren't needed, but deactivation itself can be expensive and a hassle keeping track of ids. That said, deactivating all of an object (using the one function designed to deactivate all of an object_index at once) I've heard is pretty inexpensive, so you can deactivate all of them and reactivate the one you need onscreen. But you'd need to keep track of the instance_ids in a ds_list so you know which one to reactivate.

But yeah, for a quesiton like this, the details of the case can really help/
 
Last edited by a moderator:
H

Homunculus

Guest
Just to be sure, by "objects" do you mean actual GM objects (as in obj_player, obj_enemy...), instances, or just a bunch of data?
 

samspade

Member
Dealing with data structures is never going super easy to handle, but if you aren't doing too much, it doesn't have to be that complicated either. I would strongly recommend ds_maps or ds_lists. I would recommend against ds_grids or using raw items. Items provide a few advantages but there's a larger overhead cost and, most importantly, it is difficult to save. ds_grids provide one minor advantage (the ability to index in a single line) but otherwise are far more inflexible, harder to work with, and most importantly, can't be easily saved. Basically, if your data doesn't have a grid like spacial relationship don't use a ds_grid.

Lists and maps are fast, have the greatest functionality, and are easy to save. Arrays would be pretty similar, but they can't save as easily, so if saving is important I'd stick with lists or maps. Without knowing the details of what the data is, it's hard to say the best structure for the lists and maps.
 
S

Smenkare

Guest
I think about a game where choices have impact on the game. Something similar to Reigns game on the android. I mean gm objects.

example

i draw a card and depends what i do it changes variables in other objects or in data structure.
 
H

Homunculus

Guest
i draw a card and depends what i do it changes variables in other objects or in data structure.
So you actually mean instances? Sorry if I'm being pedantic, but it's an important point.
 

samspade

Member
I think about a game where choices have impact on the game. Something similar to Reigns game on the android. I mean gm objects.

example

i draw a card and depends what i do it changes variables in other objects or in data structure.
That's not very descriptive. You'll probably have to be more specific. What is in your data structure? What are the types of data that need to be stored? You said six properties in your original post, what are these properties?
 
S

Smenkare

Guest
If i use data structure there will be only one object with changing data, he will change sprites and change data in data structure. If i dont use data structure i will use objects. Every card will be different object with different variables changing other objects and their variables. I just don't know whats the best way to do it.
 
S

Smenkare

Guest
Properties are: card's index, card is in pool of cards to pick true/false, card is still able to be added to pool, probability to be picked from pool etc. I didnt think that its important what kind of properties they are.
 

samspade

Member
Properties are: card's index, card is in pool of cards to pick true/false, card is still able to be added to pool, probability to be picked from pool etc. I didnt think that its important what kind of properties they are.
I would use a map of maps for that. Main map is a map of all the cards by name. Each card is a map that has those values, also by name. But it wouldn't be that much different to use a list. Either way, it would be pretty easy to edit these externally and just load them in. I've done this on a couple small projects and it is nice. You can load in cards without restarting the game as well.
 
H

Homunculus

Guest
I would use a map of maps for that. Main map is a map of all the cards by name. Each card is a map that has those values, also by name. But it wouldn't be that much different to use a list. Either way, it would be pretty easy to edit these externally and just load them in. I've done this on a couple small projects and it is nice. You can load in cards without restarting the game as well.
I agree. Having the cards stored as JSON externally is a good idea. I tend to use a map of maps for this kind of situation, and eventually also keep a ds_list of every card name/id in case I need to loop through every card.
 
C

Catastrophe

Guest
yeah if you have 700 cards, you'll want a ds_list backing your ds_map. Looping through a ds_map with 700 things is pretty slow. I think it's n^2. If it's nlogn I wouldn't bother, though.

Or just use a ds_list with a ds_map and give each card a numbered index.
 
Top