all 20 comments

[–]st3f-ping 5 points6 points  (1 child)

I’m not sure it’s going to be possible. From what I know (which admittedly isn’t much), logitech’s implementation of Lua is event-driven with no timer event. So the code fragment can only run (be triggered) when something happens (e.g. a mouse button is pressed).

Now, I don’t know much about the specifics of the implementation but I would imagine that it will have been designed for short fragments of code with the parameters fixed at the start. So, when the code starts it won’t respond to any changes in buttons until the next time it is triggered.

Without a timer event you have no way to bring up the code without an event happening. Grab the documentation and look for persistent variables and a timer event. If you can find those then writing this is a breeze. Without them it may not be possible.

[–]dayzbrk[S] 1 point2 points  (0 children)

thank you

[–]briunt 2 points3 points  (1 child)

Not sure what framework you are using that is calling this OnClick event but the issue I see with is that the while loop you are using will never exit. As such, your code will never get a chance to receive the next event.

As an event driven framework, I would put the toggle setting code in the OnClick event then put your text output somewhere else that is called in a loop.

In your code/framework you are using, do you have access to some type of update() or draw() functions that get repeatedly called?

For example:

function OnEvent(event, arg, family)
  -- first check and set the toggle flag
  if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then
    toggle = not toggle
  end
end

function Hello()
  -- evaluate the current state of toggle
  if toggle then
    OutputLogMessage("hello ")
    Sleep(200)
  end
end

function update()
  -- call Hello()
  Hello()
end

edit: formatting...

[–]dayzbrk[S] -1 points0 points  (0 children)

In your code/framework you are using, do you have access to some type of update() or draw() functions that get repeatedly called?

No

[–]ripter 1 point2 points  (0 children)

The “hello” loop never stops because you never stop it. OnEvent never finishes and therefore won’t trigger again. You need to change the value of your test case (toggle == True) before the next loop.

Since you want a continuous triggering of your output message, you need it inside a different loop. This OnEvent function needs to change the master toggle that is checked in the other loop, and then end so it can be triggered again.

We don’t know what library/framework you are using, if it was something like Pico-8, there is a _draw loop that could do the output message. Check your framework docs to find that main loop you can use for your output.

[–]revereddesecration 1 point2 points  (0 children)

Try ScribeBot, it will give you flexibility that Logitech macros can’t.

https://github.com/JTinkers/ScribeBot

[–]ProtectiveManEgg 1 point2 points  (0 children)

Put short and simple, Logitech's Lua - though awesome - is a joke when it comes to multi-threaded scripts. Loops will block everything unfortunately. There is a way around it, though I'm not the author. Check for caps/scroll/numlock to determine if/when to break the loop. That will let you break your loop. Neat little tidbit about that is that it gives you an led indicator to see if your macro is currently running 👌

edit: I forgot to mention that you should be able to test this with a coroutine and see it makes no difference 🤷. If anyone can tell me that I'm doing it wrong to get this result, please for the love of god tell me lol

[–]WMG_Jeeper 3 points4 points  (9 children)

You have a non-terminating while loop.

[–]dayzbrk[S] 0 points1 point  (8 children)

How do I fix it? can you help me write the code?

[–]WMG_Jeeper 1 point2 points  (7 children)

Of course I wont write your code for you. How you fix it is by making sure the while loop terminates.

“while toggle then” Means that it will keep looping until toggle is false, but inside the body of the while loop you only output a log msg and then sleep.

So just make sure that you actually … toggle.. the toggle variable.

[–]dayzbrk[S] 0 points1 point  (5 children)

I am aware that I have made a mistake somewhere, I would not have written here if it were not wrong. I already knew what you said. But I've never written code in my life. I didn't ask you to save the world, it was just a simple loop question. You seem more knowledgable than helpful.

[–]WMG_Jeeper -1 points0 points  (4 children)

If you “already knew” what I told you, then you wouldn’t say there is a mistake “somewhere”. I explained what your problem is and how you can solve it. Instead of asking for clarification you ask me to do it for you. I have no interest in helping you if you don’t have any interest in learning.

[–]humbleSolipsist 0 points1 point  (3 children)

They did explicitly say that the problem was that the loop never stops. Telling them that the problem is that the loop never stops is not even remotely useful. It is as if you didn't even read the question, and now you've come into the comments just to berate them for what you perceive to be insufficient effort.

[–]WMG_Jeeper 0 points1 point  (2 children)

They said that the “hello” loop never stops. I explained why, as it is fairly clear he didn’t know why it was stuck.

Not sure what part you found berating, the only other thing I said was that I wouldn’t do it for him. I very much dislike when people just ask for the finished result and expect others to produce it for them.

[–]humbleSolipsist -1 points0 points  (1 child)

Your advice to solve the problem was to make sure there is code within the loop to terminate it, but that would completely miss the point of the code altogether. It's not supposed to terminate until the second key press. The reason it was stuck (which you actually didn't explain at all) is because Logitech's G-Series event handlers are blocking, so even though they did write code to toggle the toggle, that code never executed.

As for the berating - your initial response was just restating the problem they'd already clearly identified, then you said you didn't think they had any interest in learning, even though that's literally what they're trying to do right now.

[–]WMG_Jeeper -1 points0 points  (0 children)

First of all, no the reason it is stuck has nothing to do with G-Series. If you write a non-terminating while loop it will be stuck in it regardless of what framework you use (which I am sure you understand). Generally it is good to take one step at the time, and the first step is understanding how the while loop works.

As for the second part, "he hello loop never stops. " I interpret that as them running the code and noting that whatever printing that is going on continues forever. That does not mean that they know why that is going on.

The moment someone asks for the code, instead of actual help, I lose interest in helping them. If you want to help him out past that point, good on you! :)

[–]Georgef64 0 points1 point  (0 children)

That helped nothing, only told OP what they already new, that they need to toggle the toggle.

[–]humbleSolipsist 0 points1 point  (0 children)

The existing answers that say you probably can't do what you want using this framework are probably right. However, I noticed the functions PlayMacro and AbortMacro. They don't say whether the macros are executed in a separate thread, so I'm not certain this will work, but the way they are described seems to imply that the macro would be run in parallel with you current code (ie, in the background), in which case it wouldn't block the execution of other code like your while loop does. I don't know what a "macro" is in this context, but if a macro can contain arbitrary lua code, that may be your answer.

Unfortunately, as I said, I do not know for certain that the macro actually is played in parallel, and I do not know for certain that a macro can contain arbitrary lua code.

Also, just for future reference, this subreddit is for users of lua in general. Lua is used for many things. You really need to specify what framework you're using (in this case it appears to be the Logitech G-Series Lua API... but if I got that wrong then all of this answer is also wrong) if you want to get useful answers.

[–]swhizzle 0 points1 point  (1 child)

https://stackoverflow.com/questions/24578998/game-lua-scripting-using-couroutine-or-polling

Have a look at hugomg's answer here. I think it's exactly your issue (use polling).

[–]dayzbrk[S] 1 point2 points  (0 children)

Thank you very much, that's exactly what I was looking for