Forum

> > CS2D > Scripts > Save function
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Save function

10 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Save function

Mami Tomoe
User Off Offline

Zitieren
hi guyz it is meh egain
please help me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function SaveUserAddons(id,file,UsgnID)
	file = io.open("sys/lua/"..lua_name.."/"..save_name.."/"..UsgnID..'-addons.txt', "w+") or io.tmpfile()
	local text = ""
	local text = Addons[1]
	for k, v in ipairs(Addons) do
		local firstround=false
		if firstround==false then
			local text = v.FLic[id]
			local firstround=true
		else
			local text = text .." "..v.FLic[id]
		end
	end
	file:write(text)
	file:close()
end

What I'm meant to find inside the text file is this:
(1 = have the addon 0 = don't have the addon)
0 0 0 0 0 0 0 0 (8 times and ignore the fact they are only 0's they can be 1 if I have the addons)
but I get 0 once.

How to fix it

alt Re: Save function

Rainoth
Moderator Off Offline

Zitieren
You should begin by fixing small irregularities in your code. For example line 3 declaring text and then line 4 declaring text again. Why execute line 3 at all then if you're going to overwrite the value of it right after? Just remove line 3

The next thing is how you think loops work (maybe?). You say "firstround is false" then do something with it and set firstround to true. The loop goes again and finds the first line in it - "firstround is false". So firstround will ALWAYS be false and the 'else' part of your 'if' will never work.
1
2
3
4
5
6
7
8
9
function SaveUserAddons(id,file)
	file = io.open("sys/lua/"..lua_name.."/"..save_name.."/"..player(id,"usgn")..'-addons.txt', "w+") or io.tmpfile()
	local text = ""
	for k, v in ipairs(Addons) do
		text = text..v.FLic[id].." " -- will result in string such as '0 0 0 0 0 0 0 0 0 ' note the extra space at the end
	end
	file:write(text)
	file:close()
end
Mind that I re-wrote it according to what you posted.
The problem I see potentially lies in how you loop the table. I don't know the contents of 'Addons' but you take every value of it (which looks like is a table too) and access it by player id. I don't know if that's your problem or if that's intended. I'm just guessing.

alt Re: Save function

Mami Tomoe
User Off Offline

Zitieren
it work

but i found another problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
calling the function from another function:
file = io.open("sys/lua/"..lua_name.."/"..save_name.."/"..UsgnID..'-addons.txt', "r+") or io.tmpfile()
LoadUserAddons(id,file,UsgnID)

the function:
function LoadUserAddons(id,file,UsgnID)
	for line in file:lines() do
		local counter=1
		local parses = totable(line)
		for k, v in ipairs(Addons) do
			counter=counter+1
			local LicItem = tonumber(parses[counter])
			if LicItem~=nil then
				v.FLic[id]=LicItem
			else
				v.FLic[id]=0
			end
		end
	end
end
the loading doesn't work
if i do
0 1 0 0 0 0 0 0
it will give me the first addon as if it was 1 0 0 0 0 0 0 0
2× editiert, zuletzt 13.07.16 20:17:34

alt Re: Save function

Mora
User Off Offline

Zitieren
Or not.. I'm using this:
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
39
40
41
42
function totable(t,match)
	local cmd = {}
		if not match then match = "[^%s]+" end
			for word in string.gmatch(t, match) do
			table.insert(cmd, word)
			end
		return cmd
end

addhook("join","_jj")
function _jj(id)
players[id] = {}
players[id].house = 0
players[id].money = 1000
players[id].license = 0
	if (player (id, "usgn") > 0) then
		local file = io.open ("sys/lua/save/" .. player (id, "usgn") .. ".txt", "r") 
			if (file) then
			for line in io.lines("sys/lua/save/"..player (id, "usgn")..".txt","r") do
				local parses = totable(line)
				players[id].money = tonumber(parses[1])
				players[id].license = tonumber(parses[2])
				file:close ()
				break
			end
			end
	end
end

addhook("leave","_ll")
function _ll(id)
	if (player (id, "usgn") > 0) then
		local file = io.open ("sys/lua/save/" .. player  (id, "usgn") .. ".txt", "w+")
		
		file:write (players[id].money.." "..players[id].license)
		file:close ()
		
		players[id].money = 0
		players[id].license = 0
		players[id].house = 0
     end
end

alt Re: Save function

Mami Tomoe
User Off Offline

Zitieren
what is this and how do i use it

Edit: nevr mind problem fixed i changed counter=1 to counter=0

alt Re: Save function

Masea
Super User Off Offline

Zitieren
Sorry for going to off-topic but it will be little bit important. Don't use important things at "join" hook.
user Yates hat geschrieben
Join is triggered when someone tries to connect to the server. You can see them in the spectators but they might still be connecting and then the game is not initialized yet. Because of this it is recommended to NOT do anything important or visual on join, instead either solve this the ugly way by doing what you've done and use a timer or do things the first time they join a team, this way you know everything is done and loaded on their side.
1× editiert, zuletzt 13.07.16 20:33:24

alt Re: Save function

Mora
User Off Offline

Zitieren
for example you have your addons..
their name.. let it to be:
1
2
3
players[id].addonWALL=0
players[id].addonKID=0
players[id].addonMOM=0

this is the first lines in join hook.

You have to write your values which you need to safe after
local parses = totable(line)

for example my lines is:
1
2
3
4
5
6
7
8
9
if (file) then
               for line in io.lines("sys/lua/save/"..player (id, "usgn")..".txt","r") do
                    local parses = totable(line)
                    players[id].money = tonumber(parses[1])
                    players[id].license = tonumber(parses[2])
                    file:close ()
                    break
               end
               end

you have to replace the values you need to save to:

1
2
3
4
5
6
7
8
9
10
if (file) then
               for line in io.lines("sys/lua/save/"..player (id, "usgn")..".txt","r") do
                    local parses = totable(line)
			players[id].addonWALL = tonumber(parses[1])
			players[id].addonKID = tonumber(parses[2])
			players[id].addonMOM = tonumber(parses[3])
                    file:close ()
                    break
               end
               end

then you have to add them in leave hook like you added in join.
A part of leave hook:
1
2
3
4
local file = io.open ("sys/lua/save/" .. player  (id, "usgn") .. ".txt", "w+")
          
          file:write (players[id].money.." "..players[id].license)
          file:close ()
To yours:
1
2
3
4
local file = io.open ("sys/lua/save/" .. player  (id, "usgn") .. ".txt", "w+")
          
          file:write (players[id].addonWALL.." "..players[id].addonKID.." "..players[id].addonMOM)
          file:close ()
After masea said:
You can change join to spawn hook.

alt Re: Save function

Rainoth
Moderator Off Offline

Zitieren
@user Mami Tomoe: You should also remove "UsgnID" variable in parameters and replace it in the function with player(id,"usgn") just like I did for saving. Also, listen to what Masea suggested. I'd advise loading on team selection.

alt Re: Save function

Mami Tomoe
User Off Offline

Zitieren
user Mami Tomoe hat geschrieben
Edit: nevr mind problem fixed i changed counter=1 to counter=0


I have to load at join otherwise it might cause problems since the whole script is built off its own which means changing one line will make it a must to change the rest and that's way to much to do in order to fix something that is already fixed.

alt Re: Save function

Rainoth
Moderator Off Offline

Zitieren
@user Mami Tomoe: No. You only set variables when someone joins. I can't think of a likely way how changing the time when they load will affect anything.

Maybe if you were trying to utilize the loaded stuff on time hooks regardless if a person joined properly or not but that would still give you errors.

Leaving it on join hook will screw you over some times. It's not hard to change "join" into "team". Seriously, that's only one word. You are probably lazy.

alt Re: Save function

Mr_Benson
BANNED Off Offline

Zitieren
1
2
3
4
5
function test (id,file)
# string...lower- then if b = file (..player(value)) id = 1,32 do #- value = -1 to do and if not 
end
end
end
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht