Infinite Loop

  • BrianTheEngineer
    30th Sep 2019 Member 0 Permalink

    I was making my potions script when suddenly I found out that infinite loops are not welcome by TPT. I was making a levitation potion and that requires me to give STKM a constant VY of -3. How can I make infinite loops work without having the game lag?

  • DreamingWarlord
    2nd Oct 2019 Member 0 Permalink

    There is an update function for that.

  • jacob1
    2nd Oct 2019 Developer 0 Permalink
    Here's some information on how to use Update functions: https://powdertoy.co.uk/Wiki/W/Lua_API:Elements.html#Properties

    There's a code example on the page. If you need more information, ask in this thread.


    The infinite loop detection is for a good reason. Without that there, your game would become unresponsive and hang forever. Adding an update function to STKM is the intended way to solve your problem.
  • BrianTheEngineer
    2nd Oct 2019 Member 0 Permalink

    How can I add an extra update function to an already existing element?

  • jacob1
    2nd Oct 2019 Developer 0 Permalink
    @BrianTheEngineer (View Post)
    Just use tpt.el.stkm.id as the element identifier (first argument to elem.property)

    I think it adds on a new update function instead of overriding the default. If it does override STKM's default update function, there's some way to turn it off, but by default I think it doesn't do that.
  • BrianTheEngineer
    2nd Oct 2019 Member 0 Permalink

    @jacob1 (View Post)

     

    But now my code to stop the person from levitating after they have died becomes obsolete because the function will not run while the STKM is dead, therefore, the STKM will levitate with no code to stop it.

     

    if sim.partProperty(levitationTarget, "type"== nil then

        levitationTarget = -1

    end
     
    This is the stop levitation code. It is in the STKM's code but will never run because the function will not run while the STKM does not exist.
     
    The way I solved it before was by having the levitate function and the stop levitates function in the potion's update function. But if the STKM drinks all the potion there will be no more updating or if the potion is destroyed in some way, the STKM will fall out of the sky.
    Edited 2 times by BrianTheEngineer. Last: 2nd Oct 2019