Jump to content

[TUT]latentEvents


^.^

Recommended Posts

Hey Guys,

as an experienced member from TG said to me I should show you how to use latentEvents for loadingFiles I thought why not. So heres a small tutorial on how to use latentEvents. In this case it's for music and mod files. You can use latentEvents for every file you want to transfer to the client.

triggerLatentClientEvent: This stands for triggering a client event. Either to transfer some kind of stuff or only to start a function. I'll explain it now more exactly.

triggerLatentClientEvent([sendTo = the Element you wish to trigger it for], the Eventname, [the bandwith = how much bytes it should transfer in 1 second], [persist = [true/false]If the source resource stops and persist = true the latentEvent will be continued otherwise it will be interrupt], theElement = should be the root Element, [arguments = the files you want to trigger]) 

[ ] means that the are not needed

So let's start with an easy example. If the resource starts a file should be opened and should get transfered to the client.

Serverside:

addEventHandler("onResourceStart", getResourceRootElement(getThisResource()),function() 
    local file = fileOpen("yourFile.txt") 
    local buffer 
    if file then --Just asking if the operation was successfull 
      buffer = fileRead(file,fileGetSize(file)) --the buffer is now our argument we want to trigger 
      local output = {buffer} --more comfortable to transfer 
         for _, p in ipairs(getElementsByType("player")) do --we send it now to every player on the server 
             --to set the transferspeed we can use the filesize if not too high or an wishingvalue 
             triggerLatentClientEvent(p,"sendFile",50000 or fileGetSize(file),false,root,output) 
         end 
     end 
end)  

Clientside:

addEvent("sendFile",true) 
addEventHandler("sendFile",root,function(buffer) 
   local newFile = fileCreate(":resource/yourFile.txt") --creating the clientfile 
   for r, p in ipairs(buffer) do --getting the content out of the table by using a loop 
      fileWrite(newFile,p) --writing the content into the new file 
   end  
   fileClose(newFile) --closing the file, this will also flush the file 
end)( 

Thanks for reading this short tutorial and if there are any questions please ask me or give me some ideas on how to improve this small tutorial.

Best Wishes

Edited by Guest
Link to post

Of course I explained the the use of triggerLatentClientEvent. Well I used always the file size cause the files were not over a size of 1 MB so it works perfectly. I'll add it to the code.

Link to post

Ixjf is correct about this. You are sending the file at the same speed as the file size, if this was a big file it would lag the server. The point of latent events is too be able to slow the process so the server doesn't get overworked so much with bandwidth.

Link to post
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...