Problem adding character age

Midnight Kitty

Midnight
Veteran
Joined
Jun 18, 2013
Messages
129
Reaction score
14
First Language
English
Primarily Uses
RMMV
The problem I have with the following code is it does not seem to save the number entered into the prompt box, does anybody know how I can fix this?
Code:
  ◆Script:ageS = $gameVariables.value(29);
  :      :var ageS = prompt("Enter a Value", "0");
  :      :if(isNaN(ageS)){
  :      :$gameMessage.setFaceImage('Actor1',0);
  :      :$gameMessage.setBackground(1);
  :      :$gameMessage.setPositionType(1);
  :      :$gameMessage.add("This isn't a number");
  :      :$gameVariables.setValue(35, 0);
  :      :}else{
  :      :var ageI = parseInt($gameVariables.value(29));
  :      :$gameVariables.setValue(35, ageI);
  :      :}
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,356
Reaction score
7,671
First Language
German
Primarily Uses
RMMV
why use a script for this? there is a number input event command....
 

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
604
Reaction score
1,421
First Language
English
Primarily Uses
RMMV
Prompt always returns a string, which means your if(isNaN(ageS)) will always be true. You need to move your parseInt function above your if statement and then use if(isNaN(ageI)).
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
First Language
Czech
Primarily Uses
RMMV
Well, I'm amazed you managed to put 12 lines of code in there, since the capacity of script field is only 10 lines of code.
However, in all seriousness, it looks like you are confused about javascript a bit.
So let me say what the code you did does:
1. defines new global variable ageS and sets its value to variable 29's value
2. defines new local variable ageS and sets its value to prompt input
3. checks if local variable ageS is NaN, which it will never be, because it is a string
4. ageI takes value from variable 29, which has been COMPLETELY UNCHANGED through the whole code
5. sets variable 35 equal to variable 29.

There are also two things. One of them is reference, the other one is assignment. It has to do with mutable (can be changed) and immutable things. Mutable get referenced, immutable get assigned.
For example a number is immutable. You cannot redefine 3. Strings are immutable. You can use methods like push etc., but in practice that will create a new string, it will not change the current one. Null and undefined are immutable. So if you set ageS equal to $gameVariables.value (29), if $gameVariables.value(29) is equal to a number, string, null, undefined or whatever immutable thing, it means that no matter how much you change ageS from now on, variable 29 will NOT be changed. And no matter how you change variable 29, ageS will not be changed either.

So hoping that ageS = $gameVariables.value(29) and then ageS = prompt (whatever) changes somehow value of variable 29 is kind of a waste of time... It will not.

If you want to save a prompt input to variable 29, you'd go at it like this:
$gameVariables.setValue (29, prompt("Enter a value", "0"));
No need for more than that.


Prompt always returns a string, which means your if(isNaN(ageS)) will always be true
No, that is wrong. NaN is a special value that signifies illegal operations regarding numbers. For example Number("ag6") returns NaN, because "ag6" is impossible to be converted to number. 6 * "3" returns NaN, because you can't multiply number with a string. And isNaN simply checks if the variable value is explicitly equal to NaN. In other words it is equivalent to var.typeOf (NaN).
 

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
604
Reaction score
1,421
First Language
English
Primarily Uses
RMMV
@Poryg whoops, you're right. That's what I get for answering questions before breakfast. :p
 

Midnight Kitty

Midnight
Veteran
Joined
Jun 18, 2013
Messages
129
Reaction score
14
First Language
English
Primarily Uses
RMMV
@Poryg I did pretty much what you said but now get an error saying Unexpected syntax ;
Code:
  ◆Script:$gameVariables.setValue(29, prompt("Enter a value", "0");
  :      :$gameVariables.setValue(35, $gameVariables.value(29));
Sorry I just noticed the massive glaring problem. No ) at the end
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
First Language
Czech
Primarily Uses
RMMV
Yup.
Well, with this code you have a pretty useless step there :D
So you can simplify it even further:
label: age
Script: $gameVariables.setValue (35, Number(prompt("Enter a value", "0")));
conditional branch - script - isNaN($gameVariables.value(35))
jump to label: age
 

Midnight Kitty

Midnight
Veteran
Joined
Jun 18, 2013
Messages
129
Reaction score
14
First Language
English
Primarily Uses
RMMV
that is a very helpful tip and yes it did work as you said.
I may implement that if not using the other variable to track age change.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:

Forum statistics

Threads
105,854
Messages
1,016,998
Members
137,562
Latest member
tamedeathman
Top