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:I feel as though your judging me. :(
Of course, who doesn't judge others?
:/ as in, in a bad way.
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::/ as in, in a bad way.
You do keep saying sorry, as if you have done something bad. Maybe it's all in your head.
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

So I read through the Programming in Lua (the tables bit) and watch SockMunkee's tutorials. I have attempted at making an Inventory and it kinda failed. I've done all you asked. Any help now please? ;) And now my feelings of reality are slipping, cheers Azhukar

Cheers,
Pigeon
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:Any help now please? ;)
You were already given an example inventory code.
Pigeon wrote:And now my feelings of reality are slipping, cheers Azhukar
One step further out of your bubble. :)
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:Any help now please? ;)
You were already given an example inventory code.
Pigeon wrote:And now my feelings of reality are slipping, cheers Azhukar
One step further out of your bubble. :)
Fine, sure. Thanks. I guess.

Code: Select all

function time:wasted(?)
    if time == "wasted" then
    	log_off_lua = true
    	curse_timewasters_name = true
    	pissed = true
    end
end
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:Fine, sure. Thanks. I guess.

Code: Select all

function time:wasted(?)
    if time == "wasted" then
    	log_off_lua = true
    	curse_timewasters_name = true
    	pissed = true
    end
end
Syntax error: ?:1: <name> or '...' expected near '?'
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:Fine, sure. Thanks. I guess.

Code: Select all

function time:wasted(?)
    if time == "wasted" then
    	log_off_lua = true
    	curse_timewasters_name = true
    	pissed = true
    end
end
Syntax error: ?:1: <name> or '...' expected near '?'
The question mark was for show, not for real code.

Code: Select all

function time:wastedevenmore()
	if time == "wastedevenmore" then
		thoughts = "f*ck off, all I asked for was a bit of help"
	end
end
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:The question mark was for show, not for real code.

Code: Select all

function time:wastedevenmore()
	if time == "wastedevenmore" then
		thoughts = "f*ck off, all I asked for was a bit of help"
	end
end
Is there something you do not understand about the inventory code? Like say, tables? D:
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:The question mark was for show, not for real code.

Code: Select all

function time:wastedevenmore()
	if time == "wastedevenmore" then
		thoughts = "f*ck off, all I asked for was a bit of help"
	end
end
Is there something you do not understand about the inventory code? Like say, tables? D:
almost everything in the code... + sorry for being rude
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: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
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 226 guests