Forum

> > CS2D > Scripts > Open , save to table.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Open , save to table.

10 replies
To the start Previous 1 Next To the start

old Open , save to table.

Cons
User Off Offline

Quote
Hey guys, just need your help with a table. If you can make me an example, like when i enter on the server Creates, or load stats from a notepad in sys folder, and than if i use:

1
2
3
4
5
6
7
8
9
if txt:lower():sub(1, 1) == "#" then
                    local ex=txt:gsub("#","")
                    mode=ex
                    msg2(id,"Your example is now ("..mode..")")
return 1
end
end

mode="MEMBER"


Then it saves on tha notepad the "local ex" ..

My tip: File = io.open("sys/lua/stats", "w")


I knew a little of this before, but i stoped playing cs2d for a while, and now iwant to go back to my scripts.
edited 1×, last 08.07.12 10:50:57 pm

old Re: Open , save to table.

VADemon
User Off Offline

Quote
The best solution is to save the table in LUA format. So you can easily load it with dofile() and unload a table with
random_table=nil

old Re: Open , save to table.

Cons
User Off Offline

Quote
Just want a table that open the stats of a player , for example: color={255255098}

and saves it auto.

old Re: Open , save to table.

Avo
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
TBL={"LOL","FAIL","HUMILIATION"}
function SaveTableToFile(tbl,filename)
	file=io.open(filename,"w+")
	for i=1,#tbl do
		file:write(tbl[i].."\n")
	end
	file:close()
end


function LoadFileToTable(tbl,filename,clear)
	if clear==nil then clear=false end
	if clear==true then tbl={} end
	file=io.open(filename,"r")
	if file~=nil then
		for line in file:lines() do
			table.insert(tbl,line)
		end
		file:close()
	else
		print("File:'"..filename.."' not found!")
	end
end

SaveTableToFile(TBL,"sys/lua/FILE1.txt")
LoadFileToTable(TBL,"sys/lua/FILE1.txt",true)
SaveTableToFile(TBL,"sys/lua/FILE2.txt")
LoadFileToTable(TBL,"sys/lua/FILE2.txt")
SaveTableToFile(TBL,"sys/lua/FILE3.txt")

I gave you two functions that save table to txt file and open file and save to table.

function LoadFileToTable(tbl,filename,clear)
tbl is table to which data will be loaded
filename is filepath with file name
clear is optional parameter, write true if you want to clear table and then load lines to it.
function SaveTableToFile(tbl,filename)
tbl is table from data will be write to txt file
filename is filepath with filename.

old Re: Open , save to table.

VADemon
User Off Offline

Quote
Then you have "just" to write a save script.
I will do that for my plugin too... maybe I will do it tomorrow just because of you

old Re: Open , save to table.

Cons
User Off Offline

Quote
Bolt, the notepads must be named with the player usgn. For example, a player use a cmd in game: @color 255123255 ...
Than it auto saves on his file, color="255123255"

Can you understand me?

old Re: Open , save to table.

Avo
User Off Offline

Quote
Do you just want to save what player says?

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
43
44
45
--//save functions and other
function array(m,v)
	local t={}
	for i=1,m do
		t[i]=v
	end
	return t
end
function SaveTableToFile(tbl,filename)
	file=io.open(filename,"w+")
	for i=1,#tbl do
		file:write(tbl[i].."\n")
	end
	file:close()
end
function LoadFileToTable(tbl,filename,clear)
	if clear==nil then clear=false end
	if clear==true then tbl={} end
	file=io.open(filename,"r")
	if file~=nil then
		for line in file:lines() do
			table.insert(tbl,line)
		end
		file:close()
	else
		print("File:'"..filename.."' not found!")
	end
end
--//variables
texts=array(32,{})


--//game functions
addhook("say","OnSay")
function OnSay(id,txt)	
	if player(id,"usgn")>0 then
		table.insert(texts[id],txt)
		SaveTableToFile(texts[id],"sys/lua/saves/"..player(id,"usgn")..".txt")
	end
end

addhook("join","OnJoin")
function OnJoin(id)	
	LoadFileToTable(texts[id],"sys/lua/saves/"..player(id,"usgn")..".txt",true)
end
you need 'saves' folder in sys/lua/
every join data of player will be loaded (only logged to usgn,if player has played on server), if not data, nothnig loaded. On leave, texts that player said are saved to text file with his usgn as name (88318.txt for example).

old Re: Open , save to table.

Cons
User Off Offline

Quote
Fails

Like , when player uses specify command, then this is added to a line in notepads, so when he enters again in svs , it will loads his stats.

1
2
3
4
5
6
7
8
mode="User"

addhook("say", "geral")
function geral(id, txt)
              if txt:lower():sub(1, 2) == "@s" then
                    local s1=txt:gsub("@s ","")
                    mode=s1
                    msg2(id,"Your status now is ("..mode..")")

Now, just write on tha notepad, whats his MODE, so when he comes back, it will loads his MODE. Same happens if he uses @c 255255255 (or other RGB)...

old Re: Open , save to table.

Snurq
BANNED 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local objects = {}
setmetatable(objects, {__index={["subset"]=function(object, proxies)
    for _,o in ipairs(proxies) do
        if object == o then return true end
    end
end}})

function _pickle(object, seen, indent)
    --if not seen then seen = {} end
    if not indent then indent = "" end

    local serialize_key = function(key)
        if type(key) == "string" then
            return "[\""..key.."\"]"
        elseif type(key) == "table" then
            return "[".._pickle(key):gsub("\n"," ").."]"
        else
            return "["..key.."]"
        end
        return key
    end

    local escape = function(o)
        return o:gsub("\\","\\\\"):gsub("'","\\'"):gsub('"','\\"')
    end

    --Switch Object type:
    if type(object) == "table" then
        local serialize = "{\n"
        for key, value in pairs(object) do
            serialize = serialize .. indent.."\t" .. serialize_key(key) .. " = " .. tostring(_pickle(value, seen, indent.."\t")) .. ",\n"
        end
        serialize = serialize .. indent .. "}"

        return serialize
    elseif type(object) == "string" then
        return '"' .. escape(object) .. '"'
    elseif type(object) == "function" then
        return "loadstring([["..string.dump(object).."]])"
    elseif objects.subset(object, {"userdata"}) then
        return nil
    end
    return tostring(object)
end

pickle = {}

function pickle.dumps(object)
    return "return ".. _pickle(object)
end

function pickle.dump(object, filename)
    local dump = pickle.dumps(object)
    local _file = io.open(filename, "wb")
    _file:write(dump)
    _file:close()
    return dump
end

function pickle.loads(object)
    local fn = loadstring(object)
    if fn then
        return fn()
    end
end

function pickle.load(filename)
    local _file = io.open(filename, "rb")
	if not _file then return end
    local dump = _file:read("*all")
    local object = pickle.loads(dump)
    _file:close()
    return object
end


pickle.dump({color="255255255",level=100},"1337.txt")
b = pickle.load("1337.txt")
print(b.color)

Code can be found in file cs2d Devtools-r0.1 , under pickle.lua
Run it, and be amazed
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview