Forum

> > CS2D > Scripts > Simple Save Script
Forums overviewCS2D overview Scripts overviewLog in to reply

English Simple Save Script

7 replies
To the start Previous 1 Next To the start

old Simple Save Script

Zeik
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
sv={}

addhook("say","sv.say")
function sv.say(id,txt)
	local usgn = player(id,"usgn")
	local file
	if (txt=="!save") then
		if (usgn~=0) then
			file = io.open("sys/lua/saves/"..usgn..".txt", "w")
			file:write(VAR.."\n")  -- Writes Line 1
			file:write(VAR.."\n")  -- Writes Line 2
			file:write(VAR.."\n")  -- Writes Line 3
			file:write(VAR.."\n")  -- Writes Line 4
			file:write(VAR.."\n")  -- Writes Line 5
			file:close()
			msg2(id,"©000255000Saved!")
			return 1
		else
			msg2(id,"©000255000You must have an USGN account for saving/loading.")
			return 1
		end
	elseif (txt=="!load") then
		if (usgn~=0) then
			file = io.open("sys/lua/saves/"..usgn..".txt", "r")
			VAR=file:read("\*n") -- Reads Line 1
			VAR=file:read("\*n") -- Reads Line 2
			VAR=file:read("\*n") -- Reads Line 3
			VAR=file:read("\*n") -- Reads Line 4
			VAR=file:read("\*n") -- Reads Line 5
			file:close()
			msg2(id,"©000255000Loaded!")
			return 1
		else
			msg2(id,"©000255000You must have an USGN account for saving/loading.")
			return 1
		end
	end
end

I made a simple script for saving data to a file. I had to search a lot and had to download someone's else script (whom I don't remember and I couldn't find his post again) to understand how to actually do it.

I wanted to share it because it's very easy to understand this way, and very easy to edit it.

You just need to change VAR with whatever you want to save/load:
file:write(VAR.."\n") -- VAR is saved and "\n" creates a new line, so when you use file:write() again, it will write in that new line.
VAR=file:read("\*n") -- Whatever is in the line is assigned to VAR, calling file:read() again, reads the next line. The "\*n" is for reading numbers, so if you want to load a string, just leave VAR=file:read().

You should read the same quantity of lines that you write.
You can keep adding lines as you please.
It only saves/loads if you have an usgn account (you obviously can change that).

If you don't understand something just ask.

old Re: Simple Save Script

RisingXD
User Off Offline

Quote
If it is a single player map, can it save position, weapons, killed npc and haven't killed npc?

old Re: Simple Save Script

Zeik
User Off Offline

Quote
@user RisingXD: It can save whatever you want, you just need the variables with the information you want to save.
EDIT: if it's an "offline" singleplayer, it'd probably be better to use the player's nick instead of the usgn account.

old Re: Simple Save Script

RisingXD
User Off Offline

Quote
How to save and load?
Can you make like when press F2, a menu with save and load.
You can save and load directly there?

old Re: Simple Save Script

Zeik
User Off Offline

Quote
@user RisingXD: With this script you can save writing "!save", and load writing "!load" (without "").
The script is simple and you can improve it as you want. I'm not making personalized codes, sorry.

I don't know what you meant with "You can save and load directly there?".

old Re: Simple Save Script

Shawni
User Off Offline

Quote
Something smiliar...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
savestr={}

function readfile(filelocation, linenum) 
   saveline=1
     for line in io.lines(filelocation) do 
       savestr[saveline]=line
       saveline=saveline+1
    end
   return savestr[linenum]
end

addhook("say","a")
function a(id,txt)
    usgn = player(id,"usgn")
      if txt == "!save" and usgn ~= 0 then
       --local c = player(id,"score") -- Use something else if you want to save something else.
          SScore = io.open("sys/lua/"..player(id,"usgn")..".txt", "w")
          SScore:write(""..c.."")
          SScore:close()
       return 1
      elseif txt == "!load" and usgn ~= 0 then
          score = readfile("sys/lua/"..player(id,"usgn")..".txt", 1)-1+1
           --parse("setscore "..id.." "..score) -- To load score if it's already saved.
           --msg2(id,"©000255000Your score was succesfully loaded")
    return 1
  end
end
edited 2×, last 07.01.14 07:33:09 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview