[Solved] How do you declare a local variable in a screen?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] How do you declare a local variable in a screen?

#1 Post by henvu50 »

What's the correct way to declare and use local variables in a screen? Every time I've tried to use local variable, they somehow store data that messes up my code. I know it's my fault, so I need to figure this out once and for all.

Code: Select all

screen test():
   # which way is the correct way to declare a local variable in a screen?

   # THIS?
   default shortenString = "dir1/dir2/" + someVar + anotherVar + "this_that" + tableXYZ[x].data

   # OR THIS?
   $ shortenString = "dir1/dir2/" + someVar + anotherVar + "this_that" + tableXYZ[x].data

   imagebutton:
      idle  shortenString + "_idle.png"
      hover shortenString + "_hover.png"
      
Last edited by henvu50 on Thu Jul 15, 2021 10:21 pm, edited 1 time in total.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How do you declare a local variable in a screen?

#2 Post by hell_oh_world »

The former is actually a screen variable, the latter is a local variable, that's where the SetScreenVariable and SetLocalVariable actions come into place.
If you want a static-like declaration of something when the screen is called/shown and have it update only at specific interactions, then use screen variables.
If you want a variable to update every interaction then use local variables.
Basically, local variables are unstable since they get updated every interaction, while screen variables only update through SetScreenVariable action.
A good example that exactly shows the difference...

Code: Select all

default something = 1

screen something():
  default txt1 = "screen variable something value: " + str(something)
  $ txt2 = "local variable something value: " + str(something)

  vbox:
    align (0.5, 0.5)

    text txt1
    text txt2

label start:
  show screen something
  pause
  $ something += 1
  pause
  $ something += 1
  pause

Post Reply

Who is online

Users browsing this forum: Google [Bot]