Forum

> > CS2D > Scripts > Load a variable from .txt file (LUA script)
Forums overviewCS2D overview Scripts overviewLog in to reply

English Load a variable from .txt file (LUA script)

21 replies
Page
To the start Previous 1 2 Next To the start

old Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Quote
Hello us nrealSoftWare! Today im making one simple script to load 4 variables from file. so i don't know why my script is not working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("join","_load_data")
function _load_data()
owner = {0}
admin = {0}
vip = {0}
dude = {0}

local file = io.open("sys/lua/rp/1.txt")
local string1, string2, string3, string4 = file:read("","*n","*n","*n")
	owner = string1
	admin = string2
	vip = string3
	dude = string4
end

EDIT: there is code where i using variables "owner","admin","vip","dude":
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
pl = {}
addhook("join","_join")
function _join(id)
pl[id] = {}
pl[id].usgn = player(id,"usgn")
	for _, usgn in ipairs(owner) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the OWNER!")
		end
	end
	for _, usgn in ipairs(admin) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the ADMIN!")
		end
	end
	for _, usgn in ipairs(vip) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the VIP!")
		end
	end
	for _, usgn in ipairs(dude) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the DUDE!")
		end
	end
end
edited 2×, last 13.02.15 07:25:53 pm

old Re: Load a variable from .txt file (LUA script)

Rainoth
Moderator Off Offline

Quote
1
file:read("","*n","*n","*n")
Why is the first one "" not "*n" ?

You should use
table.insert to add values to your tables. what you're doing is basically

a = {} (a table)
a = 11111 (a number)

No way would that work. Especially if you're looping it to gain all elements.

old Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Quote
@user Rainoth: i do not understand you, i should use that: ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("join","_load_data")
function _load_data()
owner = {}
admin = {}
vip = {}
dude = {}

local file = io.open("sys/lua/rp/1.txt")
local string1, string2, string3, string4 = file:read("*n","*n","*n","*n")
     owner = {string1}
     admin = {string2}
     vip = {string3}
     dude = {string4}
end
Console has written:
table expected, got nil

old Re: Load a variable from .txt file (LUA script)

_Yank
User Off Offline

Quote
Console was written, ROFL.

Anyways, you probably want to use string.match function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
addhook("join","_load_data")
function _load_data()
	owner = {}
	admin = {}
	vip = {}
	dude = {}

	local file = io.open("sys/lua/rp/1.txt", "r")
	local string1, string2, string3, string4 = string.match(file:read("*all"), "(.*), (.*), (.*), (.*)")

	table.insert(owner, string1)
	table.insert(admin, string2)
	table.insert(vip, string3)
	table.insert(dude, string4)
end

old Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Quote
1
2
3
4
5
6
7
8
local file=io.open("1.txt","r")
local line1=file:read("*n")
local line2=file:read("*n")
file:read("*l")	-- for unknown reason you need to read till newline then discard it.
local line3={string.match(file:read("*l"),"(%d*),(%d*)")}
local line4=file:read("*n")
line3[1]=tonumber(line3[1])
line3[2]=tonumber(line3[2])
That's just an example how to read them.

Now line1,line2,line3,line4 contains these
1
2
3
4
0
0
{88222,117228}
88305

old Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Quote
user MikuAuahDark has written
1
2
3
4
5
6
7
8
local file=io.open("1.txt","r")
local line1=file:read("*n")
local line2=file:read("*n")
file:read("*l")	-- for unknown reason you need to read till newline then discard it.
local line3={string.match(file:read("*l"),"(%d*),(%d*)")}
local line4=file:read("*n")
line3[1]=tonumber(line3[1])
line3[2]=tonumber(line3[2])
That's just an example how to read them.

but how i should use that?
edited 1×, last 09.02.15 01:10:17 pm

old Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Quote
line 1 equal to value at line 1, you can change the variable.
That's just an example, use it if you think that's suitable for you because we don't know what those numbers mean at your file. Maybe like money or something like that.

old Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Quote
Ok, after i see your code at first post, i assume that you want something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
owner=0
admin=0
vip={}
dude=0

addhook("startround","_load_data")
function _load_data()
	local f=io.open("sys/lua/rp/1.txt")
	owner,admin=f:read("*n","*n")
	f:read("*l")
	local v=f:read("*l")
	for w in v:gmatch("%d+") do table.insert(vip,tonumber(w)) end
	dude=f:read("*n")
end
_load_data()

old Re: Load a variable from .txt file (LUA script)

VaiN
User Off Offline

Quote
why not do this the easy way? Save your data as Lua and let Lua do the work for you.

Example Data in 1.txt:
1
return {0,0,{88222,117228},88305}

Load the data into a table:
1
2
3
4
local func,err = loadfile("1.txt")
if func then
	mydata = func()
end
now your data is all in a table and easy to use however you want to.

old Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Quote
user muxarus has written
Quote
Console has written:
bad argument #1 to 'ipairs' (table expected, got number)

Strange, i never use ipairs. That should be problen in the another script.

EDIT: Oh, i understand now. You may want something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local function list_usgn_by_comma(_)
	local __={}
	for ___ in _:gmatch("%d+") do
		table.insert(__,tonumber(___))
	end
	return __
end

addhook("startround","load_data")
function load_data()
	local f=io.open("sys/lua/rp/1.txt","r")
	owner=list_usgn_by_comma(f:read("*l"))
	admin=list_usgn_by_comma(f:read("*l"))
	vip=list_usgn_by_comma(f:read("*l"))
	dude=list_usgn_by_comma(f:read("*l"))
	f:close()
end
Code above shouldn't throw any ipairs error in your script.

old Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Quote
user VaiN has written
why not do this the easy way? Save your data as Lua and let Lua do the work for you.

oh, of course i can do this

user MikuAuahDark has written
Code above shouldn't throw any ipairs error in your script.

still have problem.
Quote
Console has written:
bad argument #1 to 'ipairs' (table expected, got number)

old Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Quote
what line? it's bit hard to debug them if you don't give us the line number. Maybe the variable is overwritten somewhere in your another script. Check it again, or add this after the declaration of load_data
load_data()

old Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Quote
@user MikuAuahDark:
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
local function list_usgn_by_comma(_)
     local __={}
     for ___ in _:gmatch("%d+") do
          table.insert(__,tonumber(___))
     end
     return __
end

addhook("startround","load_data")
function load_data()
     local f=io.open("sys/lua/rp/1.txt","r")
     owner=list_usgn_by_comma(f:read("*l"))
     admin=list_usgn_by_comma(f:read("*l"))
     vip=list_usgn_by_comma(f:read("*l"))
     dude=list_usgn_by_comma(f:read("*l"))
     f:close()
end

pl = {}
addhook("join","_join")
function _join(id)
pl[id] = {}
pl[id].usgn = player(id,"usgn")
     for _, usgn in ipairs(owner) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the OWNER!")
          end
     end
     for _, usgn in ipairs(admin) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the ADMIN!")
          end
     end
     for _, usgn in ipairs(vip) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the VIP!")
          end
     end
     for _, usgn in ipairs(dude) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the DUDE!")
          end
     end
end
errors only in that script. if i add inside that script default numbers for variables (owner, admin, vip, dude) will be no errors but i want to load variables from file
edited 2×, last 15.02.15 10:42:39 am
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview