Saving objects

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
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving objects

Post by Robin »

The first argument to lady.save_all() needs to be the filename you're saving to. So you would use something like:

Code: Select all

lady.save_all('savegame', player)
Help us help you: attach a .love.
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: Saving objects

Post by Lacotemale »

Ah thanks! Thought it said to just put in the object in the docs. Btw, how do I register an object array?

Got error: Trying to serialize unregistered userdata

If I add to Equipment because Items have this:

Code: Select all

image = love.graphics.newImage("assets/items/small/"..fileName..".png"),
imageLarge = love.graphics.newImage("assets/items/large/"..fileName.."LG"..".png"),
I tried the following:

Code: Select all

for i = 1, #itemsOnMap do
  lady.register_class(itemsOnMap[i], 'item'..i)
end
No luck with it though. Also as a result of this save file stuff I found out saving player.x and player.y is useless to me. Since the player will always be at the center of the screen. What im looking for really is how to store the position on the map. (where the player is in the game)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving objects

Post by Robin »

For images and sounds, you'll want to register resources:

Code: Select all

lady.register_resource(resource, name)
Probably something like:

Code: Select all

lady.register_resource(image, fileName)
lady.register_resource(imageLarge, fileName.."LG")
In the documentation user_input is supposed to be the file name, but I guess that isn't clear enough, so I'll change that right away.
Help us help you: attach a .love.
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: Saving objects

Post by Lacotemale »

Awesome lib dude! Thanks! :awesome:
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: Saving objects

Post by Lacotemale »

Right so I will come back to tackle this. I was delighted because I seemed to get the save game working. However since I ran into trouble I converted any images to strings and draw the images from that. I ended up getting save/load working BUT broke the game quite badly somehow.

I will start again but this time with a focus on getting it working right. Loading the images into objects on game load makes lots more sense than creating them during the game. (Laaaaagggg)

How would I register the resources for an array of item objects?

Code: Select all

itemsOnMap = {
		Item:new(itemw,itemh,32, 32, 1, "Wooden Bat","woodenBat",2,nil, 0, "", 1, 1),
		Item:new(300,50,32, 32, 2, "Vital Ring","vitalRing",nil,nil, 5, "Health", 1, 7),
		Item:new(70,250,32, 32, 3, "Rusty Dagger", "rustyIronDagger",2,nil, 0, "", 1, 1),
		Item:new(60,110,32, 32, 1, "bronzeSpear","bronzeSpear",2,nil, 0, "", 1, 1),
		Item:new(390,70,32, 32, 2, "Cloth Robe","clothRobe",nil,1, 0, "", 1, 5),
		Item:new(100,250,32, 32, 3, "Pine Plank Shield", "pinePlankShield",nil,2, 0, "", 1, 3),
		Item:new(80,100,32, 32, 1, "Wooden Bat","woodenBat",2,nil, 0, "", 1, 1),
		Item:new(350,50,32, 32, 2, "Ox Ring","oxRing",2,nil, 2, "Strength", 1, 8),
		Item:new(90,250,32, 32, 3, "Cloth Robe", "clothRobe",2,nil, 0, "", 1, 5),
		Item:new(20,110,32, 32, 1, "bronzeSpear","bronzeSpear",2,nil, 0, "", 1, 1),
		Item:new(10,70,32, 32, 2, "Cloth Hood","clothHood",nil,1, 0, "", 1, 2),
		Item:new(50,250,32, 32, 3, "Cotton Boots", "cottonBoot",nil,2, 0, "", 1, 4),
		Item:new(50,250,32, 32, 3, "Vital Necklace", "vitalNeck",nil,2, 7, "Health", 1, 6)
	}
My Item class:

Code: Select all

function Item:new(x,y,w,h,itemID,itemName,fileName,atk,def,specialPercent,specialType, itemAmount, type)

    local ItemO = {
    	x = x,
		y = y,
        w = w,
        h = h,
		id = itemID,
        name = itemName,
		image = love.graphics.newImage("assets/items/small/"..fileName..".png"),
        imageLarge = love.graphics.newImage("assets/items/large/"..fileName.."LG"..".png"),
		attack = atk,
        defense = def,
        health = 1,
        amount = itemAmount,
        itemType = type
    }
    setmetatable(ItemO, { __index = Item })
    return ItemO
end
I think I tried a loop for it already but failed.
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: Saving objects

Post by Lacotemale »

Think I got it.. almost. Not sure how to go about checking if resource already exists though. Failing on some images used more than once. :D
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: Saving objects

Post by Lacotemale »

Ugh.. nope.
Trying to serialize unregistered userdata image
This is starting to annoy me now. Right. I am registering the image but then I thought maybe the registered class is missing. So then I try it all and still see this message about unregistered userdata. What on earth is going on?

Code: Select all

for i=1,#itemsOnMap do
  lady.register_class(itemsOnMap[i], itemsOnMap[i].fileName)
  lady.register_resource(itemsOnMap[i].image, itemsOnMap[i].fileName)
  lady.register_resource(itemsOnMap[i].imageLarge, itemsOnMap[i].fileName.."LG")
end
This seems to have issues with object arrays involving images. =/
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: Saving objects

Post by Lacotemale »

Decided to not use register resource on items since all that stuff is too dynamic but using it in cases where the image is attached to an object. (Not dynamic)

This approach is much better and working well. Apologies for all the posts. xD
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Saving objects

Post by Robin »

Glad to see you got it working. If you have any suggestions for improving Lady, or think of some in the future, I'd love to hear those.
Lacotemale wrote:Apologies for all the posts. xD
In the future, you can edit your last post in the thread to put in the new things you want to say if less than a day or so has passed. That way you don't spam the thread like you did today.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Todespreis and 90 guests