A Beginner's Guide to TPT Lua

  • Videogamer555
    5th Dec 2017 Member 0 Permalink
    How do I save a lua file as the default one, so that it runs automatically when I start powder.exe? Is it default.lua? Or powder.lua? Or some other file name?
  • jacob2
    5th Dec 2017 Member 0 Permalink
    autorun.lua

    I recommend getting the script manager. Instructions are on the thread for that. You type a console command to install it.

    With the script manager, you just put it in scripts/ and then you can enable / disable it at will
  • Videogamer555
    5th Dec 2017 Member 0 Permalink

    jacob2:

    autorun.lua

    I recommend getting the script manager. Instructions are on the thread for that. You type a console command to install it.

    With the script manager, you just put it in scripts/ and then you can enable / disable it at will


    Thanks for the info. Autorun.lua will work quite nicely for permanently enabling a feature that should have always remained enabled. In this case it's allowing the act of pressing "R" to start recording frames.
    Edited 2 times by Videogamer555. Last: 5th Dec 2017
  • jacob2
    5th Dec 2017 Member 0 Permalink
    @Videogamer555 (View Post)
    It definitely should not have stayed, btw :p
    Benefits a few handful of users and causes problems for many others who accidentally recorded for a very long time and filled up their disk
  • Videogamer555
    30th January Member 0 Permalink

    jacob2:

    @Videogamer555 (View Post)
    It definitely should not have stayed, btw :p
    Benefits a few handful of users and causes problems for many others who accidentally recorded for a very long time and filled up their disk


    I know this is old, but I need to reply to this. Back when R for recording was enabled, it wouldn't just start when you pressed R. Pressing R popped up a dialog box. You had to intentionally click "Ok" on that dialog box to start recording. If you didn't want to record, you could press "Cancel". There was NO WAY that you could accidentally start recording by pressing R. That may be the excuse the developers are using to excuse their choice to remove that feature, but it's not the real reason. The real reason is the developers just thought it was a useless feature, so they removed it. And that was a HUGE mistake. Fortunately I developed an autorun.lua to fix the situation.

    Unfortunately, either TPT is no longer autoloading the autorun.lua file, or TPT no longer recognizes the function tpt.record(true). Whatever the case, my autorun lua file that used to work, is not working on the current version of TPT.


    Here's the full text of my autorun.lua file:

    IsRecording=false
    FirstFrameCaptured=false

    function KeyPress(key,nkey,modifier,event)
    if key=="r" then
    if event==1 then
    if modifier==256 then
    if IsRecording==false then
    if tpt.record(true)~=0 then
    FirstFrameCaptured=false
    IsRecording=true
    end
    else
    tpt.record(false)
    IsRecording=false
    end
    end
    end
    end
    return true
    end

    function FrameFunc()
    if IsRecording==true then
    if FirstFrameCaptured==false then
    FirstFrameCaptured=true
    tpt.set_pause(0)
    end
    end
    end

    tpt.register_step(FrameFunc)
    tpt.register_keypress(KeyPress)
    Edited 5 times by Videogamer555. Last: 30th January
  • jacob1
    30th January Developer 0 Permalink
    @Videogamer555 (View Post)
    Your script is configured to run on alt+r, it works for me. It will fail if caps lock or num lock is on. This is because the tpt.register_keypress is pretty bad, almost every script that listens for ctrl has the same problem as yours.
    I recommend switching to the new event api: https://powdertoy.co.uk/Wiki/W/Lua_API:Event.html
    IsRecording=false
    FirstFrameCaptured=false

    function KeyPress(key, scan, rep, shift, ctrl, alt)
    if key == 114 and ctrl and not rep then
    if IsRecording==false then
    if tpt.record(true)~=0 then
    FirstFrameCaptured=false
    IsRecording=true
    end
    else
    tpt.record(false)
    IsRecording=false
    end
    end
    return true
    end

    function FrameFunc()
    if IsRecording==true then
    if FirstFrameCaptured==false then
    FirstFrameCaptured=true
    tpt.set_pause(0)
    end
    end
    end

    event.register(event.tick, FrameFunc)
    event.register(event.keypress, KeyPress)


    Your script is exactly what I intended to happen when I removed the r shortcut. It's fairly simple to create a Lua script to do recordings, and the players that actually want to do recordings are probably the same as the players who know how to Lua script and put together all the recorded parts with ffmpeg.

    Don't try to come up with theories about why I removed the 'r' shortcut. I've stated the real reason in the past. Perhaps I didn't state it clearly enough. First, it was too easy to press with one key. If users accidentally started recording, it would fill up their disk very quickly, and this did actually happen before. I'm sure it also happened to many other users who didn't post about it on the forums.

    Many users would find the recording function by accident, and try to ask the forums how to use it. They would get recommended to use a script.

    So if it confuses and causes problems for users, and requires a script anyway, the obvious solution is to just require the record function to be a script too. The new record function also has the benefit of not spamming your data directory with recordings, and you can do multiple recordings in one session more easily without the .ppm IDs starting where the last one left off. In the future, I could embed a script like this into TPT. But I was actually thinking of just embedding ffmpeg and calling it via c++ to make gifs.

    I also don't like the suggestion that I removed a feature because I thought it was useless. I hate removing features, and often ask other devs to add them back. I don't remove things for no reason, I had good reason to remove that key shortcut.