[Solved] Global variables and Yarn

Hi, I am a beginner and I’ve been trying to follow @worriedpixels tutorial on yarn variables and conditions.

I’m trying to set up a scene where once I go to a new scene(dialogue scene) and return to the previous one(main menu scene), the dialogue changes. I am trying to do this by using a global variable and setting it to 1 after the last dialogue branch is read in the dialogue scene, and then passing it in to yarn in the main scene, but the dialogue does not change as expected.

Any help would be appreciated! Thanks!

Hi there. In the first event that you have highlighted in red, you change the scene before changing the variable. So the scene gets changed and the variable change won’t happen because you’ve already left the scene (you leave the scene as soon as the action is encountered, so everything that comes after won’t be processed). Order is very important in GDevelop. It reads things top-to-bottom. Change the action order so that the variable is changed before the scene change.

Edit: you might want to change the order of the actions in the second red box too, so that the variable is set before the dialogue branch is launched, but I’m 100% sure on that one. Try your way and then re-order if it doesn’t work, but I think if a branch is launched and then a later action passes values to it that a Yarn if-block needs, that might not work out.

Edit 2: Is the expected result that you click the button and get the text ‘talk to man’ and then ‘do next task’, and if the variable is not equal to 1 the only text the player will see is ‘talk to man’? (the if-block won’t do anything if the variable isn’t equal to 1. The branch will just end after ‘talk to man’). That seems to be what will happen with your current set-up.

Edit 3: your godbox bbtext object is not set to show the text from Yarn. You only have style tags in it, so you won’t see the dialogue appear in it. You would need something like DialogueTree::LineText() in the ‘set to’, or clipped line text if you’re doing typewriter style text. You might find my newer Yarn dialogue tutorial useful for reviewing how to set up a dialogue system (18 minutes long): ‘Make a simple dialogue system with Yarn in GDevelop’. I use a regular text object in the tutorial, but you can use a bbtext object instead.

So there’s quite a few things to check, change and brush up on. You’re diving in at the deep end if you’re a complete beginner. Yarn can be a little tricky to start with. Good luck!

(It’s generally frowned on to ‘at’ people out of the blue.)

Hi, thank you for your response. My sincerest apologies, I didn’t realize it was frowned upon to ‘at’ people. I saw some others doing the same and thought it would be okay, but will make sure not to do so next time. Thanks for letting me know!

I tried your first two tips and it still didn’t work. In response to edit 3, If you go 4 lines up to the left, I believe I already did ‘set to’ DialogueTree::LineText(). My text from yarn is also correctly showing in the bbtext. However what you said in edit 2 helped me figure out what was wrong. I changed my yarn statements to look like this and it worked!

<< if $maintask_variable_yarn == 0 >>
talk to man
<< elseif $maintask_variable_yarn == 1 >>
do next task
<< endif >>

Hopefully this is the “proper” way to fix it. Thank you so much! I greatly appreciate it.

I’m glad I could help, even if just a little bit. I haven’t seen a Yarn set-up quite like yours. It can be hard sometimes to help without literally recreating a person’s whole events and testing things. I sometimes do that, as do many others on the forum, but it depends on how much time people have to help. So I had a look and spotted a few things I suspected were wrong. I’ve done a lot with Yarn, but I am no expert. I still consider myself a beginner with GDevelop (well, ‘intermediate’ if I’m feeling generous!). I am familiar with the standard way of setting Yarn up, but when people do things a little differently, like you have, I get stumped sometimes too.

Your Yarn code looks fine to me. If the variable might be anything other than 0 or 1, you might want a final ‘else’ to catch other values.

Edit: Regarding the order of scene change action: I did a few experiments in my game, and it seems that variable change actions that come after a scene change action do work. I didn’t know that. I learned something new today! I thought a scene change action would be processed immediately and any actions that come after would be ignored. I was wrong.

I didn’t mind being 'at’ed by you, but generally 'at’ing people when they are not already involved in the discussion is frowned upon (on here and Discord) because it might lead to some people having the inbox from hell with lots of people asking them directly for help. Some people are more relaxed about these things than others. I think it’s actually called ‘mentioning’! I never remember.

Yeah I can understand its quite hard to help on a case by case basis, but I’m grateful for any insight I can get! Often times just talking to someone in the same boat helps a lot too. Everyone’s different perspectives are always greatly valued.

Oh I see! This is actually the first time I’ve ever posted on any public forum, so thank you for the heads up.

Thanks again for your time and help! I look forward to any other tutorials you make in the future!

1 Like