Inventory help in an RPG game

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

Azhukar wrote:
Pigeon wrote:almost everything in the code... + sorry for being rude

Code: Select all

local function addItemToInventory(inventory,item,amount)
	if (inventory[item] ~= nil) then  --there are items like this in the inventory already
		inventory[item] = inventory[item] + amount --so we add the amount to the ones already there
	else --there is no item like this in the inventory
		inventory[item] = amount --so the new amount of that item is the one we're adding
	end
end

local function removeItemFromInventory(inventory,item,amount)
	if (inventory[item] == nil) then return end --there are no such items in the inventory, we do nothing
	
	if (inventory[item] <= amount) then --we're removing more of such items than there are in the inventory
		inventory[item] = nil --so we will have no items like it afterwards
	else --there is more of such items than we are removing
		inventory[item] = inventory[item] - amount --so we subtract the amount we're removing
	end
end

local function printItemsInInventory(inventory)
	for item,amount in pairs(inventory) do --we're traversing the table, table keys are items, table values are amounts of items
		if (type(item)=="table") then --complicated items are represented by a table
			print(item.name or "Unknown",amount) --if item has a name we use that, otherwise we use "Unknown" for printing
		else --it's a simple item represented by a string
			print(item,amount) --we print the item and how much there is of it
		end
	end
end

local myInventory = {} --our new inventory is a simple table

local myItem = "hammer" --simple item will be a string
local myUniqueItem = {name="superhammer",bonus=5} --special item will be a table with anything we like in it

addItemToInventory(myInventory,myItem,10) --we add 10 hammers to inventory
addItemToInventory(myInventory,myUniqueItem,1) --we add 1 superhammer to inventory
removeItemFromInventory(myInventory,myItem,4) --we remove 4 hammers from inventory

printItemsInInventory(myInventory) --we print contents of the inventory


Thank you so much for that. I truly mean it. However it doesn't seem to work, even when I copy and paste it into the main.lua. I'm such a d*ck at times. That's why I need Love ;)
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Inventory help in an RPG game

Post by Azhukar »

Pigeon wrote:I truly mean it. However it doesn't seem to work, even when I copy and paste it into the main.lua.
The code works, you're doing something wrong.
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Inventory help in an RPG game

Post by jjmafiae »

you can't just copy and use a code you don't understand go out and learn some lua and then write a code yourself thats how you program for real!
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

jjmafiae wrote:you can't just copy and use a code you don't understand go out and learn some lua and then write a code yourself thats how you program for real!
I said EVEN when I copied and pasted it. I tried typing it in first. Read first, Judge later.
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Inventory help in an RPG game

Post by jjmafiae »

but if it was a code you made yourself you would know how to fix it, you can't ask for help in all your game modules, im just saying!
Last edited by jjmafiae on Mon Oct 21, 2013 2:49 pm, edited 1 time in total.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Inventory help in an RPG game

Post by Davidobot »

Pigeon wrote:
jjmafiae wrote:you can't just copy and use a code you don't understand go out and learn some lua and then write a code yourself thats how you program for real!
I said EVEN when I copied and pasted it. I tried typing it in first. Read first, Judge later.
Calm down everybody. The inventory code will print into the console, make sure you have that enabled to see what the code prints.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

Davidobot wrote:
Pigeon wrote:
jjmafiae wrote:you can't just copy and use a code you don't understand go out and learn some lua and then write a code yourself thats how you program for real!
I said EVEN when I copied and pasted it. I tried typing it in first. Read first, Judge later.
Calm down everybody. The inventory code will print into the console, make sure you have that enabled to see what the code prints.
I have, but it just won't say anything. Where exactly do I load it? (and I hope to make an inventory with pictures, not words)
jjmafiae wrote:but if it was a code you made yourself you would know how to fix it, you can't ask for help in all your game modules, im just saying!
I understand what your saying, it's just that I feel you've been slightly (mean?) towards me, but I fully understand what you meant.
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Inventory help in an RPG game

Post by Azhukar »

Pigeon wrote:I have, but it just won't say anything. Where exactly do I load it? (and I hope to make an inventory with pictures, not words)
Learn what every line of the code does and why it's there. Try drawing a few pictures in a grid on your own and work from there. It will only get harder if you don't understand the basics.

I won't code entire game features for you. What I did provide you with is an example to get you started.
Pigeon wrote:I understand what your saying, it's just that I feel you've been slightly (mean?) towards me, but I fully understand what you meant.
Here's the deal, you openly said "fuck off" to me yet you yourself don't like it when there is even a slight hint of something mean being said to you. Do you see me making a deal out of it? Don't take everything so seriously and personally. Stop looking for things to be insulted over.

We're trying to help you with your problem. Saying sorry repeatedly does not matter in the slightest if you're just going to start arguing with someone the next post you make.
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

Azhukar wrote:
Pigeon wrote:I have, but it just won't say anything. Where exactly do I load it? (and I hope to make an inventory with pictures, not words)
Learn what every line of the code does and why it's there. Try drawing a few pictures in a grid on your own and work from there. It will only get harder if you don't understand the basics.

I won't code entire game features for you. What I did provide you with is an example to get you started.
Pigeon wrote:I understand what your saying, it's just that I feel you've been slightly (mean?) towards me, but I fully understand what you meant.
Here's the deal, you openly said "fuck off" to me yet you yourself don't like it when there is even a slight hint of something mean being said to you. Do you see me making a deal out of it? Don't take everything so seriously and personally. Stop looking for things to be insulted over.

We're trying to help you with your problem. Saying sorry repeatedly does not matter in the slightest if you're just going to start arguing with someone the next post you make.
I can't help the issues I have with my head. That's why when you said 'it might be all in your head' it really hurt me basically, because I have a severe anger disorder. So yeah. I am sorry, but I might not be in the next post, then I will be in the next. It is in my head, but I can't help it. Just don't openly abuse people anymore. It is probably just me who gives two shits. Now I'm sorry but I don't understand the code, because it has yet to have worked.
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

Basically, where am I gonna put all of that code. That's what I want to know. Enough with me de-railing a thread I made.

Where do I put all of ze code?
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 217 guests