Show different sprites depending on a variable?

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
Frynler
Newbie
Posts: 7
Joined: Mon Dec 19, 2016 8:45 am
Contact:

Show different sprites depending on a variable?

#1 Post by Frynler » Mon Dec 19, 2016 9:04 am

Hi there,

You can call me Frynler here, I'm new on this software and also this programming language, as I'm currently learning C#, but since Renpy uses python I'm kind of lost.

Note that this is my first ever attempt at programming something serious so forgive me if i make dumb mistakes.

Now to the problem:

I want to make a somewhat customizable experience, meaning that the player can change clothes. For this, my plan is to have a scene background with the nude player on the drawing, and then on top of it, place the sprites for the clothing.

I tested it in a simple way and it works easily, the problem comes when I try to use the command "show" through a variable instead of the regular way.

Meaning that, for instance:

image clothing1 = "images/clothing1.png"

show clothing1


^This works, but if I plan to add a lot of clothing variation, it can be a pain in the ass to make so many lines, as I plan to let the player customize every single part of the clothing, from shoes to complements. This way is just not viable.

So, my idea was to make a simplification, have some kind of list of the clothing that the player has currently available to choose from, something like:

$ wardrobe = ["tshirt","jeans"]

and so on.

Then, I would have another variable for each part of the body, something like:

$ Torso
$ Legs
% Hats

etc.

Then, my plan was to make those small variables the ones that changed over all the scenes instead of having to write all the possibilities each time.

Like, if a player choose to wear jeans, it would be like:


image jeans = "images/jeans.png"

$ Legs = jeans

show Legs


This way all the "show" commands would be equal along all the game, only having to change the variables depending on the clothing the player choose.

However, this doesnt seem to work, I tried many different ways, but I just don't seem to know how this language works. Is it even possible to do that?

The best result i could achieve is for the game to display the text of the variable instead of its content. Its like when i put "show" it doesnt recognize its a variable and instead just uses its text, is there any way to tell the engine that its a variable and that instead should use its content for the "show" command?

Or is there a different way to do this? I'm really lost since im new to this language, so any help is welcome.

Hope I explained myself well, thank you beforehand!

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Show different sprites depending on a variable?

#2 Post by nyaatrap » Mon Dec 19, 2016 9:31 am

There are two ways: Combination of DynamicImage and Fixed, or DynamicDisplayable.

Fixed is a way to layout layers. For example:

Code: Select all

image figure = Fixed("firstlayer.png", "secondlayer.png", fit_first=True)
DynamicImage is explained like:
Strings may have one or more square-bracket substitutions in them, such as "eileen [mood]" or "eileen_[outfit]_[mood].png". When such a string is given, a dynamic image is created. A dynamic image has text interpolation performed at the start of each interaction (such as say statements and menus). The resulting string is processed according to the rules above.
So you could:

Code: Select all

define legs = "boots.png" #filename
define body = "shirt.png"
image figure = Fixed(DynamicImage("[legs]"), DynamicImage("[body]"), fit_first=True)
Another way is DynamicDisplayable, but it needs a bit of python coding.
Document about them is here: https://www.renpy.org/doc/html/displayables.html

Frynler
Newbie
Posts: 7
Joined: Mon Dec 19, 2016 8:45 am
Contact:

Re: Show different sprites depending on a variable?

#3 Post by Frynler » Mon Dec 19, 2016 9:50 am

nyaatrap wrote:There are two ways: Combination of DynamicImage and Fixed, or DynamicDisplayable.

Fixed is a way to layout layers. For example:

Code: Select all

image figure = Fixed("firstlayer.png", "secondlayer.png", fit_first=True)
DynamicImage is explained like:
Strings may have one or more square-bracket substitutions in them, such as "eileen [mood]" or "eileen_[outfit]_[mood].png". When such a string is given, a dynamic image is created. A dynamic image has text interpolation performed at the start of each interaction (such as say statements and menus). The resulting string is processed according to the rules above.
So you could:

Code: Select all

define legs = "boots.png" #filename
define body = "shirt.png"
image figure = Fixed(DynamicImage("[legs]"), DynamicImage("[body]"), fit_first=True)
Another way is DynamicDisplayable, but it needs a bit of python coding.
Document about them is here: https://www.renpy.org/doc/html/displayables.html
Woah, thanks for the quick answer!

That looks quite easy and simple yet it will still give me a pain in the ass to get it right because im so new at programming!

But hey, that's how we all learn! I will try this later and comment if it worked out!

EDIT: Alright, I tried it, took me a bit to understand what the code you wrote was and that it did, but eventually I made it, and it works!

The only thing left for me to know now is, how can i change the value of the defined variables?

i tried definning them again with a different file like:

define legs = "jeans.png"

and then

define legs = "shorts.png"

but it did nothing, set command doesnt seem to do it neither.

Im gonna keep looking, but in the meantime if you see this and know the answer, that would surely save me a lot of headache!

While im at it, im gonna ask this too: I was testing the imagemap thing to have an image which you can click and it will jump you to different labels to progress, like some kind of map.

However, i would like that the buttons did more than just jumping to another label when pressed, like changing a variable of money for instance., how can I do multiple actions with that button click?

hotspot (803, 63, 167, 163) clicked Jump ("test"), $money += 10

tried adding that last part but as usual it doesnt work.

Whatever i dont make from a guide doesnt work :(

Thanks beforehand!

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Show different sprites depending on a variable?

#4 Post by nyaatrap » Mon Dec 19, 2016 12:17 pm

I never use imagemap because it's not efficient, so I skip to answer about it.
Then about define, document is here: https://www.renpy.org/doc/html/python.h ... -statement
basically, define is same to "init python" - run python codes in initial phase before the game starts.
so

Code: Select all

define A=B
label start:
    $ A=B
is same thing, but define runs before starting game. after game starts, use $ or python.
Last edited by nyaatrap on Mon Dec 19, 2016 12:28 pm, edited 1 time in total.

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Show different sprites depending on a variable?

#5 Post by nyaatrap » Mon Dec 19, 2016 12:24 pm

However, i would like that the buttons did more than just jumping to another label when pressed, like changing a variable of money for instance., how can I do multiple actions with that button click?

hotspot (803, 63, 167, 163) clicked Jump ("test"), $money += 10
It's clicked [SetVariable("money", money+10), Jump("test")]. Document here: https://www.renpy.org/doc/html/screen_actions.html

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Show different sprites depending on a variable?

#6 Post by nyaatrap » Mon Dec 19, 2016 12:30 pm

nyaatrap wrote:I never use imagemap because it's not efficient, so I skip to answer about it.
Then about define, document is here: https://www.renpy.org/doc/html/python.h ... -statement
basically, define is same to "init python" - run python codes in initial phase before the game starts.
so

Code: Select all

define A=B
label start:
    $ A=B
is same thing, but define runs before starting game. after game starts, use $ or python.
Sorry, I mistake. define always defines variable when game loaded, so any changes after game starts are nullified. There's default statement to prevent this thing happenes, but just define all variables after game start without using define statement might be simpler.

Code: Select all

default A=B
label start:
    $A=B
is correct, but you can ignore define/default if it's hard to understand.

Frynler
Newbie
Posts: 7
Joined: Mon Dec 19, 2016 8:45 am
Contact:

Re: Show different sprites depending on a variable?

#7 Post by Frynler » Mon Dec 19, 2016 1:40 pm

nyaatrap wrote:
However, i would like that the buttons did more than just jumping to another label when pressed, like changing a variable of money for instance., how can I do multiple actions with that button click?

hotspot (803, 63, 167, 163) clicked Jump ("test"), $money += 10
It's clicked [SetVariable("money", money+10), Jump("test")]. Document here: https://www.renpy.org/doc/html/screen_actions.html
Damnit! I was doing it right! All that i was missing were the brackets! T-T

I noticed that this text editor is VERY picky, like even a tabulation makes the compiler to not want to do his job :(

C# is picky too but not this much!

Still, thanks to you i got the clothings thing done.

However, when i put the code you mentioned, it keeps giving me an error on the hotspot about expecting a colon or an end of line. No matter what I put now it keeps giving me that error, any idea of why is that? It's very frustrating, I don't know if its something as dumb as a missing colon or just completely messed up code :(

screen test:
imagemap:
ground "images/Scene_1.png"
hover "images/Scene_2.png"
hotspot (803, 63, 167, 163), clicked Jump("test")
#[SetVariable(money, money+10), Jump("test2")]


There is my code, i cut out that last part to check if that was the problem, but it still doesnt work.

It worked before but for some reason it wont work again now, what is wrong here? I really dont know! :( I've been testing stuff that could be the reason for that compiling error with no luck, everything seems to be in order to me!

Im gonna keep looking, maybe even rewrite it completely.

EDIT: Nevermind! It was the freaking colon after the coordenates!

I would swear that colon was always there and it worked before, but i guess that was just my imagination, i've been messing up with this all evening so my head is pretty dense lol, apologies.

Now I would like to know how to get and change elements from a list, but i think i can figure it out on my own with the documentation i got now.

If for god's sake cant do it on my own i will come back to ask again, it seems that im gonna learn to programm the hard way, damn.

ALTERNATIVE METHOD!

Here is an alternative method for imagemaps to get the buttons to do different things, in a way more versatile and easy way (for me at least.)

screen test2:
imagemap:
ground "images/Scene_1.png"
hover "images/Scene_2.png"
hotspot (803, 63, 167, 163) clicked Return("prueba")


First we make the imagemap and instead of clicked, we use Return("YourText")

call screen test2
$ result = _return
if result == "prueba":
"wut"


Then when we call that screen, we check all the possibilities for return with a conditional, and then we are able to do whatever we want from there with that info. This is way easier for me since i can make menus, other conditionals, etc. without having to worry about those damn brackets :(

This method is probably worse when you plan to have loooots of things on the imagemap, but its not my case exactly.
Hope someone with my same problem finds that useful!

Thank you very much for your help, you were very kind and helpful! Cheers to you!

Post Reply

Who is online

Users browsing this forum: No registered users