PDA

View Full Version : Saving?



aphonia
March 13th, 2006, 22:06
I'm having a lot of trouble getting my game to save. I read a tutorial about it, but couldn't get any results.

Could anyone tell me what code to add to save my state to the file save.txt when I press pad:r()?

Here's a little sample code to work with:

--COLOR
white = Color.new(255,255,255)

--IMAGE
Player = Image.createEmpty(12,12)
Player:clear(white)

--PLAYER ARRAY
player = {}
player = {48,80,130}
player.x=player[1]
player.y=player[2]

--PAD
oldpad=Controls.read()

--MAIN LOOP

while true do
pad = Controls.read()
screen:clear()

screen:blit(player.x,player.y,Player)

--PLAYER MOVEMENT
if pad:left() then
player.x = player.x - 2
end
if pad:up() then
player.y = player.y - 2
end
if pad:right() then
player.x = player.x + 2
end
if pad:down() then
player.y = player.y + 2
end

screen.waitVblankStart()
screen.flip()
oldpad=pad
end

flashmasterfong
March 16th, 2006, 00:47
Do you know what tutorial it was? And if so could you post a link for me to see.

Zack3008
March 16th, 2006, 01:38
what is it supposed to do(yes, it matters)?? and if ur going to put oldpad and pad then u should put also oldpad:x() ~= pad:x().
E.G. "if pad:left() and oldpad:left() ~= pad:left() then"
otherwise, it's just useless and will get people confused. And, if i'm right, that is from evilmania.com, PSPMillionairre's site.

flashmasterfong
March 16th, 2006, 02:35
Hm, that does look like a lot of Charlie's code, did you just copy and paste it? Cause thats your first mistake right there, you should be typing it out. And about that code for saving, here it is:

So frist create a save.txt file.

This is a little program a made, so don't give me hell if my codes a bit sloppy.

__________________________________________________ _____________
white = Color.new(255, 255, 255)

while true do

screen:print(10, 10, "writing to file...", white)

file = io.open("save.txt","w")
file:write("writing sucessful")
file:close()

file = io.open("save.txt","r")
save = file:read()
file:close()

screen:print(10,20,save,white)

screen:flip()

screen.waitVblankStart()

end
__________________________________________________ _____________

This code works, I tested it with the windows lua player.

aphonia
March 16th, 2006, 05:04
Thank you very much, and yes, I copied those bits from evilmana.com in an effort to make a simple, easy to test program. That code isn't in my program.

As for 'what is it supposed to do?' I know the basics of it, so I'll try to figure out the rest on my own, that way I'll remember it (and know why it's done that way). But thanks anyway.