Changing the font and sprite from the actual program

Hey,i know this might be a dumb question,but i just got into coding.I want to know what code lines to write so that i can change the font of the text,and the sprite with the help of some parameters or something. :slight_smile:

see the font() and fontSize() functions in the built in docs!

I know,i already set them,but i’d like to be able to change it while running the program,and the same for the sprites…

You first need to understand how Codea draws

https://coolcodea.wordpress.com/2013/03/10/starting-with-codea/

The draw function is called repeatedly by the Codea runtime - in fact it’s called every time Codea wants to redraw the screen (ideally 60x per second), so if you call font() inside draw() then the specified font will be used for any subsequent calls to text() until you change it to something else.

What this means is that if you store the value you pass to font() or fontSize() in a variable then you can change the value over time and then if you call fontSize(myFontSize) (assuming myFontSize is the name of the variable) then the font size will be set to whatever number is stored in myFontSize.

Hopefully this will make some sense

function setup()
    parameter.number("myFontSize",10,120,25)  -- this just declares the variable and allows you to modify it by using the slider
    otherFontSize = 10  -- this is just a normal variable
end

function draw()
    background(0)
    fontSize(myFontSize)
    textMode(CENTER)
    text("HELLO WORLD",WIDTH/2,HEIGHT/2)
    
    fontSize(otherFontSize)
    text("CODEA!",WIDTH/2,HEIGHT/4)
    otherFontSize = otherFontSize + 1
    if otherFontSize == 120 then otherFontSize = 10 end
end

Thank you so much for your help!..but i meant the font type,for example: Arial to American Typewriter,emoji,copperplate and so on.

While running the program

function setup()
    f1 = "AmericanTypewriter-CondensedBold"
    f2 = "Baskerville-Italic"
    f3 = f1
    
    parameter.action("Change Font",function()
        if f3 == f1 then
            f3 = f2
            else
            f3 = f1
        end
    end)
    
end

function draw()
    background(0)
    font(f3)
    fill(255)
    fontSize(50)
    text("Test",WIDTH/2,HEIGHT/2)
end

note
Just substitute the font name with sprite names and change the text with sprite To work with sprites.

Thank you all so much for the help and the time! :slight_smile:

@LucaLazar - no worries, as you can see from @CodeaNoob’s example the principal is exactly the same, just store the value in a variable. Lua variables can basically hold any of the four main types of data - numbers, strings (sequence of characters), tables (basically a catchall data structure that can be used to hold lot’s of different data) and functions (you can store functions in variables and then pass them as parameters to other functions - but this a more advanced topic). Variables can also store the special value “nil” which basically means nothing (or not set to any value)

Variables are the key to making programs more useful and interactive - if you just use constant (or literal) values - ie by specifying a number directly or by by using a string “in quotes”, then every time the program run’s you’ll get exactly the same output, however by storing those values in a variable (ie something that can change) you can use the variable instead of the constant and then if you change the value (or the contents) of the variable the value that’s used will also change.

Hope that makes sense.

Yes,it does make sense :),but i m not really that advanced so for now i m learning a lot more from examples.i don t know how and when to use nil,yet.But thanks anyway