i made my first script but i need some help with optimizing it

Started by jodi33, Sep 26, 2023, 08:26 PM

Previous topic - Next topic

jodi33

hello all, first time poster so sorry for spelling mistakes.

i have been playing with the CC: C bridge mod to read my create contraptions data and visualize it on a computer monitor

my code can be found here:

https://pastebin.com/YGSgjbV7

i hope to hear some good tips and or tricks

Lupus590

I would use peripheral.find instead of wrap for the first part, then you don't need to ask the user each time. If you can't use find for some reason then the settings API cane be used so that the user doesn't have to type it in every startup.

I would also indent the contents of the while true loop and reduce the repeated code with a for loop.

aAso, many people dislike slowPrint when it slows down the startup of the program.

QuickMuffin8782

Quote from: jodi33 on Sep 26, 2023, 08:26 PMi hope to hear some good tips and or tricks

Some good tips and tricks I can give you is that you can disable termination when the "terminate" event is sent through the os.pullEvent() function by doing this code:
local oldOSPull = os.pullEvent -- stores the current pullEvent function
os.pullEvent = os.pullEventRaw -- disables termination by changing the pullEvent function to the same function as pullEventRaw
You can also reverse it by doing this:
local oldOSPull = os.pullEvent -- stores the current pullEvent function
os.pullEvent = os.pullEventRaw -- disables termination by changing the pullEvent function to the same function as pullEventRaw

os.pullEvent = oldOSPull --reverse the termination prevention back where it was

You can also take a look at the old fourms for programs to use as an assist to making your programs or operating systems. Some are reposted here in the new fourms.

If you do need help, message me and I'll help you in anyway I can. Cheers! ;D
GOTTA GO FAST!!!! - Sunrise Studios now calling testers! Check the post!
Gitlab - Join my official discord! - SS Discord

Lupus590

It would be better to use pull event raw at all call sites instead of abusing it to overwrite the normal pull event. That way if your program crashes you don't leave a mess for other processes.