money variable not defined

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
User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

money variable not defined

#1 Post by monkeykg »

I'm finally trying to integrate a money system and it's saying my money isn't defined but, well I'm not sure how to define it then.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 58, in script
    menu:
  File "game/script.rpy", line 13, in show_money_overlay
    if first_time_showing_this:
NameError: global name 'goldcoins' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 58, in script
    menu:
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\ast.py", line 652, in execute
    renpy.exports.say(who, what, interact=self.interact, *args, **kwargs)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\exports.py", line 1180, in say
    who(what, *args, **kwargs)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\character.py", line 1016, in __call__
    self.do_display(who, what, cb_args=self.cb_args, **display_args)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\character.py", line 817, in do_display
    **display_args)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\character.py", line 566, in display_say
    rv = renpy.ui.interact(mouse='say', type=type, roll_forward=roll_forward)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\ui.py", line 287, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\display\core.py", line 2635, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\display\core.py", line 2890, in interact_core
    self.compute_overlay()
  File "C:\Users\monkey\Downloads\renpy-6.99.14.1-sdk\renpy\display\core.py", line 2307, in compute_overlay
    i()
  File "game/script.rpy", line 13, in show_money_overlay
    if first_time_showing_this:
NameError: global name 'goldcoins' is not defined

Windows-8-6.2.9200
Ren'Py 6.99.14.3.3347
test 1.0
Tue May 22 20:34:45 2018
here's the script.

Code: Select all

# The script of the game goes in this file.

# Declare characters used by this game. The color argument colorizes the
# name of the character.
init python:
    
    first_time_showing_this = False
    show_gold_coins = False

    def show_money_overlay():
        global first_time_showing_this, show_gold_coins;
        if show_gold_coins:
            cash_widget = Text( str(goldcoins) );
        if first_time_showing_this:
            cash_widget = Alpha(0,1.0,1.0)( cash_widget ); # makes a fade_in effect
            first_time_showing_this = False; # remember to don't show the fade in effect again
       
        ui.at( Position(xalign=.9, yalign=.9) ); # positions it on the screen
        ui.add(cash_widget); # add the widget to the screen

# a convenient way of showing this =D
    def show_money():
        global first_time_showing_this, show_gold_coins;

    first_time_showing_this = True;
    show_gold_coins = True;

    config.overlay_functions.append(show_money_overlay);
label start:
    jump morning
   
label morning:
    $ goldcoins = 100
    $ _game_menu_screen = None
    $ show_button_game_menu = False
    window show dissolve
    $ show_button_game_menu = True
    scene color_black
    $ show_money(); # ← this line over here

    jump afternoon
    
label afternoon:
    scene color_black ## Make screen black
    $ goldcoins = 100 ## Shit so goldcoins
    $ _game_menu_screen = None  ## bye right click
    $ show_button_game_menu = False  ## Hide In-Game Menu
    ## Change BG image to store_afternoon
    window show  #Show dialogue box
    $ show_button_game_menu = True  ## Show In-Game Menu
    "Hello Uncle Pacundo!... Today, I make much much money! See!?"
    $ show_money(); ## Show me the money!
    show gui_goldcoins #Show fancy window around goldcoins Text
    "Oho!... This much!?... You are very good girl today!"
    "buy something bitch"
    
label shop:
    "buy.."
    menu:
        "Dog. Just a whole ass dog." if goldcoins>= 200:
            $ goldcoins -=200
            if goldcoins<=200:
                jump bitchnomoney
            jump boughtdog
        "two forty dog." if goldcoins>=3000:
            $ goldcoins -=3000
            if goldcoins<=3000:
                jump nonomoney

label boughtdog:
    "you bought a whole ass dog."
                
label nonomoney:
    "Cant afford it..."
    jump shop
    
    return ## Go back to main menu?... I dunno

User avatar
SpicyMayo1429
Regular
Posts: 36
Joined: Tue Mar 20, 2018 11:23 am
Contact:

Re: money variable not defined

#2 Post by SpicyMayo1429 »

try using

Code: Select all

default goldcoins = 100
before the start label and see if that does anything.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: money variable not defined

#3 Post by kivik »

Where did you get the code from to show your money this way? It's way more complicated than it needs to be... You can use a screen to display your money with very few lines of code...

Code: Select all

screen money:
    zorder 999
    frame:
        background None xalign 0.9 yalign 0.9
        text "[goldcoins]" color "#fff"

...
# time to show the money
show screen money
I didn't get the error when I pasted in your code, so I'm not sure if you're on an older version of Renpy and it has issues with global variables - if so, it's because you haven't added goldcoins in this line:

Code: Select all

global first_time_showing_this, show_gold_coins, goldcoins
P.S. You don't need semi-colons at the end of your python statements. You can, but you don't need to :)

User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

Re: money variable not defined

#4 Post by monkeykg »

kivik wrote: Wed May 23, 2018 5:02 am Where did you get the code from to show your money this way? It's way more complicated than it needs to be... You can use a screen to display your money with very few lines of code...

Code: Select all

screen money:
    zorder 999
    frame:
        background None xalign 0.9 yalign 0.9
        text "[goldcoins]" color "#fff"

...
# time to show the money
show screen money
I didn't get the error when I pasted in your code, so I'm not sure if you're on an older version of Renpy and it has issues with global variables - if so, it's because you haven't added goldcoins in this line:

Code: Select all

global first_time_showing_this, show_gold_coins, goldcoins
P.S. You don't need semi-colons at the end of your python statements. You can, but you don't need to :)
it's from a very old post from 2009 so i wasn't really sure if it was still accurate anymore. I'm using the most recent version of renpy!
I'm gonna try what the person above said too and remove the semi colons to see what that does and get back to you.

User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

Re: money variable not defined

#5 Post by monkeykg »

kivik wrote: Wed May 23, 2018 5:02 am Where did you get the code from to show your money this way? It's way more complicated than it needs to be... You can use a screen to display your money with very few lines of code...

Code: Select all

screen money:
    zorder 999
    frame:
        background None xalign 0.9 yalign 0.9
        text "[goldcoins]" color "#fff"

...
# time to show the money
show screen money

Also! Sorry, I meant to ask @kivik, is that code a replacement for the entire code? I'm a bit confused as to where that's supposed to fit in

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: money variable not defined

#6 Post by kivik »

monkeykg wrote: Thu May 24, 2018 12:00 am
kivik wrote: Wed May 23, 2018 5:02 am Where did you get the code from to show your money this way? It's way more complicated than it needs to be... You can use a screen to display your money with very few lines of code...

Code: Select all

screen money:
    zorder 999
    frame:
        background None xalign 0.9 yalign 0.9
        text "[goldcoins]" color "#fff"

...
# time to show the money
show screen money

Also! Sorry, I meant to ask @kivik, is that code a replacement for the entire code? I'm a bit confused as to where that's supposed to fit in
Have a read up on the screen language in Renpy: https://www.renpy.org/doc/html/screens.html

Basically the above code creates a screen that displays the goldcoins variable. And you show it by showing the screen. Learn how screens work and the code should make sense :)

User avatar
monkeykg
Regular
Posts: 48
Joined: Mon Jan 01, 2018 6:36 pm
Projects: DATE KNIGHT
Tumblr: monkeywiki
Contact:

Re: money variable not defined

#7 Post by monkeykg »

kivik wrote: Thu May 24, 2018 5:54 am
monkeykg wrote: Thu May 24, 2018 12:00 am
kivik wrote: Wed May 23, 2018 5:02 am Where did you get the code from to show your money this way? It's way more complicated than it needs to be... You can use a screen to display your money with very few lines of code...

Code: Select all

screen money:
    zorder 999
    frame:
        background None xalign 0.9 yalign 0.9
        text "[goldcoins]" color "#fff"

...
# time to show the money
show screen money

Also! Sorry, I meant to ask @kivik, is that code a replacement for the entire code? I'm a bit confused as to where that's supposed to fit in
Have a read up on the screen language in Renpy: https://www.renpy.org/doc/html/screens.html

Basically the above code creates a screen that displays the goldcoins variable. And you show it by showing the screen. Learn how screens work and the code should make sense :)
Ah ty will do. I got the code working as is for now but im gonna look into this if it means it can be simplified further.

Post Reply

Who is online

Users browsing this forum: Google [Bot]