Jump to content

Return a value from a callback function


'LinKin

Recommended Posts

Posted

Hello,

I've a function (sqlCheck) that executes a dbQuery using a callback function (myCallBackFunc).

In the callback function (myCallBackFunc), I process the result etc...

But I want to return a value from this callback function (myCallBackFunc) to the function that first called it (sqlCheck).

Is it possible? Most likely it's not, just making sure.

  • Like 1

Need a clanwar script? Click here!

Do you want some free scripts for your DD server? Visit my website.

Posted

The thing is, callbacks are not called immediately, which means that the rest of the code will have been executed by the time the callback is called.

Why would you want this? You can just do whatever you want to do with the data inside the callback.

Business System viewtopic.php?f=108&t=35797

Notepad++ Syntax Highlighting & Auto Completion viewtopic.php?f=91&t=76726

SQLite Tutorial viewtopic.php?f=148&t=38203

Posted

Yes, well the only solution would be to call another function or trigger an event inside the call back function

Need a clanwar script? Click here!

Do you want some free scripts for your DD server? Visit my website.

Posted

Calling dbPoll with -1 timeout parameter will wait for response. That's how it will return the result to the calling function. If you instead want to suspend the execution of function and resume it when result is available (without forcing the whole server to wait), use coroutines (http://www.lua.org/manual/5.1/manual.html#2.11).

  • Like 1

-

Posted

This might explain what CrystalMV meant:

local db_coroutine = coroutine.create(function() 
 dbQuery(callback, database, "SELECT * FROM table"); 
 local queryHandle = coroutine.yield(); 
 local result = dbPoll(queryHandle, 0); 
 -- more code 
end); 
coroutine.resume(db_coroutine); 
  
function callback(queryHandle) 
 coroutine.resume(db_coroutine, queryHandle); 
end 

This is what happens: you first create your coroutine by passing it its main function, we then start the coroutine by running coroutine.resume, our function gets executed, the first line (dbQuery) gets called and then the coroutine yields (waits until another call to coroutine.resume is ran). In our callback, we run coroutine.resume once more passing it our queryHandle, which will be sent to coroutine.yield and the rest of the function will get called.

It's more than okay if you don't understand any of that, coroutine and multithreading are not exactly simple. Remember that all you really need is to just use callbacks.

More links for coroutines tutorials:

http://www.lua.org/pil/9.html

http://lua-users.org/wiki/CoroutinesTutorial

  • Like 1

Business System viewtopic.php?f=108&t=35797

Notepad++ Syntax Highlighting & Auto Completion viewtopic.php?f=91&t=76726

SQLite Tutorial viewtopic.php?f=148&t=38203

Posted

I think I get it. So the when the coroutine gets resumed, the code above the "coroutine.yield();" is not executed, but the code below it is executed. Am I right?

Also, what if I have 2 coroutine.yields inside that function and the coroutine yields by the second time, when the function is executed again, is it capable to understand that it must continue with the code-execution that is below the second coroutine.yield?

Need a clanwar script? Click here!

Do you want some free scripts for your DD server? Visit my website.

Posted

You're right.

Yes, if you have 2 coroutine.yield, it will stop twice and will continue from that very respective yield.

The idea is actually very simple, a call to coroutine.yield will pause and wait for a return, which only happens through coroutine.resume, much like using executeSQLQuery or dbPoll with -1, it pauses everything and wait for the result and then continues with the rest of the code.

  • Like 1

Business System viewtopic.php?f=108&t=35797

Notepad++ Syntax Highlighting & Auto Completion viewtopic.php?f=91&t=76726

SQLite Tutorial viewtopic.php?f=148&t=38203

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...