Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#] Sequential Arrays?

Discussion in 'Scripting' started by Ericks89, May 14, 2015.

  1. Ericks89

    Ericks89

    Joined:
    Nov 3, 2012
    Posts:
    39
    Hey!

    I have a pretty basic question, right now I'm porting over a 2D RPG Engine I did in Game Maker now I used ds_list which according to the manual were sequential arrays the index is increased by 1 whenever a new entry into the array is created.

    So here's the script in Game maker for creating our Hero's.
    Code (csharp):
    1. // rpg_add_character(name, level, maxhp, maxki, bp, atk, def, pow, spd, evade, addtoparty?);
    2. var map;
    3.  
    4. // This portion creates the structure for the new character
    5. map = ds_map_create();
    6.  
    7. // Now we add it to the database so we don't ever lose it!
    8. ds_list_add(_rpg_character_database, map); // Now it's there! Safe!
    9. _rpg_character_mostrecent = map;           // Flag as most recent, we may refer to this global variable for the map index if we need it
    10.  
    11. // Create the character stats!
    12. ds_map_assign(map, 0, string(argument0)); // Name
    13. ds_map_assign(map, 1, argument1); // Level
    14. ds_map_assign(map, 2, argument2); // HP
    15. ds_map_assign(map, 3, argument2); // Max HP
    16. ds_map_assign(map, 4, argument3); // Ki
    17. ds_map_assign(map, 5, argument3); // Max Ki
    18. ds_map_assign(map, 6, argument4); // Battle Power
    19. ds_map_assign(map, 7, argument5); // Attack
    20. ds_map_assign(map, 8, argument6); // Defense
    21. ds_map_assign(map, 9, argument7); // Power
    22. ds_map_assign(map, 10, argument8); // Speed
    23. ds_map_assign(map, 11, argument9); // Evade
    24.  
    25. // Add it to the party?
    26. if(argument10) {
    27.    // Yes, add this new character to the party.
    28.    if(rpg_group_size() < 3) { // 3 would be the maximum party size, it would be best to make this a constant
    29.       // Good! There was a slot available! (this is easy)
    30.       ds_list_add(_rpg_group, map);
    31.    }
    32.    else {
    33.       // Ah crap... now what are we going to do?
    34.       // In most RPGs, they don't do anything by default. If you want to really make this character switch parties, manually do that later on. [This happens in cutscenes and the like.]
    35.       show_debug_message("RPG GROUP ERROR: Not enough room in party to add '" + string(argument0) + "' to the group.");
    36.       return 0;  // But since we're cool, we're going to let the programmer know that we didn't add them to the party successfully.
    37.    }
    38. }
    39.  
    40. // Return that everything went according to plan
    41. return 1;

    So for example I run rpg_add_character it's going to come up with 0,1,2 indexes respectively but all this code does is add the character to the database we still need to add them.

    Code (CSharp):
    1. // rpg_group_member_add(mapindex);
    2. // This script removes a party member by their mapindex. Will not add if there is no room in the group.
    3. if(rpg_group_size() >= 3) {
    4.     show_debug_message("RPG GROUP ERROR: Not enough room in party to add map index '" + string(argument0) + "' to the group.");
    5.     return 0; // There was no room for the character!
    6. }
    7.  
    8. // Check to make sure there isn't already a copy of this member in the party.
    9. if(rpg_group_member_find_index(argument0) >= 0) {
    10.     show_debug_message("RPG GROUP ERROR: A character using the map '" + string(argument0) + "' was already found in the group.");
    11.     return 0;
    12. }
    13.  
    14. ds_list_add(_rpg_group, argument0);
    15. return 1;

    And sorting them is pretty simple as we know that they were created sequentially and it's always a good idea to have some sort of cheat sheet when coming back so say created 20 characters you have 0-19 created indexes but the party will only have 0-2 so regardless if we change out with say the 19th index, our formation code will still work.

    Sloppy but works.
    Code (CSharp):
    1. if keyboard_check_pressed(vk_down) {
    2.     if first_selection = false {
    3.         cursor = cursor + 1;
    4.     } else {
    5.         cursor1 = cursor1 + 1;
    6.     }
    7. }
    8. if keyboard_check_pressed(vk_enter) {
    9.     // first key pressed activates first selection
    10.     selection_pressed = selection_pressed + 1;
    11.     if first_selection = false {
    12.         if cursor = 0 {
    13.             swap1 = 0;
    14.             first_selection = true;
    15.         } else if cursor = 1 {
    16.             swap1 = 1;
    17.             first_selection = true;
    18.         } else if cursor = 2 {
    19.             swap1 = 2;
    20.             first_selection = true;
    21.         }
    22.     }
    23.     // second key pressed activates the second selection and activates swap
    24.     if first_selection = true {
    25.         if selection_pressed > 1 {
    26.             if cursor1 = 0 {
    27.                 swap2 = 0;
    28.                 second_selection = true;
    29.             } else if cursor1 = 1 {
    30.                 swap2 = 1;
    31.                 second_selection = true;;
    32.             } else if cursor1 = 2 {
    33.                 swap2 = 2;
    34.                 second_selection = true;
    35.             }
    36.         }
    37.     }
    38. }
    39.  
    40. // Swap characters
    41. if first_selection and second_selection = true {
    42.     rpg_group_member_swap(swap1,swap2);
    43.     instance_delete(self);
    44. }
    45.  
    46. // make sure the cursor doesn't go beyond the maximum allowed party
    47. if cursor > 2 {
    48.     cursor = 0;
    49. }
    50. if cursor1 > 2 {
    51.     cursor1 = 0;
    52. }
    53.  


    I know this was a long list of code and text but wanted to give an idea of what I'm trying to do.
    So basically what I'm asking does C# have any way of doing sequential arrays? without creating various 2d arrays?
     
  2. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Look at System.Collections.Generic.List
     
  3. Ericks89

    Ericks89

    Joined:
    Nov 3, 2012
    Posts:
    39
    Thanks man!