msgbox with random number

Discussion in 'Javascript/Plugin Support' started by XGuarden, Jan 27, 2017.

  1. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French
    In one of my event, one NPC alway lie about how hold she is.


    So she alway give random number between 18 and 104.


    But I dont want to use variable for that.


    So i guess my only solution was to create messagebox by script right?


    I sean this command:


    $gameMessage.setFaceImage('Actor1',0)
    $gameMessage.setBackground(1)
    $gameMessage.setPositionType(1)
    $gameMessage.add("Show Text Script Call")



    So I guess I will be able to use that, but for random number inside this, im not sure.


    Thanks for your help.
     
    #1
  2. Andar

    Andar Veteran Veteran

    Messages:
    28,332
    Likes Received:
    6,444
    Location:
    Germany
    First Language:
    German
    Primarily Uses:
    RMMV


    Why not?


    That is the easiest way, and exactly what that command is intended for.
     
    #2
  3. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French
    Because My projet is pretty big and I will ran out of variable really fast if I use it for stuff like that.


    I think I almost got it right:

    $gameMessage.setFaceImage('Face_Child_Suzy',0)


    $gameMessage.setBackground(0)


    $gameMessage.setPositionType(2)


    $gameMessage.add("I'm" + rand(3)+1 + "Year old")
     
    #3
  4. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,476
    Likes Received:
    2,868
    Location:
    Philippines
    First Language:
    Tagalog
    If that does not work, use this for the number part


    Math.floor(Math.random()*(104-18+1))+18
     
    Last edited by a moderator: Jan 27, 2017
    #4
  5. Andar

    Andar Veteran Veteran

    Messages:
    28,332
    Likes Received:
    6,444
    Location:
    Germany
    First Language:
    German
    Primarily Uses:
    RMMV
    Then you're using variables in the wrong way - you don't need to store the number if it is different every time.


    So what you should do in any larger project is to reserve about ten "temporariy" variables for cases where you only need the numbers for calculations inside the specific event or sequence.


    You use one of those temp variables to get a random number for the age and use that in the show text, forgetting about it afterwards.


    And in another event, the temp variable can be used for something else.


    But it should be named for temporary or something like that to remind you that you cannot store anything there, because you will be overriding the old values any time you need a temporary random number.
     
    #5
  6. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French
    Thanks andar, I use that some time. But it's also for learn javascript in same time and for a personnal chalenge heheheh.


    But curently with variabl or without, that did'T work anyway.


    This line:


    $gameMessage.add(Math.floor(Math.random()*(104-18+1))+18);


    Give me undefined is not a function. I guess Math did't exist in rpg maker lol
     
    #6
  7. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,476
    Likes Received:
    2,868
    Location:
    Philippines
    First Language:
    Tagalog
    Math exists for MV, I've seen people use it in threads here... I've also used it for some things.


    can you try this


    $gameMessage.add(104-18+1)


    and see if it errors or displays the message correctly?
     
    Last edited by a moderator: Jan 27, 2017
    #7
  8. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French
    That give 96 on first line and undefined on next line
     
    #8
  9. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French
    That give 96 on first line and undefined on next line


    forget to quote you first time
     
    #9
  10. Shaz

    Shaz Veteran Veteran

    Messages:
    37,572
    Likes Received:
    11,273
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Why are you so worried about running out of variables?  You don't have to KEEP a variable for a single purpose unless you need to.  


    Have a few "rubbish" variables specifically for short-term use.  Use the same ones over and over again.


    In this case, your variable is going to be set, then used, within the same event, and you don't care what happens to it after that.  It can be used for an entirely different purpose by a hundred other events, if all they want to do is set it, then use it.


    Your girl who lies about her age - set the variable to a random number, then use that variable in the dialogue.


    A merchant who wants to sell you something - set the variable to the amount of gold, then use it in a conditional branch to see if you can afford the item.


    A man who's given you a quest to collect 10 of some item - set the variable to the number of that item in inventory, then use it in a conditional branch to see if you've got the number he requested.


    A door that uses a numeric password - use a variable with Number Input, then use it in a conditional branch to see if they've entered the right number.


    A puzzle that wants you to select a key item to solve it - use a variable with Select Item, then use it in a conditional branch to see if they chose the right item.


    Every one of these events can use the exact same variable.  You don't need to allocate a separate variable for each one, when it's going to be set and used in the same page of the same event, and then forgotten about for the rest of the game.


    Put more thought into the way you design things and make use of your database, and you won't have any problems like that anymore.
     
    #10
    BrandedTales likes this.
  11. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French


    I try to do it without variable also for try to understand what can be done and not. But Finally, I will have ni choice to use a variable.


    But still, did't get how map random work, still working on it. Dont worry, I never user a diffrent variable for these kind of thing, reuse same one.


    The reason Im afraid of running out of variable it's because it's a real big projet(140 hour to finish if w know what to do). Im afraid of running out of switch too because it's a puzzle based game an for keep a track of a puzzle already done, I need to use switch, that will use more then 2000 switch just that.
     
    #11
  12. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,476
    Likes Received:
    2,868
    Location:
    Philippines
    First Language:
    Tagalog


    What's the next line?


    Can you also try


    $gameMessage.add(Math.random());


    then


    $gameMessage.add(Math.floor(Math.random()));


    So that we can see which one fails? Though I used Math.random() before in an event so I don't think it should be causing errors.
     
    Last edited by a moderator: Jan 29, 2017
    #12
  13. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French
    Automaticly give  error: undefined is not a function.

    undefined.png
     
    Last edited by a moderator: Jan 29, 2017
    #13
  14. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,476
    Likes Received:
    2,868
    Location:
    Philippines
    First Language:
    Tagalog
    That's weird... a search of Math.random() here in the forums yields so many threads where Math.random() is suggested so that function itself should actually be fine..
     
    #14
  15. Nicke

    Nicke Fortune seeker Veteran

    Messages:
    529
    Likes Received:
    500
    Location:
    Sweden
    First Language:
    Swedish
    Primarily Uses:
    RMMV
    Here you go:

    Code:
    var i = Math.floor(Math.random()*(104-18+1)+18);
    $gameMessage.add(String(i));
     
    #15
    XGuarden likes this.
  16. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French


    It's work, any explanation why w can't us it directly?


    ANd how Can I add text before and after String(i)?


    Also why 104-18+1? Why not put directly 96?


    If I want to put it in existing var number 144 by script? How I do?
     
    #16
  17. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,476
    Likes Received:
    2,868
    Location:
    Philippines
    First Language:
    Tagalog


    Because you said you wanted it from 104 to 18, so we showed you how to do it in a more teaching way... Putting there just 96 is not educational right? Also 104-18+1 is 87, not 96... 


    Or I guess a more educational approach is


    var difference = 104 - 18


    $gameMessage.add(String(Math.floor(Math.random()*(difference+1)+18))));


    I guess the problem was the fact that the result of Math.floor and Math.random isn't a string, and somehow the engine doesn't want to auto convert them so you need to convert it to a string manually. Which is kinda confusing since your test with $gameMessage.add(104-18+1)  resulted to it showing 96, when 104-18+1 also basically returns a number too


    A more general approach if you want it to be based on variables will be


    var difference = $gameVariables.value(ID of higher limit) - $gameVariables.value(ID of lower limit)


    $gameMessage.add(String(Math.floor(Math.random()*(difference+1)+ $gameVariables.value(ID of lower limit)))));
     
    Last edited by a moderator: Jan 29, 2017
    #17
  18. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French


    You right that work (with on ) less) lol.


    How can I add text before and after?
     
    #18
  19. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,476
    Likes Received:
    2,868
    Location:
    Philippines
    First Language:
    Tagalog


    By using the + operator


    var difference = $gameVariables.value(ID of higher limit) - $gameVariables.value(ID of lower limit)


    $gameMessage.add(TEXT_BEFORE + String(Math.floor(Math.random()*(difference+1)+ $gameVariables.value(ID of lower limit))) + TEXT_AFTER);
     
    Last edited by a moderator: Jan 29, 2017
    #19
    XGuarden likes this.
  20. XGuarden

    XGuarden Veteran Veteran

    Messages:
    419
    Likes Received:
    12
    First Language:
    French


    I tryed with & and it's work too, same?


    Only qustion left, if I want to add value to variabl 144, how I do? Already try the 
    $gameVariables.setValue(var, value);  and it's did'T work.


    But I will make some test, maybe I need to use the string trick too.
     
    #20

Share This Page