Forum

> > CS2D > Scripts > [solved] Having issue in "load" function
Forums overviewCS2D overview Scripts overviewLog in to reply

English [solved] Having issue in "load" function

2 replies
To the start Previous 1 Next To the start

old [solved] Having issue in "load" function

_oops
User Off Offline

Quote
I'm using user MikuAuahDark's file cs2d table.uneval for save and load files.
before Lua got updated to 5.2, the function "loadstring" was exist however after update, the function (loadstring) deprecated but from then, function "load" can now work exactly same as loadstring.

the problem is, I have no idea about how to use this function. because originally user MikuAuahDark stated way to load is - use function "loadstring "
1
2
3
f=io.open("pl_"..usgn..".txt","r")
PLAYERS[id]=loadstring("return "..f:read("*a"))()
f:close()

I tried similar ways to figure out but I have no idea.

and this code is mine that only just replaced loadstring to load.
1
2
3
4
5
6
7
8
9
10
11
12
function find_save()
	local f = io.open("ff.dat","r")
		if ( f ) then
		local s, temp = load("return "..f:read("*a"))
		temp = s() -- Problem : assign temp with function "s" results nil ;(
		unlock.main.intro.once = temp.intro.once  
		f:close()
		return true
		end

	return false
end

So my question is - how can I assign variable "temp" from function s() ? without getting nil?

I think this error happens because of bad use of function "load", but I don't know how to use that function.
edited 1×, last 11.04.18 03:59:04 pm

old Re: [solved] Having issue in "load" function

VADemon
User Off Offline

Quote
Oh, this function is not easy to understand without a good explanation.

There're 3 functions in Lua 5.1 that CS2D uses:
• load - very specific function, you must provide a function that returns pieces of Lua code, those pieces are then put together
• loadfile - basically delayed dofile()?
• loadstring - interpretes the given string as Lua code
> ALL these functions return a FUNCTION when called. Finally, you must run this function to execute the loaded Lua code. If something fails, they return boolean ok, string errorMsg.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local luacode, err = loadstring("print( 12345 )")

print(type( luacode )) -- it's a function

if luacode then
    luacode() -- execute loaded code: > 12345
else
    error(err) -- generate Error with message
end

-- compact form:
assert(loadstring("print( 12345 )"))
-- or without error handling, just execute the returned function
loadstring("print( 12345 )")()

In your case, you need to return the Lua table as a value:
1
2
3
4
5
6
7
8
9
10
11
num = assert( loadstring('return 1234')() )
print(num)

tbl = assert( loadstring('return {3,15,1,18,492,249}')() )
print(tbl)

local tbl_var
-- should work too:
-- instantly save table into variable
assert( loadstring('tbl_var = {3,15,1,18,492,249}')() )
print(tbl_var)

The following line works as intended in CS2D console:
lua "print(assert(loadstring('return 1234')()))"


Now, I don't know what exact problem you're having. CS2D uses Lua 5.1 and what you need is loadstring(), not load(). If this question is not related to CS2D, then your code with load() should work too.

Please make sure
f:read("*a")
returns real code as is not empty.

old Re: [solved] Having issue in "load" function

_oops
User Off Offline

Quote
After reading last line of your answer "Please make sure f:read("*a") returns real code as is not empty."

I noticed that I declared function with table name like..
1
2
3
4
5
6
7
8
unlock = {
	main = 
	{ -- various type of values 
	}
}

function unlock.attack(id)
end

and used table.uneval to convert table variable. result was
1
2
3
4
5
6
{
attack = "0966BCF8"
	main = 
	{ -- variables
	},
}

comma was missing next to the value. that's why an error occurred

even though It was simple error, thanks to this I got good examples for those functions that I couldn't really understand in the book! thank you user VADemon
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview