1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GM:S 1.4 [SOLVED] get_integer_async issue

Discussion in 'Programming' started by Alan Régis, Jan 11, 2017.

  1. Alan Régis

    Alan Régis Guest

    I'm building a custom dice roller in wich the number of sides of the dice is provided by user through a get_integer_assync function. Here's the code:

    Code:
    // In the Create Event
    
    show_roll = false ;
    dice_type = "" ;
    
    
    Code:
    // in the Mouse Left Press Event
    
    dice_question = get_integer_async("How many sides?","");
    
    
    Code:
    // In the Async Dialog Event
    
    dice_check = ds_map_find_value(async_load, "id");
    
    if dice_check == dice_question
    {
       if ds_map_find_value(async_load, "status")
          {
            if ds_map_find_value(async_load, "result") != ""
                 {
                    dice_type = ds_map_find_value(async_load, "result");
                    script_execute(dice_roll) ;
                 }
          
          }
    }
    
    
    This is the "dice_roll" script:
    Code:
    show_roll = true;
    randomize() ;
    dice_result = irandom_range(1,dice_type) ;
    
    
    And Finally:
    Code:
    // In the Draw Event
    
    draw_self() ;
    
    pos_vertical = obj_chatbox.y + 15  ;
    pos_horizontal = obj_chatbox.x + 25 ;
    
    if show_roll
    { 
        draw_set_font(font_dados) ;
        draw_text(pos_horizontal, pos_vertical, "Rolled a d" + string(dice_type) + " and got a " + string(dice_result)) ;
    }
    
    
    Now the issue: Everything works as intended except for the result of the dice, wich always returns 1. something like this:

    >(left click custom dice object)
    >Opens Dialog "How many sides?"
    >User insert a value, let's say 999
    >This text shows on screen: "You rolled a d999 and got a 1"

    Needless to say i tested it many times, so it's not a coincidence. It always returns 1.


    What am i doing wrong? Can someone please help me?
    (and sorry for the bad english.)
     
  2. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    I'm quite sure you should call randomize() at the start of your game only:
    What are your results if you get rid of randomize()? Do you keep getting 1 every time?
     
  3. Alan Régis

    Alan Régis Guest

    I dont think that randomize() is the issue here. I believe that as long as randomize() is called before any "random function" it should work well.

    The same "dice_roll script" is used for a regular dice object with a "dice_type" fixed value. So when i roll a regular d6 (dice_type = 6) it gives me random results as intended... The issue seems to be only with the custom dice.

    (But yeah, i tested it with randomize() at a GameStart event instead of script and the same issue occour)

    Maybe i'm doing something wrong in the Async event dialog? Can you spot something wrong there?

    And thank you for your assistance! :)
     
  4. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    That sounds like something goes wrong with the dice_type variable, however you draw it correctly afterwards. I mean, it shows what kind of dice was thrown so at the same time I'm not sure. Can you make sure the dice_type is set correctly before you throw the dice?
    Code:
    show_roll = true;
    randomize() ;
    show_message(dice_type); // Does this show you the correct value?
    dice_result = irandom_range(1,dice_type) ;
     
  5. Alan Régis

    Alan Régis Guest

    That's exactly what is confusing me. The dice_type is working, as we can see from the "Rolled a 999d (dice type) and got a 1"
    I have done the "show_message(dice_type)" verification and it does give me the right value, however, the dice_result variable still always returns 1. =/
     
  6. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    Very strange behaviour. Try setting dice_type like this, with real() to make sure GM understands that it's a number and doesn't pass it as a string into irandom_range(). Maybe the ds_map with the async values keeps it as a string instead of a number, and then messes up your irandom_range().
    Code:
    dice_type = real(ds_map_find_value(async_load, "result"));
    I also find it very strange that you initialize dice_type to be and empty string ("") when it is supposed to hold a number. Why isn't it set to a default of 1?
     
    Alan Régis likes this.
  7. Alan Régis

    Alan Régis Guest

    Thats it! You were right. the async_load result was returning a string instead of a number.

    dice_type = real(ds_map_find_value(async_load, "result")); solved the issue!

    Thank you very much, PWL!
     

Share This Page