Anyway to do this?

Discussion in 'Programming' started by Jihl, Jan 11, 2019.

  1. Jihl

    Jihl Member

    Joined:
    Nov 26, 2018
    Posts:
    21
    Hey there all!

    I am trying to make an X*2 long array which has pairs of numbers, the first the item id and second the item quantity, I'm doing this with a switch statement but it looks horribly unefficient and an unproffesional piece of code </3

    I need to get the array in once piece so I can pass it as an argument to the final script. But I havent figured out how to do it iteratively.

    Any ideas on how to make this iterative?
    Code:
    var total_slots = buffer_read(buffer, buffer_u8)
     
    // Create auxiliar list and array
    var list_aux = ds_list_create()
    var array_aux = 0
     
    for (var i = 0; i < total_slots; i++)
    {
       // Add item to the list
       ds_list_add(list_aux, buffer_read(buffer, buffer_u16))
      
       // Add item quantity to the list
       ds_list_add(list_aux, buffer_read(buffer, buffer_u16))
    }
    
    // Make array to pass as argument
    switch total_items
    {
        case 1:
            array_aux = array(list_aux[0], list_aux[1])
            break
         
        case 2:
            array_aux = array(list_aux[0], list_aux[1], list_aux[2], list_aux[3])
            break
    
       case 3: ...
       case 4: ....
    }
    
    // Show the corpse loot "You don't need to know where does the corpse variable comes from for this post"
    with corpse scr_show_loot_ensured(total_slots, array_aux)
    
    Thank you very much! :D
     
    Last edited: Jan 11, 2019
  2. Kyon

    Kyon Member

    Joined:
    Jun 27, 2016
    Posts:
    408
    Look into "for" statements.
    Like,
    Code:
    for (i=0; i<4; i+=1;){
    list_aux[i]=0;
    }
    which made 4 array variables.
    Change that "4" to another variable, like max items, or amount of items you have at the moment or idk, not sure what you're trying to do.
    But that "for" statement will help you.
     
  3. Jihl

    Jihl Member

    Joined:
    Nov 26, 2018
    Posts:
    21
    I get that the for statement can iterate through the list, but can I use a for statement inside an argument for a function?

    It would look like this:
    Code:
    array_aux = array( for(....))
    I am pretty sure that this can't be done X_X I need to get the array in just once piece, and the only way of calling the function "array(arg0, arg1, ...)" is to call it from that switch statement, otherwise you would create lots of different arrays.

    Thanks for your reply! :D
     
    Last edited: Jan 11, 2019
  4. Jihl

    Jihl Member

    Joined:
    Nov 26, 2018
    Posts:
    21
    I was so mistaken, you were right, arrays can be made that way. This question was nonsense!

    I changed the code for this, in any case anyone was wondering
    Code:
    // Create auxiliar array
    var array_aux
      
    for (var i = 0; i < total_slots; i+=2)
    {
       // Add item to the array
       array_aux[i] = buffer_read(buffer, buffer_u16)
       // Add item quantity to the array
       array_aux[i+1] = buffer_read(buffer, buffer_u16)
    }
      
    with corpse scr_show_loot_ensured(total_slots, array_aux)
    
     
    Last edited: Jan 11, 2019
  5. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    148
    EDIT: Just saw last post I was partially correct.

    Are buffers necessary? Not that its bad. Are you trying to randomize the loot too?

    Go with maps to hold the loot name and value.

    Use a list to add and randomize by name. Pick one from the list. Now you can call the map key by that pick and get the value.
     
    Last edited: Jan 11, 2019
  6. Jihl

    Jihl Member

    Joined:
    Nov 26, 2018
    Posts:
    21
    Yes, buffers are neccesary. The function is called from the client project when you touch a corpse and the server's got the information. So I have to read the loot information and keep it updated in case another person grabs some of the loot (like in tibia)
     

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