how to save my inventory?

Just use the code I gave you and use your own variables and values. When your game starts, get the properties with the get() function of Properties. [import]uid: 58455 topic_id: 12352 reply_id: 45293[/import]

but with your get() function… it will only return a string to me right?

what i realy want is load inventory’s images and variables

with this get() function
i cant load images, i guess [import]uid: 23063 topic_id: 12352 reply_id: 45303[/import]

Your images themselves do not need to be saved between games, only a list of what is in your inventory.
The images will not change so you will have a table with the images in them, what they are called, etc.
You only need to save a list of what images (items) are in your current inventory which will change. [import]uid: 10389 topic_id: 12352 reply_id: 45305[/import]

You have code that loads and draws images now, right? So just store their x and y position as properties, and use your existing image loading and drawing code to load and draw them based on what is stored in the properties file.

I don’t even know what it means to store and retrieve images as properties.

[import]uid: 58455 topic_id: 12352 reply_id: 45308[/import]

@dhennrich,
think a bit differently.

  1. How do you manage it in your game, let’s not focus on saving for a moment? You display the item on the screen in your inventory. You have a particular number of slots of inventory, right? And each slot will have an item and the position of that slot is fixed. Each of these items have a corresponding image, right?

Now,
create an array of 1…N (where n is the maximum number of items you can hold) and in each of the index, put a number that corresponds to the item you have, so for example
[lua]local prop = require(“property”)

local myInventoryBag={}
local maxItems = 10 – change this to how many you want

myInventoryBag[5]=3 – Hammer for instance
myInventoryBag[4]=7 – A metal Pipe for instance

local function getImageForItem(thisItem)
local itemNumber = tonumber(thisItem)
local theImage=""
if itemNumber==3 then
theImage=“hammer.png”
elseif itemNumber == 7 then
theImage=“metalpipe.png”
elseif … – for other options
else
return nil
end

local image = display.newImage(theImage)
return image
end

local function displayItems()
local i
for i=1,#myInventoryBag do
local x = 0 – calculate based on the i
local y = 0 – calculate based on the i

local image = getImageForItem(myInventoryBag[i])

if image==nil then return end

image.setReferencePoint(display.TopLeftReferencePoint)
image.x = x
image.y = y
end[/lua]

now all you need to do is save the items using the property.lua from gitHub

and the way to save it would be to save each item as a single property and load then also in the same way.

[lua] local i
for i=1, #myInventoryBag do
prop:setProperty(i,myInventoryBag[i] or 0)
end

prop:SaveToFile()[/lua]

to populate your inventory,

[lua] local i
for i=1, maxItems do
myInventoryBag[i] = tonumber(prop:getProperty(i,0))
end[/lua]

Give me some time to create a sample, I guess I will have a class that manages inventory and even displays it on howto.oz-apps.com

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12352 reply_id: 45348[/import]

if you can made a sample of this, i will appreciate

i’m kind a newbie so I dont understand 100%

i dont get it how do when you click on a object, him save to my inventory

i know that you says

 local i  
 for i=1, #myInventoryBag do  
 prop:setProperty(i,myInventoryBag[i] or 0)  
 end  
   
 prop:SaveToFile()  

but i need to put this in all my “touch” functions to get the itens?

and another question… where i put this whole code?
if i put this in my first scene, when i return to her from another scene, it will recreate all again… right? =X [import]uid: 23063 topic_id: 12352 reply_id: 45420[/import]

You need to look at the volume of samples that come with the SDK. There are lots of examples of adding touch listeners to objects, and performing whatever action you need in the listener. [import]uid: 58455 topic_id: 12352 reply_id: 45431[/import]

i know how to make a touch function… what i dont know is how to implement this code to my touch function… it’s like

touchItem = function(e)  
 if e.phase == "ended" then  
 ...  
 end  
end  

i dont know what to put in this function to get it works
to send my object options to "function getImageForItem(thisItem)
" [import]uid: 23063 topic_id: 12352 reply_id: 45433[/import]

jayantv ,if you make that example, it will be unbelievably awesome
looking forward to it) [import]uid: 16142 topic_id: 12352 reply_id: 45435[/import]

In your touchItem function, e.target refers to the displayObject that was touched, e.x and e.y are the coordinates of the touch. [import]uid: 58455 topic_id: 12352 reply_id: 45440[/import]

i know how it works, what I dont know is how to usa the code that jayantv gave us

how to call those functions
to save and etc [import]uid: 23063 topic_id: 12352 reply_id: 45444[/import]

With the little time that I could spare to make a sample inventory system, here is an article to go with it.

http://howto.oz-apps.com/2011/07/inventory-management-system-for-games.html

and the code can be downloaded from my GitHub account listed at the end of the article.

hope this helps you to understand

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12352 reply_id: 45730[/import]

Thank you very much
I will take a look very carefull to understand all of your code do

if I have some issues I will return to you again =X
for now… thank you very much :smiley: :smiley: [import]uid: 23063 topic_id: 12352 reply_id: 45732[/import]

@Dhennrich,
auf English alle teilen neuter sind.

I understand but it just seems so funny when you replace ihn and ihnen with him and her because in English it is all it.

You were after a sample that used GUI so that you can understand how to when you click on something. I will try to make another sample, this time a bit more like a game that you can use, but I am really pressed for time.

Hope you can understand how to use the same from your code by looking at the code in the article above.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12352 reply_id: 45733[/import]

thank you very much for this tutorial
but i cant understand how to display inventory with items on the screen, not just in terminal

could you point me in right direction? anyway i will wait for your new tut if it will come someday) [import]uid: 16142 topic_id: 12352 reply_id: 45765[/import]

same problem hehe [import]uid: 23063 topic_id: 12352 reply_id: 45795[/import]

@Darkconsoles & Dhennreich,
did you understand how the inventory system works? How to put them into slots and how to save/load them to/from a file?

I will try to create a sample, but do not hold your breath for that.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12352 reply_id: 45796[/import]

I will try to display images and stuff while you are busy to made this sample ^^
not feel pressured, take your time hehehe

thanks [import]uid: 23063 topic_id: 12352 reply_id: 45801[/import]

another question… if I put this code in loading scene, for example, just the functions and method… i will be able to use some functions like “addItemToInventory” ? [import]uid: 23063 topic_id: 12352 reply_id: 45805[/import]

Why not give it a try to find out? You’re not going to learn how to program if someone else does all the work for you. If you don’t even know where to begin, it sounds as if you have some reading to do. Or maybe simplify your first game. Doom wasn’t built in a day, to borrow a phrase. [import]uid: 58455 topic_id: 12352 reply_id: 45812[/import]