Jump to content

Saving Down functions and playing them


.:HyPeX:.

Recommended Posts

Hey Guys should this work?

  
local chatbox = outputChatBox 
function CallMe() 
chatbox("Test") 
end 
CallMe() 
  

Also, can i save them down as a string to send them, and how?

  
function BassHandler() 
--Stuff 
end 
local MyString = ""..BassHandler.."" 
MyLoad = loadstring(MyString) 
  

Link to comment
  • Moderators
Hey Guys should this work?
  
local chatbox = outputChatBox 
function CallMe() 
chatbox("Test") 
end 
CallMe() 
  

Yes (why didn't you try yourself ?!)

Also, can i save them down as a string to send them, and how?
  
function BassHandler() 
--Stuff 
end 
local MyString = ""..BassHandler.."" 
MyLoad = loadstring(MyString) 
  

This won't work even if you used the tostring function because you would get a lua function pointer instead of the instructions. As far I know, you can't

If you want to call a function with its name, you can do something like this:

function callMyFunction( funcName ) 
    _G[funcName]() 
end 
  
--and then 
function myFunction() 
    outputChatBox("Hi") 
end 
  
callMyFunction("myFunction") 

But if your function is on the server side, you can't call it from the client side and vice-versa.

Btw, I just got an idea. It would be possible actually. You can do this by making a function which will take a function name as argument. It will then open all lua files look for the function using the name provided. Once it found it, then it will read the instruction and append each line of it into a string. When the end keyword of that function is found, then it will stop everything and send the string to the client side wich will receive the string, use the loadstring function and then call that function.

That should definitly work if you can make a well coded function to parse the files and get the functions properly.

Link to comment

Well, i'm just saying about not using any client-side script, just loadstring and everything on a string server-side.

Doing this will have any effect on event handlers or it will work perfectly? (I would only have a client-side function wich loads all server-side stuff)

PD: About the first stuff, i thought anyone could just say it out, seemed easier than trying it (Since it was just a random question, it involved less effort from both sides to ask it. :mrgreen: )

Link to comment
  • Moderators
Doing this will have any effect on event handlers or it will work perfectly? (I would only have a client-side function wich loads all server-side stuff)

Please explain better (give a lot of details) what you are trying to achieve.

Do you want to prevent others to steal your client script code ?

Link to comment
Doing this will have any effect on event handlers or it will work perfectly? (I would only have a client-side function wich loads all server-side stuff)

Please explain better (give a lot of details) what you are trying to achieve.

Do you want to prevent others to steal your client script code ?

Yes, but im unsure if loadstring will let a function go on perfectly, if i load like this, will it work?

  
myString = "function Cheetah() 
dxDrawText('Test', 400,200, 100,100,tocolor(255,255,255,255),3) 
end 
addEventHandler('onClientRender', getRootElement(), Cheetah)" 
loadstring(myString) 
  

Link to comment
  • Moderators
Doing this will have any effect on event handlers or it will work perfectly? (I would only have a client-side function wich loads all server-side stuff)

Please explain better (give a lot of details) what you are trying to achieve.

Do you want to prevent others to steal your client script code ?

Yes, but im unsure if loadstring will let a function go on perfectly, if i load like this, will it work?

  
myString = "function Cheetah() 
dxDrawText('Test', 400,200, 100,100,tocolor(255,255,255,255),3) 
end 
addEventHandler('onClientRender', getRootElement(), Cheetah)" 
loadstring(myString) 
  

Yes it will work but only if you don't forget to execute that loaded string:

loadstring(myString)() --the extra parenthesis will execute the string loaded returned by the loadstring function  

Also note that you can't wrote a string in multiple lines like u did from line 2 to 5 but I got the main idea you were trying to explain.

There is a usefull topic for what you want to achieve:

https://forum.multitheftauto.com/viewtopic.php?f ... 90#p337791

This script is sending only one file to the clientside but you can easilly modify it to provide a list of file to send (for example using a xml file like the meta but only listing the client files you want to send using this way). It's loading that script as a string, then send it to the client that will receive and then use the loadstring as you did to make this work.

Note that the event "onClientResourceStart" won't be triggered for your resource in your resource because it would be triggered before your script has done loading your script from the server.

Edited by Guest
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...