This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Twijn's profile picture

How to run a function within a function that will run by itself

Started by Twijn, 18 November 2015 - 09:58 PM
Twijn #1
Posted 18 November 2015 - 10:58 PM
I need a way to make a loop continue while a function is called (that will also not return soon) and the function HAS to be called within the other function
KingofGamesYami #2
Posted 18 November 2015 - 11:15 PM
Wait, what? You want to run something in parallel?
Twijn #3
Posted 19 November 2015 - 01:49 AM
Wait, what? You want to run something in parallel?
Yeah, after reading that that was quite confusing

I need to be able to run a function within a loop and have the loop continue (hopefully that's better?)
KingofGamesYami #4
Posted 19 November 2015 - 01:58 AM
That makes more sense, and although I know how to do this, it's not necessarily the best idea in the world. You'd want to "piggyback" your code onto a function that's always called when events happen - coroutine.yield.
Twijn #5
Posted 19 November 2015 - 02:05 AM
That makes more sense, and although I know how to do this, it's not necessarily the best idea in the world. You'd want to "piggyback" your code onto a function that's always called when events happen - coroutine.yield.
I can't exactly think of a way to make it better… I'm trying to make a button api that runs on startup and will work indefinitely.
Edited on 19 November 2015 - 01:05 AM
KingofGamesYami #6
Posted 19 November 2015 - 03:10 AM
I use a function in my API that filters events, like this:


function handleEvents( ... )
  local tEventData = { ... }
  if (button was clicked) then
    return "button_click", other_data
  end
  return ...
end

I've actually added a check to it to detect if it was passed event data - and if not, will call os.pullEvent itself.

A different approach would be to add a function that simply handles your buttons and require the user to run it in parallel themselves.
Bomb Bloke #7
Posted 19 November 2015 - 04:24 AM
It'd be worth checking out the example in the touchpoint API thread, which shows that sort of "event filtering" system in action.
Twijn #8
Posted 19 November 2015 - 04:55 AM
I use a function in my API that filters events, like this:


function handleEvents( ... )
  local tEventData = { ... }
  if (button was clicked) then
    return "button_click", other_data
  end
  return ...
end

I've actually added a check to it to detect if it was passed event data - and if not, will call os.pullEvent itself.

A different approach would be to add a function that simply handles your buttons and require the user to run it in parallel themselves.
I don't exactly understand the first part… Could you explain it a bit more?

Also, for the user allowing it to run itself, that may cause a few issues.
Because it is a neverending loop, would it technically make multiple loops running at the same time that works off of the same table of buttons? Would that not cause buttons to trigger more then once?
I am also creating this for a different program I'm creating, so that would have its own loop as well. I also would prefer people to just be able to create the functions and add the buttons, it's way simpler.
KingofGamesYami #9
Posted 19 November 2015 - 04:40 PM
It's not simpler. The user should have control over the API, not the other way around. Seriously, it depends on how your API is structured.

My API forces the user to handle everything. It just makes events telling them a button was pressed and which one. From there, the user of my API has absolute control over everything - otherwise you start to get complicated work-around solutions to using my API, which isn't a good thing.

I do not use a loop. I force the user to make their own loop, because more than 50% of programs are going to need that loop anyway.

The other way I have managed things (when I want things to be simple) is to have my program work like read(). One of the function is a function that returns when a button is clicked, and the data related to that button. I feel this way is easy to transition to for new users, as they have undoubtably used read() before (and if not, why are they using my API?).
Edited on 19 November 2015 - 03:42 PM
Twijn #10
Posted 19 November 2015 - 09:55 PM
It's not simpler. The user should have control over the API, not the other way around. Seriously, it depends on how your API is structured.

My API forces the user to handle everything. It just makes events telling them a button was pressed and which one. From there, the user of my API has absolute control over everything - otherwise you start to get complicated work-around solutions to using my API, which isn't a good thing.

I do not use a loop. I force the user to make their own loop, because more than 50% of programs are going to need that loop anyway.

The other way I have managed things (when I want things to be simple) is to have my program work like read(). One of the function is a function that returns when a button is clicked, and the data related to that button. I feel this way is easy to transition to for new users, as they have undoubtably used read() before (and if not, why are they using my API?).
As I believe I said above, I'm going to be using the API additionally. It's going to be a sortof "OS" that provides APIs and other things that are useful to people that make public programs (or even private ones). I'd highly prefer it to just run indefinitely at the startup of the computer, which will overall be easier for people and me because then they do not need to set up any loop to run with the program, it's just all there.

Also, why exactly would people need the loop?e



I have been over thinking everything you have said and I have not made complete sense of it, obviously.

It is not that hard to make them create their own loops… It is actually quite simple.

Sorry for being so stupid. xD
Edited on 19 November 2015 - 09:03 PM
Bomb Bloke #11
Posted 20 November 2015 - 12:57 AM
Again, the touchpoint API example covers the scenario KoGY's talking about. If you don't understand what he's saying, I again suggest you go read that.
Twijn #12
Posted 20 November 2015 - 02:08 AM
Again, the touchpoint API example covers the scenario KoGY's talking about. If you don't understand what he's saying, I again suggest you go read that.

Oh, yeah, sorry, I completely didn't see your post somehow! I'll go check it out. :)/>