Aborting a function call

Situation:

I have a button that initiates a function that builds a table as arguments for a sql query.

(Let´s say that it is a row of buttons: Car, airplane and boat.)

When you click on Car you get red, green and white car to choose from. (At this point i have initiated the function for choosing an argument for the sql query). Here comes my question: I like to have a Back-button that abort/ terminates the query and goes back to the previous set of images again (Car, airplane and boat).

Anyone knows an alternative way to break out of the block of code and return it to the previous state?

I like to have the choice to either click back (and not choose from red, green and white car) or if the user actually like to choose one of the colours.

The code is fairly complex and there is much going on at this point in the code. :slight_smile:

the error I get is very vage, it just points to a problem with the table that is about to be sendt to the sql query and it is here I like to abort and go back.

I’m going to ignore your sentence about an error as you’ve provided no information about it and it looks like the sentence was started half way through, so I’ll let you debug your function yourself…

However…

If you want to have a long running function allow other functions (ie: the user interface) work while it is running you have two options. The first is to refactor it so that it is broken down into much smaller functions which can be called every few milliseconds, to build up the impression of one function - this is what I would advise you do, because you will likely make the code simpler and better working in the process.

The second is to add in multithreading. In Lua, this is usually done with coroutines. Essentially, you will ‘yield’ from the executing function back to your other code and then jump back into it when you want it to continue running for a bit. The complexity is that you will need to work out a way to manage the current state of the function, but essentially you are implementing ‘co-operative multitasking’.

Read here: https://www.lua.org/pil/9.4.html

Thanx for the advice horacebury, I guess I will go for the first solution, breaking it up in smaller functions, then I can call the first function that basically just hides the main buttons and shows the buttons that will make the args for the sql function. Now, I can make the choice to either go back and leave the sql as it is, or hit one of the buttons to make a new statement to the sql function :slight_smile:

That makes sense. Just for the record, a send <=> receive coroutine with yield would also do the trick.

Nice to air out ideas in the forum, it always pays off :slight_smile:

I found the error horaceburry (it´s really embarrassing haha)

my classname for the backbitten was Car.goBack and the function I called had the same name… Car:goBack()

That´s the reason I got the ‘attempt to index field ‘goBack’ (a function value)’ haha

Sometimes you think too much and the error is just in front of your nose :slight_smile:

I’m going to ignore your sentence about an error as you’ve provided no information about it and it looks like the sentence was started half way through, so I’ll let you debug your function yourself…

However…

If you want to have a long running function allow other functions (ie: the user interface) work while it is running you have two options. The first is to refactor it so that it is broken down into much smaller functions which can be called every few milliseconds, to build up the impression of one function - this is what I would advise you do, because you will likely make the code simpler and better working in the process.

The second is to add in multithreading. In Lua, this is usually done with coroutines. Essentially, you will ‘yield’ from the executing function back to your other code and then jump back into it when you want it to continue running for a bit. The complexity is that you will need to work out a way to manage the current state of the function, but essentially you are implementing ‘co-operative multitasking’.

Read here: https://www.lua.org/pil/9.4.html

Thanx for the advice horacebury, I guess I will go for the first solution, breaking it up in smaller functions, then I can call the first function that basically just hides the main buttons and shows the buttons that will make the args for the sql function. Now, I can make the choice to either go back and leave the sql as it is, or hit one of the buttons to make a new statement to the sql function :slight_smile:

That makes sense. Just for the record, a send <=> receive coroutine with yield would also do the trick.

Nice to air out ideas in the forum, it always pays off :slight_smile:

I found the error horaceburry (it´s really embarrassing haha)

my classname for the backbitten was Car.goBack and the function I called had the same name… Car:goBack()

That´s the reason I got the ‘attempt to index field ‘goBack’ (a function value)’ haha

Sometimes you think too much and the error is just in front of your nose :slight_smile: