Ducktards
  • Home
  • Behind the Scenes
  • Daily Dev
  • Server stuff

Lists

24/6/2016

 
Picture
Lists are pretty self explanatory, they work exactly how you'd expect them to.

Functionality
​Nevertheless, they're an incredibly useful resource in programming, so this article will cover how they work.

​
Starting point
One thing you must remember about lists, is that they start at position zero. So if a list has four entries, the final entry will be position 3. This can be confusing at first.
Picture
Picture

GML
Player_List = ds_list_create();

Python
Player_List = list()
​
Just call the list creation function, and a list will magically be created! ​




​



​
Picture
Picture

​GML
ds_list_add( Player_List, "Jake" );

Python
Player_List.append( "Jake" );
​
​Super easy stuff, right? :D










​
Picture

GML
ds_list_add( Player_List, "Bob", "Dave" );

Python
Player_List.extend( [ "Bob", "Dave" ] )
​
To save space and processing time, you can add multiple entries to a list in one swoop.







​


​
Picture
Picture

GML
Name = ds_list_find_value( Player_List, 1 );

Python
Name = Player_List[ 1 ]
​
In this case, you'd get "Bob", as that's what's at position 1.
​
Requesting a position outside the list will return undefined in GML, and will give an error in python.






​



​
Picture
Picture

GML
Pos = ds_list_find_index( Player_List, "Dave" );

Python
Pos = Player_List.index( "Dave" )

You can find the position of an entry in a list. Here, position 2 will be returned, as that's where Dave is at.






​
​
Be careful here!
Picture

​In GML, if you search for an entry that's not in the list, you'll be returned with position -1, which is fine.

But in python, you'll get an error message!

​You can either use a try statement, or you can use the next function I'm about to explain!
​
​







​
Picture

This function is exclusive to Python, but if you need it in GML don't worry, I've got you covered :D
​
Picture

GML (My script linked above)
​Total = ds_list_count( Player_List, "Sam" );

Python
​Total = Player_List.count( "Sam" )
​
Here, Total =  zero, because "Sam" isn't in the list. If an entry occurs zero times in a list, you know not to search for them!









​
Picture
Picture

GML
​ds_list_delete( Player_List, 1 );

Python
del( Player_List[ 1 ] )
​
Simply enter the position on the list to delete. ​If you don't know the position, you can find it with the search code above! 








​
​
Picture
Picture

GML
ds_list_insert( Player_List, 1, "Bob" );
​
Python
Player_List.insert( 1, "Bob" )
​
Bob is back! It's almost like he never was deleted.





​




​
Picture
Picture

GML
Size = ​ds_list_size( Player_List );

Python
Size = len( Player_List )
​










Picture
Picture

GML

​ds_list_shuffle( Player_List );

Python
Player_List.shuffle()
​
Remember, this doesn't guarantee every entry will be in a new position! There's a chance that a new random position will be the same position as before.





​


​

​
Picture
Picture

GML
ds_list_clear( Player_List );

Python
Player_List.clear()

This deletes all the entries in the list, but doesn't actually delete the list itself. 






​


Picture
Picture

GML
ds_list_destroy( Player_List );

Python
del( Player_List )
​
The only difference between the two, is that you're deleting the variable in python. You can't delete variables in GML.

​Instead, you're deleting the structure itself.







​
Thank you for reading, I hope you enjoyed this article!

    Only show:

    All
    Dev
    Documentation
    Networking
    Tutorial

  • Home
  • Behind the Scenes
  • Daily Dev
  • Server stuff