FatalTerror Posted July 6, 2013 Share Posted July 6, 2013 Hi everyone, I'm trying to load a script file with the loadstring function. The problem is that: In the script file i've an event handler OnClientResourceStart but the resource is never started because i call it from my other resource. File to load function lol() outputDebugString("Hello") end addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()),lol) And on my resource: local file = fileOpen (":"..mapToLoad.."/"..xmlNodeGetAttribute (node, "src"), true); if file then local buffer; while not fileIsEOF(file)do buffer = fileRead(file, fileGetSize(file)) local l = assert(loadstring(buffer))(); end fileClose(file) end Someone know how to fix that? Big Thanks. Link to comment
Castillo Posted July 6, 2013 Share Posted July 6, 2013 After the script was loaded, you can trigger the 'onClientResourceStart' event from the resource you loaded the code. Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 After the script was loaded, you can trigger the 'onClientResourceStart' event from the resource you loaded the code. Can you explain a bit more please? Link to comment
Castillo Posted July 6, 2013 Share Posted July 6, 2013 Just after you finish loading the code with loadstring, you do: triggerEvent ( "onClientResourceStart", resourceRoot ) Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 Just after you finish loading the code with loadstring, you do: triggerEvent ( "onClientResourceStart", resourceRoot ) Obviously, something went wrong. Are you sure we can do a trigger from server side to a loaded script in client side? Link to comment
Castillo Posted July 6, 2013 Share Posted July 6, 2013 What do you mean by "from server side to a loaded script in client side"? Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 What do you mean by "from server side to a loaded script in client side"? My current loadstring() is on a file server-side and i'm trying to load a script client-side. The triggerEvent will call a client event not server. You know what I mean? Maybe it's the reason of the bug, i don't know. Link to comment
Castillo Posted July 6, 2013 Share Posted July 6, 2013 You're using loadstring client or server side? Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 You're using loadstring client or server side? Server side. Link to comment
Castillo Posted July 6, 2013 Share Posted July 6, 2013 Then how do you expect it to work? you're trying to load a client side script on a server side script, doesn't make any sense. Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 Then how do you expect it to work? you're trying to load a client side script on a server side script, doesn't make any sense. I thought loadstring make the diference by himself. Only the server script is loaded. The client script doesn't work. For the server, the client file doesn't exist. It's weird. server-side: if fileExists (":"..mapToLoad.."/"..xmlNodeGetAttribute (node, "src"))then if(xmlNodeGetAttribute (node, "type") == "server")then -- server script outputDebugString("Call loadstring from serverside") local file = fileOpen (":"..mapToLoad.."/"..xmlNodeGetAttribute (node, "src"), true); if file then local buffer; while not fileIsEOF(file)do buffer = fileRead(file, fileGetSize(file)) local l = assert(loadstring(buffer))(); end fileClose(file) end else -- client script outputDebugString("Call loadstring from clientside") triggerClientEvent("doLoadStringClientSide", getRootElement(), ":"..mapToLoad.."/"..xmlNodeGetAttribute (node, "src")) end end server-side: addEvent("doLoadStringClientSide", true); addEventHandler("doLoadStringClientSide", getRootElement(), function(name) outputDebugString("Receive "..name) if(fileExists(name))then local file = fileOpen (name, true); if file then outputDebugString("File exist?") local buffer; while not fileIsEOF(file)do buffer = fileRead(file, fileGetSize(file)) loadstring(buffer)(); end fileClose(file) end else outputDebugString(" The script file doesn't exist !", 1); end triggerEvent("onClientResourceStart", getResourceRootElement(getThisResource())) end) Link to comment
Castillo Posted July 6, 2013 Share Posted July 6, 2013 The file is on the meta.xml of the loader? because if it's not, then how do you expect it to be on the client downloaded folder? Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 The file is on the meta.xml of the loader? because if it's not, then how do you expect it to be on the client downloaded folder? Oh well, that's right... If I open the file in server side and I load it in client-side. That could work? no? Otherwise, exist an function to force the download? Link to comment
Castillo Posted July 6, 2013 Share Posted July 6, 2013 You can send the content of the file to the client side, then load it. Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 You can send the content of the file to the client side, then load it. Okay, this time the loadstring don't want execute it -- client script local file = fileOpen (":"..mapToLoad.."/"..xmlNodeGetAttribute (node, "src"), true); local fileContent = ""; if file then local buffer; while not fileIsEOF(file)do buffer = fileRead(file, fileGetSize(file)) fileContent = fileContent..buffer; end fileClose(file); triggerClientEvent("doLoadStringClientSide", getRootElement(), fileContent) end addEvent("doLoadStringClientSide", true); addEventHandler("doLoadStringClientSide", getRootElement(), function(fileText) if fileText then outputConsole(fileText); loadstring(fileText)(); else outputDebugString("Something went wrong...", 1); end triggerEvent("onClientResourceStart", getResourceRootElement(getThisResource())) end) loadstring work in client-side? Nothing happens. Link to comment
FatalTerror Posted July 6, 2013 Author Share Posted July 6, 2013 What 'fileText' returns? He returns that: outputDebugString("CLIENT DONE") I put that for test. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now