Jump to content

Loadscript problem


Recommended Posts

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
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
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
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
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? xD Nothing happens.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...