Forum

> > CS2D > Scripts > How to save/load in this specific way?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to save/load in this specific way?

6 replies
To the start Previous 1 Next To the start

old How to save/load in this specific way?

Accurator
User Off Offline

Quote
I want to make a script.. But I need to make the savefile stuff first, right? Soo.. I tried to, after looking at some other related stuff, and putting it together to make the save system I want... But I have no idea what I'm doing

So I want it like this: Theres current level, exp, and next level for each stat, and they are next to each other in the savefile, like this

4 8047 10000

Thats level 4 with 8047 exp and the next level is 10000 exp, for the health stat. The next line should be the regen stat, and then some other stats.

4 8047 10000
2 29245 100000
5 59 100
7 74 100
9 653 1000
3 12 100

What I currently have as script is this:
Spoiler >


The first bit.. I think it's supposed to give new players those stats, and the loading bit (which is missing) should give players with a savegame the saved stats.

The second bit is obviously for saving stats... Dunno if I did that right.

What do I have to do to make this work?

old Re: How to save/load in this specific way?

Infinite Rain
Reviewer Off Offline

Quote
It's basically when you turn a table into a string (using serialization function, end result is going to be sth like "{exp = 5, level = 2}") then save it and just load using dofile or loadstring commands. It's much more dynamic and you won't have to fuck with whole saving system just because player table gained a few extra variables.

But I think it's too complicated for you, sorry for wasting your time.

old Re: How to save/load in this specific way?

Accurator
User Off Offline

Quote
I tried something with a table and got this:
LUA ERROR: sys/lua/Accu_server.lua:21: attempt to index global 'Stats' (a nil value)

Heres the code
Spoiler >


EDIT: Changed a bit, and now uhh.. I got a save, but it only says "Stats".

old Re: How to save/load in this specific way?

MikuAuahDark
User Off Offline

Quote
With file cs2d table.uneval , you can save and load this way(assume that the data variable name is Stats)

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
dofile"sys/lua/table_uneval.txt"
Stats={}

-- load
addhook("join","_join")
function _join(id)
	local f=io.open("sys/lua/Accu_server_saves/"..player(id,"usgn")..".sav","r")
	if f==nil then return end
	local s=loadstring("return "..f:read("*a"))
	Stats[id]=s()
	f:close()
end

-- save
addhook("leave","_leave")
function _leave(id)
	if player(id,"usgn")>0 then
		local f=io.open("sys/lua/Accu_server_saves/"..player(id,"usgn")..".sav","w")
		f:write(table.uneval(Stats[id]))
		f:close()
	end
end

-- if you want to save at other than leave(like save every minute), just call _leave(id) where id=player id. example
-- _leave(1)	-- save data for player with id 1
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview