Can i eliminate the need for an update function by doing this?

Hi!

Quick question. i don’t really need an update function except for one animation where a triangle changes height according to the x scale of another shape. Right now, i have an update function which has something like (pseudo code):

function update(self,dt)
    local height = math.acos(self.x_scale)
    gui.set_size(self.triangle, vmathvector(300, height, 1))
end 

Could i eliminate this by having something like this?

local function triangleanim()
    if triangleanimating = true then 
        local height = math.acos(self.x_scale)
        gui.set_size(self.triangle, vmathvector(300, height, 1))
        triangleanim()
    end
end

gui.animate with custom easing ?
timer.delay(.1, true, mycode) with repeat ?

3 Likes

Your second example is using recursion, without any termination condition. Thus it will either hang indefinitely, or cause some stack related issue.

1 Like

Mathias: is that true even if I set triangleanimating to false at a later time?

There won’t be a later time if the program never exits the loop.

1 Like

Got it!!! Now it makes sense. I assumed the rest of the script (outside of that function) would continue to run but it never ever would. I need a while until thing, I think.

Can you put whatever code is meant to change the triangleanimating variable inside of this function? While we’re at it, you might want to use the repeat … until loop:

local function triangleanim()
    repeat
        local height = math.acos(self.x_scale)
        gui.set_size(self.triangle, vmathvector(300, height, 1))
    until height > 100 -- or whatever condition you want to cause the loop to end
end

Edit: It just dawned on me that this won’t really do much - you want this to animate the size of the gui, right? This code would just cycle until it reaches the final size to be animated, all in one frame, so you’d only see the gui jump to the final size.

That’s why you need the update() function - to change the size gradually. Look into what @Dragosha suggested.