简体   繁体   中英

Batch file error "/100 was unexpected at this time"

I make a game for fun with batch script but in this code i have an error message "/100 unexpected at this time" i really don't understand why? Please help me!!

@echo off
mode con cols=110 lines=32
setlocal enabledelayedexpansion

set npctier=0
goto randomnpc

:randomnpc
if %npctier% EQU 0 ( 
set npctype=Wooden Dummy
set /a npclvl=%random% %% 5+1
set /a npchp=%npclvl% * 100
set /a npcdmg=0
set /a npcdef=(%npchp%*5)/100
set /a npcxp=%npclvl%*100 )

:combatchoice
echo.
echo. You see %npctype% level %npclvl%.
echo.
echo. The %npctype%'s Health: %npchp% HP
echo.
goto main

I recommend to first open a command prompt , run set /? and read the output help carefully and completely from top of first to bottom of last page. There is explained:

Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them. If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.

So there can be written just set /A npchp=npclvl * 100 as npclvl inside the arithmetic expression is interpreted as name of an environment variable and the command line works even on being inside a command block starting with ( and ending with matching ) without usage of delayed environment variable expansion.

Then run cmd /? and read again the output help carefully and completely from top of first to bottom of last page. There is explained that a file name (or any other argument string) containing a space one of these characters &()[]{}^=;,'+,`~ must be enclosed in " to get those characters interpreted as literal characters of an argument string.

Please read also How does the Windows Command Interpreter (CMD.EXE) parse scripts?

For that reason the command line set /a npcdef=(%npchp%*5)/100 should be written with one of the following notations:

set /A npcdef=npchp*5/100
set /A npcdef=npchp * 5 / 100
set /A "npcdef=(npchp*5)/100"
set /A npcdef=(npchp*5^)/100

The caret character ^ escapes the next character for being interpreted as literal character except the next character is % which must be escaped with % .

The recommendations posted on DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ should be also taken into account on writing batch files which output empty lines.

The batch file with the main improvement of changing the IF condition to avoid completely the usage of a command block.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\mode.com con cols=110 lines=32

set npctier=0
set "npctype=Wooden Dummy"

:randomnpc
if %npctier% NEQ 0 goto CombatChoice
set /A npclvl=%random% %% 5 + 1
set /A npchp=npclvl*100
set npcdmg=0
set /A npcdef=npchp*5/100
set /A npcxp=npclvl*100

:CombatChoice
echo/
echo You see %npctype% level %npclvl%.
echo/
echo The %npctype%'s health: %npchp% HP
echo/

endlocal

See also Why is no string output with 'echo %var%' after using 'set var = text' on command line? It contains several hints on how to use command SET not written in help/documentation of this command.

If you wish to maintain the same structure, then something lke this would be better:

@Echo Off
SetLocal EnableExtensions
%__APPDIR__%mode.com 110, 32
Set "npctier=0"

:randomnpc
If %npctier% Equ 0 (
    Set "npctype=Wooden Dummy"
    Set "npcdmg=0"
    Set /A "npclvl=(%RANDOM% %% 5) + 1"
    Set /A "npchp=npclvl * 100, npcdef=npclvl * 5, npcxp=npchp"
)

:combatchoice
Echo=
Echo You see %npctype% level %npclvl%.
Echo=
Echo The %npctype%'s Health: %npchp% HP
Echo=
GoTo main

You should note, that it is possible to define multiple values using Set /A in one arithmetic instruction

Before you get too carried away with creating a lengthy script which repeats code, I think it's worthwhile introducing you to batch macro's.

Macro's in Batch are commands or command blocks assigned to variables, and through the use of For loops combined with If / Else conditioning can be used to Capture arguments allowing variables to be used as Functions.

The below example contains two variations of a macro that can be used to easily generate different encounter types with minimal repetative scripting whilst also avoiding the use of inefficient calls to functions.

Macro's must be defined prior to delayed expansion being enabled as variables referenced during macro execution are defined using ! expansion so that the value the macro is parsed with at expansion is the value of the variable at the time of parsing, not it's value during definition.

@echo off
mode con cols=110 lines=32 
(Set \n=^^^
%= macro newline DNR =%
)
(Set LF=^


%= Linefeed DNR =%)

rem Example 1: npc generation; Fixed Formula macro
rem USAGE: %npc{generic}%{Npc name / type}{npc max level}{npc multiplier}{damage value or formula}
Set npc{generic}=For %%n in (1 2)Do if %%n==2 (%\n%
 For /F "Tokens=1,2,3,4 Delims={}" %%G in ("!Params!")Do (%\n%
  Set "npctype=%%G" %\n%
  Set /a "npclvl= !random! %% %%H + 1" %\n%
  Set /a "npchp= !npclvl! * %%I" %\n%
  Set /a "npcdmg= %%J" %\n%
  Set /a "npcdef= ( !npchp! * %%H ) / %%I" %\n%
  Set /a "npcxp=!npclvl! * %%I" %\n%
  echo/Enemy: !npctype!!LF!level: !npclvl!!LF!HP: !npchp!!LF!Damage: !npcdmg!!LF!Defence: !npcdef!!LF!XP: !npcxp!!LF!%\n%
 )%\n%
) Else Set Params=

rem Example 2: npc generation; Supplied Formula macro
rem USAGE: %npc{boss}%{Npc name / type}{level value or formula}{hp value or formula}{damage value or formula}{defense value or formula}{xp value or formula}
Set npc{boss}=For %%n in (1 2)Do if %%n==2 (%\n%
 For /F "Tokens=1,2,3,4,5,6 Delims={}" %%G in ("!Params!")Do (%\n%
  Set "npctype=%%G" %\n%
  Set /a "npclvl= %%H " %\n%
  Set /a "npchp= %%I " %\n%
  Set /a "npcdmg= %%J " %\n%
  Set /a "npcdef= %%K " %\n%
  Set /a "npcxp= %%L " %\n%
  echo/Enemy: !npctype!!LF!level: !npclvl!!LF!HP: !npchp!!LF!Damage: !npcdmg!!LF!Defence: !npcdef!!LF!XP: !npcxp!!LF!%\n%
 )%\n%
) Else Set Params=
rem enable delayed expansion after macro definitions
setlocal enableextensions enabledelayedexpansion

:randomnpc
 %npc{generic}%{Wooden Dummy}{5}{100}{0}
 Pause
 %npc{boss}%{Dragon}{!random! %% 10 + 10}{npclvl * 200}{npchp / 20}{npchp / (npclvl / 2)}{npclvl * 150}
Endlocal
Goto :Eof

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM