Alias Help

Posted by Rynok on Fri 14 Jul 2006 02:50 AM — 5 posts, 27,679 views.

USA #0
Alright, my original goal has been accomplished by changing my alias up a bit. I really like it now that I got it working, and it works great.

I was trying to use the file accessing with Lua so that I can read and write to a file for my different variables (to avoid read/write to MushClient variables).

Error:
[string "Script file"]:120: attempt to index global `io' (a nil value)
stack traceback:
[string "Script file"]:120: in function `ReadFile'
[string "Command line"]:1: in main chunk

Code: (I copyed it from another place, forgot where now)
function ReadFile()
do
local f = assert (io.open ("test.txt", "r"))
local t = f:read ("*all") -- read file in
f:close ()
Send (t) -- send to MUD
end
end
Amended on Fri 14 Jul 2006 04:34 AM by Rynok
Australia Forum Administrator #1
In the global preferences there is a Lua "sandbox" which is designed to stop dangerous operations. Misuse of the io library could be used to introduce viruses and other nasty things.

However if you trust the scripts that run you can find the line:


io = nil -- no disk IO


and comment it out like this:


-- io = nil -- no disk IO



Another approach to saving data is to use a plugin which saves its state. Then you simply put data into a MUSHclient variable and those variables will be automatically saved and loaded next time you run that plugin. See:

http://www.gammon.com.au/scripts/doc.php?function=GetVariable

http://www.gammon.com.au/scripts/doc.php?function=SetVariable
USA #2
Thanks for the help. I'll take a look and try to comment it out and see if that works. The reason I opted for the "io" stuff instead of the GetVariable was so I wouldn't have to save the Mud everytime I played (due to changing variables during play). With the IO it would be saved and the script would never change and the "world" file itself would never change so it would be a little bit less of a hassle I was thinking.
Australia Forum Administrator #3
If you shift the stuff into a plugin you don't save the world either, the plugin quietly saves its state when it is closed, and reloads it next time.
USA #4
A quick and dirty serialization function I use in my plugins to save arrays in MUSHclient variables. Simply use a la:

world.SetVariable('Test',serialize(this_is_a_table))

and

this_is_a_table = unserialize(world.GetVariable('Test'))

function serialize(var)
	if(type(var) == 'table') then
		local output = 't{'
		for index,value in pairs(var) do
			output = output .. serialize(index) .. '|' .. serialize(value) .. ';'
		end
		return output .. '}'
	elseif(type(var) == 'number') then
		return 'n{' .. var .. '}'
	elseif(type(var) == 'string') then
		return 's{' .. var .. '}'
	elseif(type(var) == 'boolean') then
		return 'b{' .. (var and 'true' or 'false') .. '}'
	end
	
	return ''
end
function unserialize(var)
	if(var == nil or var == '') then
		return nil
	end
	
	local var_type = string.sub(var,1,1)
	if(var_type == 'b') then
		return (string.sub(var,3,-2) == 'true')
	elseif(var_type == 's') then
		return string.sub(var,3,-2)
	elseif(var_type == 'n') then
		return tonumber(string.sub(var,3,-2))
	elseif(var_type == 't') then
		local table = {}
		local t = utils.split(string.sub(var,3,-2),';')
		for _,tmp in pairs(t) do
			local p = utils.split(tmp,'|',2)
			if(p[1] and p[2]) then
				table[unserialize(p[1])] = unserialize(p[2])
			end
		end
		return table
	end
	
	return nil
end


Hope that helps you if you decide to use a plugin.