msgbox with random number

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.
But I dont want to use variable for that.



Why not?


That is the easiest way, and exactly what that command is intended for.
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:

Spoiler
$gameMessage.setFaceImage('Face_Child_Suzy',0)


$gameMessage.setBackground(0)


$gameMessage.setPositionType(2)


$gameMessage.add("I'm" + rand(3)+1 + "Year old")
If that does not work, use this for the number part


Math.floor(Math.random()*(104-18+1))+18
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.
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
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?
That give 96 on first line and undefined on next line
$gameMessage.add(104-18+1)

That give 96 on first line and undefined on next line


forget to quote you first time
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.
Your girl who lies about her age - set the variable to a random number, then use that variable in the dialogue.



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.
That give 96 on first line and undefined on next line


forget to quote you first time



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.
$gameMessage.add(Math.random());

Automaticly give  error: undefined is not a function.

undefined.png
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..
Here you go:

Code:
var i = Math.floor(Math.random()*(104-18+1)+18);
$gameMessage.add(String(i));
Here you go:



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



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?
Also why 104-18+1? Why not put directly 96?



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)))));
$gameMessage.add(String(Math.floor(Math.random()*(104-18+1)+18))));



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


How can I add text before and after?
How can I add text before and after?



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);
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);



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.

Original thread: https://forums.rpgmakerweb.com/threads/msgbox-with-random-number.74152/