0

I ran into a bug in a game I'm making and it is caused by this command line:

set mdmg=%rdm% %%5 +1

The result is always 50 but it is meant to be 0 through 5. Rdm is %random% by the way.

Why is this happening?

Here is the block that it is run in:

REM////////////////BATTLE\\\\\\\\\\\\\\\\\\

:prebattle
setlocal enabledelayedexpansion
set rdm=%random%

if "!weapon!" EQU "1" (
set /a pdmg=!rdm! %%25 +1
)

if "!armor!" EQU "1" (
set /a php=100
)

set mhp=50
set mdmg=!rdm! %%5 +1

set /p %c%=user

if %c% EQU 1 (
echo !mhp!
)
if %c% EQU 2 (
echo !mdmg!
)
if %c% EQU 3 (
echo !mlvl!
)
pause

if "!lvl!" EQU "1" if "!lvl!" LEQ "5" (
set /a enemyno=!rdm! %%3 +1
set /a mhp+=!rdm! %%20 +1
set /a mdmg+=!rdm! %%20 +1
set /a mlvl=!rdm! %%5 +1
)

set /p %c%=user

if %c% EQU 1 (
echo !mhp!
)
if %c% EQU 2 (
echo !mdmg!
)
if %c% EQU 3 (
echo !mlvl!
)
pause

if "!lvl!" GEQ "6" if "!lvl!" LEQ "10" (
set /a enemyno=!rdm! %%6 +1
set /a mhp+=!rdm! %%20 +1
set /a mdmg+=!rdm! %%15 +1
set /a mlvl=!rdm! %%10 +1
)
if "!lvl!" GEQ "11" if "!lvl!" LEQ "15" (
set /a enemyno=!rdm! %%9 +1
set /a mhp+=!rdm! %%20 +1
set /a mdmg+=!rdm! %%15 +1
set /a mlvl=!rdm! %%16 +1
)
if "!lvl!" GEQ "16" if "!lvl!" LEQ "20" (
set /a enemyno=!rdm! %%12 +1
set /a mhp+=!rdm! %%20 +1
set /a mdmg+=!rdm! %%15 +1
set /a mlvl=!rdm! %%22 +1
)
if "!lvl!" GEQ "21" if "!lvl!" LEQ "25" (
set /a enemyno=!rdm! %%15 +1
set /a mhp+=!rdm! %%20 +1
set /a mdmg+=!rdm! %%15 +1
set /a mlvl=!rdm! %%30 +1
)
if "!lvl!" GEQ "26" if "!lvl!" LEQ "30" (
set /a enemyno=!rdm! %%18 +1
set /a mhp+=!rdm! %%20 +1
set /a mdmg+=!rdm! %%15 +1
set /a mlvl=!rdm! %%35 +1
)

set /p %c%=user

if %c% EQU 1 (
echo !mhp!
)
if %c% EQU 2 (
echo !mdmg!
)
if %c% EQU 3 (
echo !mlvl!
)
pause


if "!enemyno!" EQU "1" goto enemy1
if "!enemyno!" EQU "2" goto enemy2
if "!enemyno!" EQU "3" goto enemy3
if "!enemyno!" EQU "4" goto enemy4
if "!enemyno!" EQU "5" goto enemy5
if "!enemyno!" EQU "6" goto enemy6
if "!enemyno!" EQU "7" goto enemy7
if "!enemyno!" EQU "8" goto enemy8
if "!enemyno!" EQU "9" goto enemy9
if "!enemyno!" EQU "10" goto enemy10
if "!enemyno!" EQU "11" goto enemy11
if "!enemyno!" EQU "12" goto enemy12
if "!enemyno!" EQU "13" goto enemy13
if "!enemyno!" EQU "14" goto enemy14
if "!enemyno!" EQU "15" goto enemy15
if "!enemyno!" EQU "16" goto enemy16
if "!enemyno!" EQU "17" goto enemy17
if "!enemyno!" EQU "18" goto enemy18
if "!enemyno!" EQU "19" goto enemy19
if "!enemyno!" EQU "20" goto enemy20
if "!enemyno!" EQU "21" goto enemy21
if "!enemyno!" EQU "22" goto enemy22
if "!enemyno!" EQU "23" goto enemy23
if "!enemyno!" EQU "24" goto enemy24
if "!enemyno!" EQU "25" goto enemy25
if "!enemyno!" EQU "26" goto enemy26
if "!enemyno!" EQU "27" goto enemy27
if "!enemyno!" EQU "28" goto enemy28
if "!enemyno!" EQU "29" goto enemy29
if "!enemyno!" EQU "30" goto enemy30

goto prebattle
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 3
    Ordinarily, I would ask to see more of your code because it's impossible to determine what's wrong without seeing more of it. That said, I can practically guarantee that it's a result of you [not using delayed expansion](http://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set). I am, however, voting to close because of your lack of [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). – SomethingDark Nov 20 '16 at 20:43
  • I have setlocal enabledelayedexpansion on –  Nov 20 '16 at 20:45
  • Yeah, and you need to use `!rdm!` instead of `%rdm%`. Try actually reading the link. – SomethingDark Nov 20 '16 at 20:45
  • Really? Thanks man –  Nov 20 '16 at 20:46
  • But why would it make it result in that just by using the !! Instead of %% –  Nov 20 '16 at 20:48
  • http://stackoverflow.com/a/9681923/4158862 Read. The. Answer. – SomethingDark Nov 20 '16 at 20:48
  • Nevermind im still getting the same thing –  Nov 20 '16 at 20:49
  • 1
    In that case, please [edit your question](http://stackoverflow.com/posts/40709140/edit) to include enough code that I can paste it into my own Notepad, run it, and experience the same problem. Right now, the one line of code that you posted works perfectly for me because you have more code that is impacting what you've shown. – SomethingDark Nov 20 '16 at 20:51
  • I made the edit :) –  Nov 20 '16 at 20:55
  • 3
    Your code doesn't even work; `set /p %c%=user` doesn't set the `%c%` variable, it sets the variable whose name is the value of `%c%`, which you then never use. But once I adjusted your code, it ran fine; I kept getting random numbers between 1 and 5 (mostly 2) when I pressed 2. You are pressing 2 and not 1, right? 1 will definitely display 50. – SomethingDark Nov 20 '16 at 21:09
  • You are missing the `/A` option. – Squashman Nov 20 '16 at 21:12
  • I'm pressing 2 and it ran the same with /a , squashman –  Nov 20 '16 at 21:22
  • il give it a go tho –  Nov 20 '16 at 21:22
  • 3
    I told you this in your other questions. To do math YOU HAVE TO USE THE /A option. This is incorrect. `set mdmg=!rdm! %%5 +1` – Squashman Nov 20 '16 at 21:24
  • @Yoshirou, please tag people correctly so that they get notified that you responded to one of their comments. – Squashman Nov 20 '16 at 21:25
  • @Yoshirou, I see you did not take my advice in your previous question on getting rid of the 30 `IF GOTO` lines of code. You still seem to be thoroughly confused on how to use Delayed Expansion. Again, you are basically using delayed expansion when you do not have to. – Squashman Nov 20 '16 at 21:28
  • [How to do math in batch-file](http://stackoverflow.com/q/19989931/995714) – phuclv Nov 21 '16 at 02:53
  • @Squashman and SomthingDark Thank you guys so much, its working fine now, but the next error appeared but I'll try work this one out myself, thanks so much –  Nov 21 '16 at 20:18

1 Answers1

2

I am just putting this code out here for you so that maybe you will finally understand what is wrong with your code. There are still more input validity checks you can put in there. I am not really understanding what you are doing with the USER variable. The code I corrected makes the assumption that the code before and after this code block is correct.

REM////////////////BATTLE\\\\\\\\\\\\\\\\\\    

:prebattle
set rdm=%random%    

if "%weapon%" EQU "1" set /a pdmg=rdm %% 25 +1    

if "%armor%" EQU "1" set /a php=100


set mhp=50
set /a mdmg=rdm %% 5 +1

set "c="
set /p c=user

if %c% EQU 1 echo %mhp%
if %c% EQU 2 echo %mdmg%
if %c% EQU 3 echo %mlvl% 

pause

if "%lvl%" EQU "1" if "%lvl%" LEQ "5" (
    set /a enemyno=rdm %% 3 +1
    set /a mhp+=rdm %% 20 +1
    set /a mdmg+=rdm %% 20 +1
    set /a mlvl=rdm %% 5 +1
)

set "c="
set /p c=user

if %c% EQU 1 echo %mhp%
if %c% EQU 2 echo %mdmg%
if %c% EQU 3 echo %mlvl% 

pause

if "%lvl%" GEQ "6" if "%lvl%" LEQ "10" (
    set /a enemyno=rdm %% 6 +1
    set /a mhp+=rdm %% 20 +1
    set /a mdmg+=rdm %% 15 +1
    set /a mlvl=rdm %% 10 +1
)

if "%lvl%" GEQ "11" if "%lvl%" LEQ "15" (
    set /a enemyno=rdm %% 9 +1
    set /a mhp+=rdm %% 20 +1
    set /a mdmg+=rdm %% 15 +1
    set /a mlvl=rdm %% 16 +1
)

if "%lvl%" GEQ "16" if "%lvl%" LEQ "20" (
    set /a enemyno=rdm %% 12 +1
    set /a mhp+=rdm %% 20 +1
    set /a mdmg+=rdm %% 15 +1
    set /a mlvl=rdm %% 22 +1
)

if "%lvl%" GEQ "21" if "%lvl%" LEQ "25" (
    set /a enemyno=rdm %% 15 +1
    set /a mhp+=rdm %% 20 +1
    set /a mdmg+=rdm %% 15 +1
    set /a mlvl=rdm %% 30 +1
)

if "%lvl%" GEQ "26" if "%lvl%" LEQ "30" (
    set /a enemyno=rdm %% 18 +1
    set /a mhp+=rdm %% 20 +1
    set /a mdmg+=rdm %% 15 +1
    set /a mlvl=rdm %% 35 +1
)

set "c="
set /p c=user

if %c% EQU 1 echo %mhp%
if %c% EQU 2 echo %mdmg%
if %c% EQU 3 echo %mlvl% 
pause

REM SANITY CHECK to make sure the variable is defined and within range.
REM IF TRUE then goto enemy#
IF DEFINED enemyno (
    if %enemyno% GEQ 1 (
        if %enemyno% LEQ 30 goto enemy%enemyno% 
    )
)


goto prebattle
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • I don't see any `USER` variable in the OP's code. On the other hand, there is never defined `lvl` variable but even though defined `if "!lvl!" EQU "1" if "!lvl!" LEQ "5"` is equivalent to `if "!lvl!" EQU "1"` and `if "%lvl%" GEQ "6" if "%lvl%" LEQ "10"` **never** evaluates to _true_ (string vs. numeric comparison). – JosefZ Nov 21 '16 at 01:38
  • @JosefZ, he is only showing us a small block of his code. We can only assume he has all other variables defined before this code block. Hence my comments before my code. – Squashman Nov 21 '16 at 01:46
  • 1
    Then, the OQ does not satisfy [mcve] rules: it's at once too huge and incomplete therefore not verifiable… – JosefZ Nov 21 '16 at 02:15
  • @Squashman Thanks for the help and the user thing was just so I could debug it to check what the script was outputing –  Nov 21 '16 at 19:49