1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

[Solved]New list at Runtime

Discussion in 'Programming' started by dcamod, Nov 8, 2019.

  1. dcamod

    dcamod Member

    Joined:
    Feb 9, 2017
    Posts:
    61
    Hello all. I was just wondering how I should go about creating a button that when pressed creates a new unique list every time.
     
  2. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,974
    1. Place an object with a button sprite in the room
    2. In its Mouse Left Pressed Event,
    Code:
    randomize()
    mylist = ds_list_create()
    repeat(irandom(999)){
      ds_list_add(mylist,irandom(9999999))
    }
    //now you have a random list, do something with it
    
     
  3. dcamod

    dcamod Member

    Joined:
    Feb 9, 2017
    Posts:
    61
    Thanks for the reply but that is not exactly what I was looking for. I need a unique name for the list. This looks like it would just create a list named myList over and over with a random number assigned to index 0. I do not need the same list I need a unique list with a unique name.
     
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,124
    Exactly what Yal said but add it to an array or list.
     
  5. dcamod

    dcamod Member

    Joined:
    Feb 9, 2017
    Posts:
    61
    Yeah great and all but what am I missing is what I was asking. No need to be rude. Never asked anyone to write my code for me, just trying to learn, you did not need to reply at all. That's awfully volatile and I reported you for it. If I am wrong please explain why instead of mocking me. How ridiculous and childish.

    I do see the random number is assigned as the array size now. So even mocking me you were helpful somewhat. I have not used arrays or lists for anything yet so I did not know. You could have just said that nicely. So the array has a unique index but not a unique name. I was looking for how to give it a unique name with a string. I did not mention I needed a string before, that is my fault.

    So, Samspade said add it to another list which I get, but how do I give it a string as it's name in the list.
     
    Last edited: Nov 12, 2019
  6. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    123
    If you want to access data from that list by a string identifier it's going to be more complicated because internally there is no such thing. You will need a second reference map that you use to connect a label to a row in the ds list. Alternatively you could simply use a ds_map, it allows you to store a key => value pair and then you would use the ds_map_find_value() to search by the key (your label) and get the value you want.

    https://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds maps/index.html

    It should probably be worth noting that this is likely much slower than using an array, or ds_list, and referncing things by their ID rather than by string. If your going to be referencing many dozens of objects on every step by these labels you might want to rethink that, but if it's something your running once or every so often it should be fine. Even then, every step is probably okay, but only to some extent I imagine.
     
    dcamod likes this.
  7. robproctor83

    robproctor83 Member

    Joined:
    Sep 30, 2019
    Posts:
    123
    Oh, one more thing I wanted to mention, you should take whatever method you end up using and wrap it in a custom function, this way in the future if you figure out a better way or need to extend it in some way you can, hopefully, just edit the custom script you setup.

    I recently did something similar for the configuration options of my game, but the big difference is that in my scenario the number of options doesn't change after runtime. Originally I was just calling everything from the array like config[2,4] but after a while it got tedious trying to remember what the options were. Because I have a fixed number of options I was able to create a enum to select the options. It's a bit more tedious to setup though because you have to define the enum outside of the array or data structure you use to store the actual options, plus you have to make a custom script that switches based on the enum property that you pass it. So, for example, var gamepad = settings_get(Config.UseGamepad) and then in the custom settings_get script you make a switch on Config for each option that then returns the correct row from the master settings array.
     
    dcamod likes this.
  8. dcamod

    dcamod Member

    Joined:
    Feb 9, 2017
    Posts:
    61
    Thanks for replying. I will test this out as soon as I am done with testing it in c#. I switched to Unity for this and it is working great. Maybe it's time to migrate away from game maker...
     
  9. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    818
    So you switched to unity because you are not able to assign a list to another list or ds_map? I’m glad to read you were able to solve it, but this is not a problem related to the tool you use, it’s totally up to your skill as a programmer
     
  10. dcamod

    dcamod Member

    Joined:
    Feb 9, 2017
    Posts:
    61
    No, I am not switching entirely just choosing to use one over the other more often now and the reason is because of the flexibility. The issue was assigning a list a string as it's name that a user inputs at Runtime, not assigning a list to a ds_map. That is the easy part of it. Why not post something helpful? That was just a round about way of insulting me. Obviously I am not a skilled programmer or I would not need to ask the question. I am marking this as solved because it is leading nowhere and I have solved it in c#.
     
  11. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    818
    I admit I was trying to provoke a bit, but the reason being that I think you are approaching this in the wrong way, I hoped to get that point across, since I think that solving a problem like this by switching to another tool is totally silly.

    About addressing a list based on a string, I assume you mean getting the list index not by using a variable name but a string instead, like "list_1" as opposed to list_1 . You can't do that directly, unless we involve compatibility scripts like variable_instance_get which is not ideal. That's why people are suggesting ds_maps, since the end result is the same, allowing you to do exactly what you want. Example:

    Code:
    //initialize the ds_map somewhere
    list_collection = ds_map_create();
    
    Code:
    //creating a list using the string "my_awesome_list" as its name
    var list = ds_list_create();
    list_collection[? "my_awesome_list"] = list;
    
    Code:
    //get a list by its name
    var list = list_collection[? "my_awesome_list"];
    
    Having a user input instead of a hardcoded string doesn't change the method, you'll have a variable instead of "my_awesome_list".
     
    Last edited: Nov 18, 2019
  12. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,473
    Yeah, associating a list to a string (name) is best done with a map. That's what they do best.

    Out of curiosity, what was your solution in c#? The first solution I can think of us using a dictionary, which is a map...
     
    dcamod likes this.
  13. dcamod

    dcamod Member

    Joined:
    Feb 9, 2017
    Posts:
    61
    I was trying to solve the problem in Unity, not GameMaker. I did not switch tools. I asked on this forum while simultaneously asking on the Unity forum so I could get a grasp of the concept because I knew it would be similar. I did not feel the need to describe that because it was irrelevant. I wanted to learn a concept, not see code. Provoking people only puts them down and that is just rude. Why hinder someone when you can simply ignore them? I have done nothing but be nice even when attacked. I said it was time to migrate but I did not say why. You just assumed it was because I could not solve it in GameMaker when in reality I was trying to solve it in Unity. My reason, if you must know, is that I am learning c# in Unity now anyway and it is more flexible. I love GameMaker but I want to expand and learn more.
    Also, I am using gms 1.4 with the Android export module but I can no longer buy the IOS module. Support told me mobile game support for studio will not last much longer and is limited when I asked if I could buy the iOS module still. I did not think I had to elaborate on all of that. I will still use GameMaker and frequent the forums but it will no longer be my primary focus.
    This is where you are mistaken(as were others). I wanted the list, when declared, to be named the string the user inputs. I am not trying to get an index of a list within a list. I am trying to name the list with a string when declared and then add it to another list.

    I have not yet tried this in GameMaker since robproctor83 posted his very helpful post that was not negative in any way. I will eventually do that. Maybe I am missing the point of your explanations, but they seem to be a work around method. This may be faster but not what I was asking for. I am a beginner still, and there is no need to be negative towards someone who is trying to learn. Especially when they are determined to continuously try even if they keep failing.

    I am appreciative of all help. It is invaluable to me, but I do not like being insulted or provoked for simply asking questions. Why have a help forum if everyone gets mad about someone honestly trying to learn just because they do not catch on quickly enough according to whoever's standards.


    Yes, I did use a dictionary to help solve the problem GMWolf you are all correct about that.

    I really wish everyone would relax. There was no need for this to turn so ugly.
     
    Last edited: Nov 19, 2019
  14. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    818
    I don't know what happened before my comment (probably I'm missing some reply that has been deleted), not sure what about my answer offended you to this point. If a reply to someone pointing out that your idea is silly is "I'm offended, you are rude", the problem is yours, not mine. If you told us the context from the beginning maybe this conversation could have gone a lot smoother, since we didn't have to "assume" things (although assuming you want to solve a problem in Game Maker seems like a pretty good assumption to me since we are on the game maker forum!).

    Back on topic: a ds_map is essentially the GM equivalent of a Unity dictionary. The problem you describe is one that is solved by a ds_map, everyone is telling you this for a reason, but you are simply dismissing the answer. You let the user insert the name of the list, and save both in a ds_map as key > value pair. It's not a workaround, ds_maps (also called hashmaps, hashtables or dictionaries in other languages, with minor differences in implementation) are the go to solution when you need to store a set or list of key / value pairs.
     
    Last edited: Nov 19, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice