Trying to show a text displayable with a function, but it just shows "0" [SOLVED]

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
Aviala
Miko-Class Veteran
Posts: 533
Joined: Tue Sep 03, 2013 8:40 am
Completed: Your Royal Gayness, Love Bug, Lovingly Evil
Organization: Lizard Hazard Games
Tumblr: lizardhazardgames
itch: aviala
Location: Finland
Contact:

Trying to show a text displayable with a function, but it just shows "0" [SOLVED]

#1 Post by Aviala »

Hello,

In my game I use a function that displays a small animation and changes the amount of the player's resources. However, players are saying they want to see a numerical value pop up next to the resource amount. I tried adding a text displayable that has a variable substitution in it. It fades in, stays on screen for 3 seconds and fades out. The code works otherwise, but for some reason instead of the number it just shows "0".

The problem probably is that for some reason the game is using the original value I set for the variable instead of the current "wealthaddsum". I tried fixing this by defining the text displayables as DynamicImages but it isn't helping. Does anyone know how I should go about doing this?

Code: Select all

default wealthaddsum_variable = 0
default armyaddsum_variable = 0
default loyaltyaddsum_variable = 0
default happinessaddsum_variable = 0

image wealth_text = DynamicImage(Text("[wealthaddsum_variable]"))
image army_text = DynamicImage(Text("[armyaddsum_variable]"))
image loyalty_text = DynamicImage(Text("[loyaltyaddsum_variable]"))
image happiness_text = DynamicImage(Text("[happinessaddsum_variable]"))

transform fadeinout2:
    alpha 0.0
    linear 0.5 alpha 1.0
    pause 0.3
    alpha 1.0
    linear 0.5 alpha 0.0
    
init python:
    wealthaddsum_variable = 0
    armyaddsum_variable = 0
    loyaltyaddsum_variable = 0
    happinessaddsum_variable = 0
    
    #wealth adding function
    def addwealth(wealthaddsum):
        global wealth
        global wealthg
        global wealthr
        global wealthaddsum_variable
        wealthaddsum_variable = wealthaddsum
        wealth += wealthaddsum
        if wealthaddsum >= 0:
            renpy.play("sounds/wealth.mp3", channel="sound")
            renpy.show("wealthg", at_list = [atwealth, fadeinout], layer = "screens")
            renpy.show("wealth_text", at_list = [atwealth2, fadeinout2], layer = "screens")
        else:
            renpy.play("sounds/wealth2.mp3", channel="sound")
            renpy.show("wealthr", at_list = [atwealth, fadeinout], layer = "screens")
            renpy.show("wealth_text", at_list = [atwealth2, fadeinout2], layer = "screens")
        wealthaddsum = 0
        renpy.hide("wealthg")
        renpy.hide("wealthr")
    
label start:
	#to use the function, I just write this (here I'm adding 20 wealth to the player's resources):
	$ addwealth(20)
   
Last edited by Aviala on Thu Feb 08, 2018 7:36 pm, edited 1 time in total.

irredeemable
Regular
Posts: 78
Joined: Thu Feb 08, 2018 7:57 am
Contact:

Re: Trying to show a text displayable with a function, but it just shows "0"

#2 Post by irredeemable »

DynamicImage("[variable]") should be provided a variable to interpolate that is the name of another displayable, not another displayable directly. There are a lot of ways to pop up text on the screen, but if you're set on doing it from a function you can simply use renpy.show('wealth', what=Text("[weathaddsum_variable]")). The first argument can be any non-empty string and will act as the displayable's tag.

User avatar
Aviala
Miko-Class Veteran
Posts: 533
Joined: Tue Sep 03, 2013 8:40 am
Completed: Your Royal Gayness, Love Bug, Lovingly Evil
Organization: Lizard Hazard Games
Tumblr: lizardhazardgames
itch: aviala
Location: Finland
Contact:

Re: Trying to show a text displayable with a function, but it just shows "0"

#3 Post by Aviala »

Thanks, but when I changed the function to this I can no longer see the text at all; not even the 0 that showed up before:

Code: Select all

    def addwealth(wealthaddsum):
        global wealth
        global wealthg
        global wealthr
        global wealthaddsum_variable
        wealthaddsum_variable = wealthaddsum
        wealth += wealthaddsum
        if wealthaddsum >= 0:
            renpy.play("sounds/wealth.mp3", channel="sound")
            renpy.show("wealthg", at_list = [atwealth, fadeinout], layer = "screens")
            renpy.show('wealth_t', what=Text("{color=#38ea4a}[wealthaddsum_variable]{/color}", at_list = [atwealth2, fadeinout2], layer = "screens"))
            #renpy.show("wealth_text", at_list = [atwealth2, fadeinout2], layer = "screens")
        else:
            renpy.play("sounds/wealth2.mp3", channel="sound")
            renpy.show("wealthr", at_list = [atwealth, fadeinout], layer = "screens")
            renpy.show('wealth_te', what=Text("{color=#f46d44}[wealthaddsum_variable]{/color}", at_list = [atwealth2, fadeinout2], layer = "screens"))
            #renpy.show("wealth_text", at_list = [atwealth2, fadeinout2], layer = "screens")
        wealthaddsum = 0
        renpy.hide("wealthg")
        renpy.hide("wealthr")
        
Did I do something wrong or should I try another approach?

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Trying to show a text displayable with a function, but it just shows "0"

#4 Post by trooper6 »

My I ask why you aren't using renpy.notify to show your number?
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

ReDZiX
Regular
Posts: 32
Joined: Thu Jan 04, 2018 12:42 pm
Contact:

Re: Trying to show a text displayable with a function, but it just shows "0"

#5 Post by ReDZiX »

You're kinda declaring your variables twice, one with default, and again in your init python block. Could that be related to the problem? And isn't it better to just use a screen for this?

User avatar
Aviala
Miko-Class Veteran
Posts: 533
Joined: Tue Sep 03, 2013 8:40 am
Completed: Your Royal Gayness, Love Bug, Lovingly Evil
Organization: Lizard Hazard Games
Tumblr: lizardhazardgames
itch: aviala
Location: Finland
Contact:

Re: Trying to show a text displayable with a function, but it just shows "0"

#6 Post by Aviala »

trooper6 wrote: Thu Feb 08, 2018 2:55 pm My I ask why you aren't using renpy.notify to show your number?
Because I didn't know renpy.notify existed :'D It seems like it'd suit my purposes well! I'd have to change the notify code somehow tho, since I want the numbers to be displayed next to the resource that's changing. Not sure how to do that, but I guess I'll look into it? Or create a new screen that's similar to the notify screen and show that instead?
ReDZiX wrote: Thu Feb 08, 2018 5:00 pm You're kinda declaring your variables twice, one with default, and again in your init python block. Could that be related to the problem? And isn't it better to just use a screen for this?
I don't think declaring them twice causes it because the problem was there even before I had declared them twice, I think? I need to declare them twice because this is for a patch for the game so I need to set them with default so that if someone has a save from before the patch they can keep using that save (hopefully). But I think I also need to define them in init or otherwise renpy will show an error message... I think. I'm not 100% sure anymore, but I think that's why I declared them twice.

As for the screens issue, maybe it'd be good to use one? I think I didn't use a screen because ATL doesn't work with screens and I wanted a dissolve effect, but I guess there's some way to use dissolve with a screen since the notify screen seems to be using it. I need to look into it.

User avatar
Aviala
Miko-Class Veteran
Posts: 533
Joined: Tue Sep 03, 2013 8:40 am
Completed: Your Royal Gayness, Love Bug, Lovingly Evil
Organization: Lizard Hazard Games
Tumblr: lizardhazardgames
itch: aviala
Location: Finland
Contact:

Re: Trying to show a text displayable with a function, but it just shows "0"

#7 Post by Aviala »

All right, I got it working! Making a screen for each resource variable was the way to go:

Code: Select all

transform fadeinout2:
    on show:
        alpha 0.0
        linear 0.8 alpha 1.0
    on hide:
        alpha 1.0
        linear 0.8 alpha 0.0

transform atwealth2:
    xalign 0.945 yalign 0.342


def addwealth(wealthaddsum):
        global wealth
        global wealthg
        global wealthr
        global wealthaddsum_variable
        global wealth_text
        wealthaddsum_variable = wealthaddsum
        wealth += wealthaddsum
        if wealthaddsum >= 0:
            renpy.play("sounds/wealth.mp3", channel="sound")
            renpy.show("wealthg", at_list = [atwealth, fadeinout], layer = "screens")
            renpy.show_screen("wealth_text")
        else:
            renpy.play("sounds/wealth2.mp3", channel="sound")
            renpy.show("wealthr", at_list = [atwealth, fadeinout], layer = "screens")
            renpy.show_screen("wealth_text")
        wealthaddsum = 0
        renpy.hide("wealthg")
        renpy.hide("wealthr")

Code: Select all

screen wealth_text:
    if wealthaddsum_variable > 0:
        text "{size=-10}{font=Cinzel-Black.ttf}{color=#ceff93}+[wealthaddsum_variable]{/color}{/font}{/size}" at atwealth2, fadeinout2
    else:
        text "{size=-10}{font=Cinzel-Black.ttf}{color=#f46d44}[wealthaddsum_variable]{/color}{/font}{/size}" at atwealth2, fadeinout2
    timer 2.5 action Hide("wealth_text")
It wasn't beyond my capabilities to make this work, but I just hadn't realized screens were the way to go. Thank you so much for the help, everyone!
Now the text is also red or green depending on if you lose or gain resources so it's pretty neat.

irredeemable
Regular
Posts: 78
Joined: Thu Feb 08, 2018 7:57 am
Contact:

Re: Trying to show a text displayable with a function, but it just shows "0"

#8 Post by irredeemable »

Aviala wrote: Thu Feb 08, 2018 2:31 pm Thanks, but when I changed the function to this I can no longer see the text at all; not even the 0 that showed up before:

Code: Select all

            renpy.show('wealth_t', what=Text("{color=#38ea4a}[wealthaddsum_variable]{/color}", at_list = [atwealth2, fadeinout2], layer = "screens"))
            renpy.show('wealth_te', what=Text("{color=#f46d44}[wealthaddsum_variable]{/color}", at_list = [atwealth2, fadeinout2], layer = "screens"))
        
Did I do something wrong or should I try another approach?
You're not closing the Text() displayable. It should be:

Code: Select all

renpy.show('wealth_t', what=Text("{color=#38ea4a}[wealthaddsum_variable]{/color}"), at_list = [atwealth2, fadeinout2], layer = "screens")

Post Reply

Who is online

Users browsing this forum: No registered users