Getting the current scale

I think I’m missing something…
How do you get the current scale? When I say print(scale) it returns function: 0x11112710.
Please help!
Thanks!

perhaps scale()

scale is a function, so makes sense that’s what it prints

I know. When I tried scale(), it returns nothing. I wish that TLL would add a return to all of the functions that turn their current value. This is a major feature that’s needed.

I think they did add it but for the style functions, eg print(textMode())

Of course you do also redefine scale by doing

currentScale = 1
temp = scale
scale = function(d)
     currentScale = currentScale * d
     temp(d)
end

but also take care to redefine pushMatrix(), resetMatrix(), popMatrix() since those also change the scale

They did add it for those, but not for transformations. The code you gave me print 1 when I don’t decla the scale, but when I do, it gives me an error stack overflow. The problem is that I don’t understand your code. I just don’t get why you call funtion(d). I am not the best coder (as you can tell), but I’m only in 7th grade, so I have time to learn.
Thanks @ruilov!

Scale is a function but it’s also a variable. first store it in the temp value. Then redefine the scale function to be one that does two things 1) keep track of the currentScale, 2) call the old scale function so that the scailing actually happens. Does that make sense?

function setup()
    currentScale = 1
    temp = scale
    scale = function(d)
        currentScale = currentScale * d
        temp(d)
    end
    scale(10)
    print(currentScale)
end

And function(d) is just part of the declaration of the function. It’s the same thing as function draw() for example. It doesnt call any functions.

I wonder, would Lua’s closures help here?

This allows you to redefine scale as @ruilov has without any global variables.

function setup()
    redefineScale()
end

function redefineScale()
    local oldScale = scale
    local currentScale = 1
    scale = function( d )
        if d == nil then
            return currentScale
        else
            currentScale = currentScale * d 
            oldScale(d)
        end
    end
end

Edit: fixed the “then” bug

Sorry for my stupidity in programming, but here is what it prints in the draw function:

1
10
2
error: [string "function setup()..."]:20: stack overflow
Pausing playback

And when put in setup, it obviously prints 10 once. When put into a short loop, it still says the same error. Do I have to put this outside of a loop?

    scale = function(d)
        currentScale = currentScale * d
        temp(d)
    end
    ~~~  
Thanks and sorry @ruilov.

Oh. I didn’t see @Simeon’s post after I posted. Let me try it out. Thanks guys!

Getting 'then' expected near 'retun' at return currentScale. I do hate being the most novice programmer on this forum (or close to it).
Thanks guys!

I’m jut trying to write a simple funtion for tilting images with the new sprite size funtion.

Sorry. Fixed the “Then” bug. The code above should be okay now. It’s different to @ruilov’s: in order to get the current scale you do

currentScale = scale() -- call scale without arguments

Wonderful! Thanks! This might be a good the to put on the FAQ.

Cool Simeon. My example btw doesn’t depend on global variables because it’s also a closure, so you can make the temp variable local and still works.

And Ziot if you do this, you also need to redefine popMatrix, resetMatrix and pushMatrix (unless you’re not using them)

@ruilov - I really have no clue how to handle the matrix. And what do you mean by “if I’m not using them”?
Thanks a lot guys!