This function looks good to me (but can’t test now). Which line are you getting this error at? It seems like it doesn’t come from this function at all, as “writing to node” implies you are changing characters on the text element on canvas. Also I’m pretty sure you don’t need to load the default font of the style to change it, only the new font needs to be loaded.
Right okay I think that makes sense since I don’t think it’s coming from the function. Why would the error occur when I try to change characters on the text element. For reference this is where I call the text element.
const title1 = figma.createText();
title1.fills = s{type: "GRADIENT_LINEAR", gradientTransform: rm1,0,0], 00,1,0]], gradientStops: p{position: 0, color: {r: 0.094, g: 0.098, b: 0.235, a: 1}}, {position: 1, color: {r: 0.149, g: 0.156, b: 0.380, a: 1}}] }];
title1.characters = ("Cover Title");
title1.fontSize = (100);
You don’t load fonts at all in this piece of code, so this would explain the error (unless you have font loading code before it somewhere of course). You need to await for the fonts loading function to finish and then you can change the characters.
Thanks for getting back to me Gleb!
I have code (the code in the initial post) before this code, wouldn’t that load and await it?
No. Just like you are awaiting for fonts to load (await figma.loadFontAsync
), you need to await for the createNewTextStyle
function to finish. Note that for that to work, it needs to be called inside the async function, not in the global scope since await only works inside async functions (but if you are using TypeScript, it should warn you about that).
Also if you removed the default font loading like I advised, you need to bring it back somewhere since you are not applying the style you just created and loaded the font for to the text you are modifying. So you can even drop that function completely and just load the default font before setting the characters to the text.
Okay so this is what I have so far. How do I await the async function to finish?
async function loadFont() {
await figma.loadFontAsync({
family: 'Roboto',
style: 'Regular',
});
};
(async() => {
await loadFont()
})();
const nodes: SceneNode[] = [];
const title1 = figma.createText();
title1.fills = [{type: "SOLID", color: {r: 1, g: 1, b: 1}}];
title1.characters = ("Cover Title");
title1.fontSize = (100);
figma.currentPage.appendChild(title1);
nodes.push(title1);
Move this line: })();
to the end of this code block.