Hello everyone! As I am working on a class system, I made a synchronization system to network between my server side and client side objects. But the synchronization process, using the net library, can take a few instants (It's pretty quick you don't even notice) making the code still running while the object on my client is not synchronized, making it not work properly.
So I thought I would make something close to the "wait" function of MySQLOO, forcing the client to wait for the synchronization to end before continuing. So I made this little piece of code:
[CODE]while (synchronized == false) do end[/CODE]
The loop should end when the synchronization is done and the client should carry on. But instead of that, it crashes my client, as it was caught in an infinite loop. And I checked, but my variable telling if the object is synchronized is set to true when the synchronization is finished. I don't understand why it crashes my client, is there another way to force the code to "wait" until a variable is set to a certain value? If you want the full code of my class system, here it is (I don't post it here as it already is in another thread): [url]http://facepunch.com/showthread.php?t=1466986[/url] . Thank you in advance!
Because that blocks the main thread which means the code networking code (or at least the code that sets synchronized variable) never gets to run.
You should do it asynchronously.
I'm sure this will help you:
[URL="http://wiki.garrysmod.com/page/Category:timer"]http://wiki.garrysmod.com/page/Category:timer[/URL]
I understand why it doesn't work now (and I fell dumb to not have thought about it). I'll do some researches on how to do it asynchronously (You mean I should do it in another thread, right?). Thank you a lot!
Callback functions.
[QUOTE=OmegaExtern;47802177]I'm sure this will help you:
[URL="http://wiki.garrysmod.com/page/Category:timer"]http://wiki.garrysmod.com/page/Category:timer[/URL][/QUOTE]I am not sure on how timers will help me here. I don't think they create another thread, do they? I think it would make my code a mess if I tell everything to wait a second with a timer for the object to be synchronized. Moreover, my code need to return the object, I don't think I can return in a timer, take a look at my class system and you'll understand. But I'll look into it (if I misunderstood what you think I could with timers tell me). Thanks!
[QUOTE=zerf;47802225]Callback functions.[/QUOTE]
Callback could be another way, but this would change my whole class system. Still, I'll look into it. Thanks!
Every time the variable changes, call a function. Inside the function, if value is what you need it to be then execute the code.
Some features in Garry's Mod utilise threads, some do not. You don't really need to worry about whether or not something does, all you need to know is that Lua is only ever called in to in a single thread, which also means once the engine calls in to Lua, nothing else will call in to Lua until it is done.
[code]
local myVar = false
local function SetDone()
myVar = true
end
timer.Simple( 4, SetDone )
while not myVar do return end
print "We're done!"
[/code]
In the above code, the while loop will execute forever and nothing will ever be able to change the value of the myVar variable. A correct "asynchronous" approach would be something like below:
[code]
local myVar = false
local function SetDone()
myVar = true
print "We're done!"
end
timer.Simple( 4, SetDone )
[/code]
Of course in the second example, the myVar variable isn't really necessary.
You (within reason) have to use callbacks.
Combining timers and callbacks seems to work fine, even if it makes the code look like quite a mess (I'll have to tweak a lot of things). I made a client side function in my classes called "_onsynch" that is called by a timer (2 seconds for now, I'll do tests to find the best time), passing the current client. the timer is of course started after the synching process is initiated. Thank you all for your answers!
PS: I really should get more sleep and learn a lot more about Lua, doing this two things would surely avoid me some trouble.
Use Think hook or Tick hook. If you don't do much in these functions it will not crash the Game.
Use that code bellow to act when this variable reachs true but only one time not every time.
[code]
local str = "none"
local curStr = ""
hook.Add("Think", "Test", function()
if (SomeVariable) then
str = "yes"
else
str = "none"
end
if (curStr != str) then
curStr = str
if (str == "yes") then /*code here*/ end
end
end)
[/code]
[QUOTE=Yuri6037;47806466]Use Think hook or Tick hook. If you don't do much in these functions it will not crash the Game.
Use that code bellow to act when this variable reachs true but only one time not every time.
[code]
local str = "none"
local curStr = ""
hook.Add("Think", "Test", function()
if (SomeVariable) then
str = "yes"
else
str = "none"
end
if (curStr != str) then
curStr = str
if (str == "yes") then /*code here*/ end
end
end)
[/code][/QUOTE]
This solution is silly when you can have your code alongside the code that changes the SomeVariable variable.
[QUOTE=Willox;47806507]This solution is silly when you can have your code alongside the code that changes the SomeVariable variable.[/QUOTE]
In that case I would just change order/create functions/create local functions.
This is why callbacks exist though, you use your variable once the (for example) MySQL data callback is ran.
[QUOTE=Willox;47806512]This is why callbacks exist though, you use your variable once the (for example) MySQL data callback is ran.[/QUOTE]
I never iterate over every Mysql elements, cause : this would make my server to take a long time and maybe crashs it a few minutes due to the fact I have about 100000 entries in it !!!
If I need to do that in Java or C for example I would create a second thread or use a second CPU Core.
Don't select 100000 elements then
Sorry, you need to Log In to post a reply to this thread.