how to save my inventory?

I’m making a game like a “escape” and I need to save my inventory to when a player leave the game and return, the inventory will be there
I have tried to use save/load of peach but… I realy dont know how to put this :S
anyone can tell me please?

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

try to learn how to save and load data, use propertyBag for a change

there’s not many options i think [import]uid: 16142 topic_id: 12352 reply_id: 45015[/import]

I have tried to usa a .lua to add itens and another stuffs to my inventory group, it works, but this isn’t a save and load… so when a player leave the game… all backs from zero again [import]uid: 23063 topic_id: 12352 reply_id: 45017[/import]

so try to learn how to save and load, i got quite a few problems with it myself, but its not so hard to understand
you need to declare some variables that will store your items and then save them to file and load when needed

its easy to say, but takes time to understand how it works

i’m making point’n’click game myself) [import]uid: 16142 topic_id: 12352 reply_id: 45076[/import]

how can I save a whole group to my txt?
anyone knows? [import]uid: 23063 topic_id: 12352 reply_id: 45239[/import]

declare a variable that will store your group and than save that var.

something like that,i think [import]uid: 16142 topic_id: 12352 reply_id: 45241[/import]

I refactored property.lua a bit, but like the old one, you only save name, value pairs (not game objects). You use it like this:

props = Properties:new("myGame.settings") -- create/open myGame.settings  
props:set("Sound", "On") -- set the Sound property to On  
props:debug() -- print out the properties table  
props:saveToFile() -- write myGame.settings  
props:get("Sound") -- get the value of the Sound property  

Include Properties.lua in your project folder and test in the Simulator. I put a print statement in the constructor that reveals the location of your file.

[code]
Properties = {}
Properties.__index = Properties

local function doesFileExist(theFile)
local datafile = io.open(theFile)
if datafile == nil then
return false
else
datafile:close()
return true
end
end

local function explode(d,p)
local t, ll
t={}
ll=0
if(#p == 1) then return {p} end
while true do
l=string.find(p,d,ll,true)
if l~=nil then
table.insert(t, string.sub(p,ll,l-1))
ll=l+1
else
table.insert(t, string.sub(p,ll))
break
end
end

return t
end

function Properties:new(filename)
local properties = {}
setmetatable(properties, Properties)

properties.kvMap={}

properties.filePath = system.pathForFile(
filename or “defaults”,
system.DocumentsDirectory
)

print(properties.filePath)

– what if it doesn’t exist???
if doesFileExist(properties.filePath) then
properties:getFromFile()
end

return properties
end

function Properties:debug()
for k,v in pairs(self.kvMap) do
print(" --> " … k … " = " … v)
end
end

function Properties:saveToFile()
local datafile, errStr
datafile, errStr = io.open(self.filePath, “w+”)
if not errStr == nil then
print(“Error occurred”)
end
print("saving to file " … self.filePath … “…”)
for k,v in pairs(self.kvMap) do
datafile:write(k … “=” … v … “\n”)
end

datafile:close()
end

function Properties:getFromFile()
local theLine, theLineValue
local datafile, errStr, line

datafile, errStr = io.open(self.filePath)
if datafile == nil then
print(“err " … errStr)
return nil
else
for line in datafile:lines() do
theLine = explode(”=" , line)
self:set(theLine[1] , theLine[2])
end

datafile:close()
end
end

function Properties:set(name, value)
self.kvMap[name] = value
end

function Properties:get(name, defaultValue)
local defaultValue = defaultValue or “”

if self.kvMap[name] == nil then
self.set(name, defaultValue)
end

return self.kvMap[name]
end
[/code] [import]uid: 58455 topic_id: 12352 reply_id: 45242[/import]

I have tried and I get this error:

attempt to call method ‘new’ (a nil value)

  
local Properties = require ( "Properties" )  
  
props = Properties:new("myGame.settings") -- create/open myGame.settings  
props:set("Sound", "On") -- set the Sound property to On  
props:debug() -- print out the properties table  
props:saveToFile() -- write myGame.settings  
props:get("Sound") -- get the value of the Sound property  
  

and i have put Properties.lua in my folder [import]uid: 23063 topic_id: 12352 reply_id: 45254[/import]

Change the first line to simply

require "Properties" [import]uid: 58455 topic_id: 12352 reply_id: 45258[/import]

still the same problem :S [import]uid: 23063 topic_id: 12352 reply_id: 45262[/import]

To be clear, not this:

local Properties = require "Properties"  

But this:

require "Properties"  

It’s working for me. Maybe someone more versed in Lua can chime in, but if I put “module(…, package.seeall)” at the top of my Properties class, it doesn’t work, either. I found that method for implementing pseudo-OO online somewhere. [import]uid: 58455 topic_id: 12352 reply_id: 45263[/import]

hmm I have put module(…, package.seeall) at the top… maybe this is the problem :S [import]uid: 23063 topic_id: 12352 reply_id: 45266[/import]

Yep, that’s probably it. I found the link where I got this method of implementing a “class” in Lua (though my new method is specified with a : instead of a .):

http://lua-users.org/wiki/SimpleLuaClasses

And there are several ways to do it…I’m still learning this myself. I’m used to C++ and Java and this is a bit of a mind twist for this old programmer.

http://lua-users.org/wiki/ObjectOrientedProgramming [import]uid: 58455 topic_id: 12352 reply_id: 45267[/import]

but still dont get how this will help me with save my inventory :X hehehe
[import]uid: 23063 topic_id: 12352 reply_id: 45273[/import]

Can you give me more details on what your inventory is? What exactly are you trying to save? [import]uid: 58455 topic_id: 12352 reply_id: 45274[/import]

I’m trying to save images and some variables
functions I dont know because all of them are global [import]uid: 23063 topic_id: 12352 reply_id: 45277[/import]

You can use the same method to save variables. What do you mean save images? [import]uid: 58455 topic_id: 12352 reply_id: 45279[/import]

it’s like… in my first scene… i catch a hammer, and this hammer go to my inventory

when I exit the game… when I return to him again… I need to have my hammer in inventory in that position

i’m clear? =S [import]uid: 23063 topic_id: 12352 reply_id: 45282[/import]

Can you save variables like “Hammer=true”, “HammerX=120”, “HammerY=300”? With the actual x and y values, of course. [import]uid: 58455 topic_id: 12352 reply_id: 45284[/import]

but when I exit the game… the images will back to the same place?

and how can I put this into a .txt? [import]uid: 23063 topic_id: 12352 reply_id: 45288[/import]