Solved How do i make an inventory in a batch game?

January 14, 2016 at 16:58:47
Specs: Windows 7
ive been working on making a batch adventure game but im having trouble with making an inventory for it can anyone help? ive been having to write it out every time it goes to it is there a way i can have a premade inventory that updates as you put things in it?

See More: How do i make an inventory in a batch game?

January 14, 2016 at 20:59:19
✔ Best Answer
I can think of many ways to accomplish this, here's an example:

Let's say the the inventory contents are stored in a file, inventory.txt
Items are listed like so:

Random item
Idunno what this is

Here's an inventory system that can be used:

:: User: Bralvis1
:: Thread: How Do I Make An Inventory In A Batch Game
:: File: Inventory.bat
:: Date/time: 15012016/05:38:17
:: ----------SCRIPT START----------
@Echo off
setlocal enabledelayedexpansion
set itemIndex=1
:: List all items in inventory.txt and assign an index to them
for /f "tokens=*" %%A in (inventory.txt) do (
	Echo !itemIndex! -^> %%A
	set /a itemIndex=!itemIndex!+1
:: Get item from user
set /p item="Item: "
set itemIndexCounter=1
for /f "tokens=*" %%A in (inventory.txt) do (
	if !itemIndexCounter!==!item! (
		:: Call subroutine that handles the item (Passed as an argument), then break the loop once the subroutine is finished.
		call :HandleItem "%%A"
		goto inventoryMenu.break
	set /a itemIndexCounter=!itemIndexCounter!+1
goto :eof

:HandleItem <item>
:: Just to make sure the argument isn't empty
if "%~1"=="" goto :eof
:: You can choose what to do with the item here
Echo Item: %~1
goto :eof

:HandleItem.Remove <item>
:: This subroutine removes an item from the inventory
if "%~1"=="" goto :eof
findstr /v /l "%~1" inventory.txt>>_inventory.txt
del /q inventory.txt
ren _inventory.txt inventory.txt
goto :eof

:HandleItem.Add <item>
:: This subroutine adds an item to the inventory
echo %~1>>inventory.txt
goto :eof

You can copy the code into your game and whenever you want to enter the inventory you can use

call :inventory

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ

Report •
Related Solutions

Ask Question