Inventory and equipment inventory help

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.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Inventory and equipment inventory help

Post by coke905 »

Adventually I would like to make an mmorpg or just an rpg game. I honestly have no clue how to make an inventory or equipment inventory atm i dont really care about the equipment inventory just getting the system to work is what i would like. In C++ you would use arrays so would i use a table.
Im confused can someone please give a detailed answer.

Thanks its appriciated ;)
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Inventory and equipment inventory help

Post by Inny »

The concept of an Inventory is very high level and difficult to explain, so it's necessary to break it down into a few parts:

First is the actual storage of the data that represents the inventory. If it's just a straight list of items, then this is trivial in Lua: use a table with integer keys. If you want to be a bit more OOP about it, use Lua's sugar syntax.

Code: Select all

inventory = {}
function inventory:get( index )
  return self[index]
end
function inventory:set(index, value)
  self[index] = value
end
function inventory:swap( first, second )
  self[first], self[second] = self[second], self[first]
end
MAX_INVENTORY = 20

inventory:set( 1, "Potion" )
inventory:set( 2, "Tent" )
Of course, that code is a bit silly, it's just for demonstration purposes.

Second, you need a way to view that inventory. That's where it can get a bit complex, because you need to draw those items in some capacity. I can give another silly example:

Code: Select all

item_highlighted = 1
function love.draw()
  love.graphics.setColor( 255, 255, 255 )
  for i = 1, MAX_INVENTORY do
    local item = inventory:get(i)
    if type(item)=="string" then
      love.graphics.print( item, 20, 10*(i-1) )
    end
  end
  if item_selected then
    love.graphics.rect( "fill", 8, 10*(item_selected-1), 10, 10 )
    love.graphics.setColor( 255, 255, 0 )
  end
  love.graphics.rect( "fill", 8, 10*(item_highlighted-1), 10, 10 )
end
Third, you need a way to do something with that inventory, like move the items around. Here's another silly example:

Code: Select all

function love.keypressed(k)
  if k == "up" then
    if item_highlighted > 1 then
      item_highlighted = item_highlighted - 1
    end
  elseif k == "down" then
    if item_highlighted < MAX_INVENTORY then
      item_highlighted = item_highlighted + 1
    end
  elseif k == "return" then
    if item_selected then
      inventory:swap( item_selected, item_highlighted )
      item_selected = nil
    else
      item_selected = item_highlighted
    end
  end
end
Officially, these three pieces of the system you want are called the "Model", "Viewer", and "Controller". If you combine them into a main.lua and test it in love, it should be a working demo. Obviously, to make a bigass RPG or MMORPG, you have a lot of work ahead of you.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Inventory and equipment inventory help

Post by coke905 »

thanks but i still ahve 1 question. Say i were to have a player and if he collides with an item how wud i make it so the item gets picked up.

Oh and how will i set each index to a different image is there an easier way then just doing
inventory[1] = love.graphics.newImage("images/apple.png")

is there an easier way?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Inventory and equipment inventory help

Post by Roland_Yonaba »

coke905 wrote:thanks but i still ahve 1 question. Say i were to have a player and if he collides with an item how wud i make it so the item gets picked up.
Fairly simple...You may have a boolean stating if the object was taken or not.
When the player collides with that object, set this boolean to false, undraw it, and move it the array of objects that the player owns.
That's a way, yet there are plenty others. Truth is, there is no specific algorithm for that purpose, just do it your way.
coke905 wrote: Oh and how will i set each index to a different image is there an easier way then just doing
inventory[1] = love.graphics.newImage("images/apple.png")

is there an easier way?
Yes, there is. And it depends.
If your images are named in a specific sequence, you could make use of it.
Using a for loop and concatenation operator.

Code: Select all

-- Assuming they are named 1.png, 2.png, 3.png
-- n must be defined!
for i = 1,n do 
inventory[i] = love.graphics.newImage(i .. '.png')
end

-- Or Assuming they are named image1.png, image2.png
-- n must be defined!
for i = 1,n do 
inventory[i] = love.graphics.newImage('image'.. i .. '.png')
end
You have to adapt it to your situation
Alternatively, you can load all images inside a folder to a table.(That's a bit rough, but fairly doable)
They will be indexed with with string corresponding to their names.

Code: Select all

local imageFolder = 'images/' --Path to your images folder
local images = love.filesystem.enumerate (imageFolder)
local inventory = {}
for i,imageRelativePath in ipairs(images) do
   local imageName = (imageRelativePath:gsub('.%a+$',''))
   inventory[imageName] = love.graphics.newImage(imagesFolder .. imageRelativePath )
end
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Inventory and equipment inventory help

Post by coke905 »

LOL thats extremely confusing. Can you make a youtube video tutorial or something, i seem to learn better with videos.

If you dont know a good free camera program to use type in "hypercam2".

I'm new and what you wrote there is just confuzzling me (LOLZ) anyways can someone make a video on it or try to explain it like your telling a grade 7er.

thanks
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Inventory and equipment inventory help

Post by Roland_Yonaba »

Making a video for such a snippet is pointless.
And you're not gonna have each time a video to help you understand things you don't get.
Just ask, afterall, threads are meant for that, I guess.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Inventory and equipment inventory help

Post by Lafolie »

coke905 wrote:LOL thats extremely confusing. Can you make a youtube video tutorial or something, i seem to learn better with videos.
Actually the snippets presented here are of quite low complexity, relative to some operations performed in game code. I would suggest that if you cannot understand these examples you should seek some basic game development (and design) articles.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Inventory and equipment inventory help

Post by coke905 »

Oh I'm sorry and I have read on google and I have tried sublime text text plus plus and way more. I know this is how I normally respond but I've been experimenting with inventory but I'm stuck on adding a strength variable to each index.

Function inventory:set(id, index, str)
self[id] = index

end

with the str perimeter I'd like to set the strength value of each id. I'm extremely sorry for wasting ur guys time I'll start looking way more into things before I post anything.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Inventory and equipment inventory help

Post by Roland_Yonaba »

coke905 wrote:Oh I'm sorry and I have read on google and I have tried sublime text text plus plus and way more. I know this is how I normally respond but I've been experimenting with inventory but I'm stuck on adding a strength variable to each index.

Code: Select all

Function inventory:set(id, index, str)
    self[id] = index
    
end
with the str perimeter I'd like to set the strength value of each id. I'm extremely sorry for wasting ur guys time I'll start looking way more into things before I post anything.
I'm not sure i caught was your problem was, but I'll suggest letting the item located a position "id" in the inventory be a table.
This way, you can store anything you want inside.

Code: Select all

Function inventory:set(id, index, str)
    self[id] = { index = index, strength = str}    
end
So that you can access to each id's index with self[id].index or inventory[id].index and each id's strength with self[id].strength or inventory[id].strength

And by the way,
coke905 wrote:Function inventory:set(id, index, str)
function must not be capitalized, as Lua is case-sensitive, Function does not refer to the keyword which declares functions, function.
May be a typo error, but just wanted to point it out.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Inventory and equipment inventory help

Post by coke905 »

Thanks ooh and I know it can't be capitalized on my iPod everytime u have a new line it automatically goes to caps ;)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 75 guests