Forum

> > CS2D > Scripts > lua request
Forums overviewCS2D overview Scripts overviewLog in to reply

English lua request

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

old Re: lua request

omg
User Off Offline

Quote
Spoiler >

awesome, i shall use this to communicate between my lols test server and my main sexy server

edit: imma claimin this new pageeeeee
also i love how u write variable=nil when ur done using it! its a good practice, especially in java

old Re: lua request

loldlold123
User Off Offline

Quote
user EngiN33R has written
You most likely want to make a shared chat between different servers, as user Jynxxx said.

Receiving module:
1
2
3
4
5
6
7
8
9
10
11
addhook("say","savetext")
function savetext(id,t)
	local f=assert(io.open("/root/cs2d/chat.txt","r")) --change the address, just a placeholder
	local txt=f:read()
	f:close()
	f=assert(io.open("/root/cs2d/chat.txt","w"))
	f:write("["..game("sv_name").."] "..player(id,"name")..": "..t.."\n"..txt)
	f:close()
	f=nil
	txt=nil
end

Writing module:
1
2
3
4
5
6
7
8
9
10
11
lastline=""

addhook("second","parsemessages")
function parsemessages()
	local f=assert(io.open("/root/cs2d/chat.txt"))
	local txt=f:read("*line")
	if txt~=lastline then
		msg(txt)
		lastline=txt
	end
end

I can't really come up with a better solution at the moment, if someone can - please do share.



do anyone tried that? cuz its not working for me (i mean saving code i changed file dicrectory to sys/lua/saves/chat.txt but it didnt saved anything)

old Re: lua request

KimKat
GAME BANNED Off Offline

Quote
The variable=nil is a way to dispose of the variable in question. Making the variable non-existant, so it's a way of setting it to a false state and therefor you'll thereafter not be able to use it again.

I don't use this, but I can understand that it's pretty efficient if you want to save memory when the script is run. Since there will be a great decrease of variables stored in memory. It's very user-friendly.

old Re: lua request

omg
User Off Offline

Quote
the script doesnt create the file for u, u have to make it manually. just make a new text file and rename it
otherwise, it works fine except for a debug line
oh yeah, the second hook function needs to be global or the io needs to close() each time

old Re: lua request

Apache uwu
User Off Offline

Quote
Actually since a lot of people are going to be talking, you're just taking up cpu rounds by setting it to nil.

It would be good to set it to nil if it were a variable that you would use and then not use for a long period of time, however in this case, there's not point of doing that.

--

For the code, does it work if multiple people talk within a second?

old Re: lua request

omg
User Off Offline

Quote
no; the way its designed, it takes the last msg spoken and writes it in the other server(s) every second, unless its the same msg

old Re: lua request

EngiN33R
Moderator Off Offline

Quote
Receiver:
1
2
3
4
5
6
7
8
9
10
11
12
13
addhook("say","savetext")
function savetext(id,t)
	local f=assert(io.open("/root/cs2d/chat.txt","a+"))
	f:write("["..game("sv_name").."] "..player(id,"name")..": "..t.."\n"..txt)
	f:close()
end

addhook("second","cleartext")
function cleartext()
	local f=assert(io.open("/root/cs2d/chat.txt","w"))
	f:write("")
	f:close()
end

Transmitter:
1
2
3
4
5
6
7
addhook("second","parsemessages")
function parsemessages()
	local f=assert(io.open("/root/cs2d/chat.txt"))
	local txt=f:read()
	msg(txt)
	lastline=txt
end

The version that parses all messages spoken within a second. Unless a person says a message in the infinitesimal interval right before the hook event executes the function, it should arrive. Downside: it doesn't save the whole conversation log, haven't really come up with a clever way to do that. Also removed the nilling - user Apache uwu is right, it really only takes extra CPU and it's still going to be overwritten soon after. Correct me if I'm wrong anywhere.

old Re: lua request

omg
User Off Offline

Quote
this is what i set up in my servers:
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
--writing to other server
--in a say hook function(id,txt)
if string.sub(txt,1,1)~="!" and string.sub(txt,1,1)~="@" then
	local f=assert(io.open("test/chat.lua","a+"))
	local team=""
	if player(id,"team")==1 then team="©255064064"
	elseif player(id,"team")==2 then team="©064064255" end
	f:write(team.."["..game("sv_name").."] "..player(id,"name")..": "..txt.."\n")
	f:close()
end

--reading from other server
--in a second hook function()
for line in io.lines("test/chatz.lua") do
	msg(line)
end
local f=assert(io.open("test/chatz.lua","w"))
f:write("")
f:close()



--other server
--writing to original server
--in a say hook function(id,txt)
if string.sub(txt,1,1)~="!" and string.sub(txt,1,1)~="@" then
	local f=assert(io.open("chatz.lua","a+"))
	local team=""
	if player(id,"team")==1 then team="©255064064"
	elseif player(id,"team")==2 then team="©064064255" end
	f:write(team.."["..game("sv_name").."] "..player(id,"name")..": "..txt.."\n")
	f:close()
end

--reading from original server
--in a second hook function()
for line in io.lines("chat.lua") do
	msg(line)
end
local f=assert(io.open("chat.lua","w"))
f:write("")
f:close()
notice i use two different files and the second server is placed inside the main server's cs2d folder
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview