1
\$\begingroup\$

I am developping a little game with GameMaker Studio.

I am sorry if this is a silly question, but I keep getting NaN as a result of this equation :

    Team_Joueur.PVsRandom[Setup_Battle.PokemonAleatoire] -= (( 2 * Team_Joueur.Niveau[0] + 10) / 250 * (Team_Joueur.Attaque[0] *
(ModificateursSimples(Team_Joueur.Modificateurs[0, 0]) / 100)) / (Team_Joueur.DefenseRandom[Setup_Battle.PokemonAleatoire] *
(ModificateursSimples(Team_Joueur.ModificateursRandom[Setup_Battle.PokemonAleatoire, 1]) / 100)) * Team_Joueur.Puissance[0, Position] + 2)
* (STAB * Coup_Critique * round(random_range(0.85, 1.01)));

Here is the original equation : The basic equation

And here is the equation with simpler variables' name :

HP[Pokemon] -= (( 2 * Level[0] + 10) / 250 * (Attack[0] *
(Modif(Modificators[0, 0]) / 100)) / DefenceOpponent[Opponent] *
(Modif(ModificatorsOpponent[Opponent, 1]) / 100)) * Power[0, Attack] + 2)
* (STAB * Critical * round(random_range(0.85, 1.01)));

I tried each argument and they are all numbers, not very large, and drawing the equation gave me the result...

draw_text(32, 224, string(Team_Joueur.PVsRandom[Setup_Battle.PokemonAleatoire] - (( 2 * Team_Joueur.Niveau[0] + 10) / 250 * (Team_Joueur.Attaque[0] *
(ModificateursSimples(Team_Joueur.Modificateurs[0, 0]) / 100)) / (Team_Joueur.DefenseRandom[Setup_Battle.PokemonAleatoire] *
(ModificateursSimples(Team_Joueur.ModificateursRandom[Setup_Battle.PokemonAleatoire, 1]) / 100)) * Team_Joueur.Puissance[0, 0] + 2)));

PS : The 'Modif' function returns a number based on another one.

I am deeply sorry for my code, I know it's messy.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ It might be easier if you break up your larger equation into a few smaller pieces and store them in temporary variables. For example, store (2 * level[0] +10) / 250 as its own variable, attack / defense as another, etc. Then run the full equation. Throwing everything into one equation on one line can make debugging problems like these very difficult. Chances are good that if you break it up you'll be able to narrow down which part of the equation is causing your error. \$\endgroup\$
    – Honeybunch
    Commented Jan 13, 2017 at 0:05
  • 1
    \$\begingroup\$ Thank you ! I got the wrong part, but can't understand why it is wrong :/ Team_Joueur.DefenseRandom[Setup_Battle.PokemonAleatoire] * (ModificateursSimples(Team_Joueur.ModificateursRandom[Setup_Battle.PokemonAleatoire, 1]) / 100)) It's the part that divides the attack \$\endgroup\$
    – Natos
    Commented Jan 13, 2017 at 0:29
  • 1
    \$\begingroup\$ Try breaking it up even further. Is it the multiplication? The division? Maybe check that the values that you're getting out of those arrays are what you expect them to be. Not sure if Game Maker has a debugger that you can use to step through the code. If not you can always just print out the values. \$\endgroup\$
    – Honeybunch
    Commented Jan 13, 2017 at 0:33
  • 1
    \$\begingroup\$ I tried few things, and here is what I found : ModificateursSimples(Team_Joueur.ModificateursRandom[Setup_Battle.PokemonAleatoire, 1]) / 100; is making the NaN. But ! When I print it, it's a number, and the correct one. \$\endgroup\$
    – Natos
    Commented Jan 13, 2017 at 0:42
  • 1
    \$\begingroup\$ BTW, round(random_range(0.85, 1.01)) always will return 1 \$\endgroup\$
    – Dmi7ry
    Commented Jan 13, 2017 at 4:38

1 Answer 1

1
\$\begingroup\$

Ok, the problem was in my function ModificateursSimples : I was returning a value in a switch, thus never reading the line break;, and when coming back to that function, the switch was never clearly closed before, thus buging.

The solution is to make a temp variable, changing in the switch, and returning it only AFTER the switch, once it is close.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nice find! That's not very intuitive as I believe many languages like C++, Java etc. wouldn't have a problem returning from a switch statement. \$\endgroup\$
    – Honeybunch
    Commented Jan 13, 2017 at 1:21
  • 1
    \$\begingroup\$ Yes, I usually code in C# and C and this problem never came in... It's seems to be a particularity of the GML. \$\endgroup\$
    – Natos
    Commented Jan 13, 2017 at 1:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .