[Code] How to declare images within python code? [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
Therozin
Newbie
Posts: 5
Joined: Mon May 02, 2016 4:46 am
Github: Therozin
Contact:

[Code] How to declare images within python code? [SOLVED]

#1 Post by Therozin » Mon May 02, 2016 5:40 am

Hello,

I apologize in advance for grammar errors, English isn't my first language.

I've been working on a some basics tools to help me in creating a visual novel, but I have troubles to overcome one difficulty. I'm currently trying to create a python class that would automatically scan and index the images for both backgrounds and characters, but I miss one crucial information. First, I'll explain how my system works, and then expose the problem I'm facing. Don't bother about the python code behind the commands or how to implement them, I'll take care of it.

The Idea.

I've already created three classes for the differents entities I'll be using (code can be found here: http://pastebin.com/qGxAjnxE, but isn't complete nor needed to understand.). The thing to remember here is that I can use the say statement using this form:

Code: Select all

t = NPC("Thierry", "#A46BBD", True)
t.who "My name's [t.name]"
Now I'm working on a class called "ImageManager" that has to scan every folder and subfolder inside the graphic_ressources folder, and then index the differents ressources contained inside. For example, let's say that I've got this data structure:

Code: Select all

Images
----|Characters
--------|Thierry
-----------|thierry_happy.png
-----------|thierry_sad.png
--------|Hans
----|Backgrounds ...
Then I perform this command

Code: Select all

t = NPC("Thierry", "#A46BBD", True)
t.update_images()
This command should call the ImageManager class to do the work asked. It should scan the Images/Characters/Thierry folder and index the two images contained within it.

The problem.

The problem is that I don't know how to add the images using python code. My first idea was to stock every image linked to an entity for example inside a dictionnary. I could have something like this:

Code: Select all

t_dictionnary = {"happy" = "images/t_happy.png", "sad" = "images/t_sad.png"}
And then I would be able to do the following statement within renpy:

Code: Select all

show t.who t.images.happy
But I have no idea of how I can link an image to a entity within python code. The question is therefore: is there a way to implemant this command in a python way? I've search through the renpy code in Github but didn't found anything useful:

Code: Select all

FROM:
define t = Character("Thierry", color = "#FABA62", show_two_window=True)
image t angry = "images/characters/thierry/angry_thierry.png"

TO SOMETHING LIKE THIS:
init python:
    t = entity_class(Character("Thierry", color = "#FABA62", show_two_window=True))
    t.add_image("angry", "images/characters/thierry/angry_thierry.png")
I hope I am clear, if there's any question, I'll answer as quickly as possible. Thank your for your time!
Last edited by Therozin on Mon May 02, 2016 10:18 am, edited 1 time in total.

drKlauz
Veteran
Posts: 237
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: [Code] How to declare images within python code?

#2 Post by drKlauz » Mon May 02, 2016 7:22 am

renpy.image to define image in python code, you can see how it works in renpy/common/00images.rpy
DynamicImage to show character, combined with character state inside entity

Code: Select all

#inside class init
  renpy.image('Thierry Angry','images/characters/thierry/angry_thierry.png')
  renpy.image('Thierry Happy','images/characters/thierry/happy_thierry.png')
  self.Image=DynamicImage('Thierry [Thierry.Mood]')

#in script
$Thierry.Mood='Angry'
show Thierry.Image
I havn't tried this code, just my first sight approach.

With right file names, you can skip manual image definition and use renpy autodefinition

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: [Code] How to declare images within python code?

#3 Post by xela » Mon May 02, 2016 7:55 am

Why define the images at all?

Code: Select all

self.Image = DynamicImage('images/characters/[self.character_name]/[self.mood].png')

Code: Select all

Thierry.mood = "happy"
show Thierry.Image
(or)

Code: Select all

show expression Thierry.Image
not sure which one is more appropriate here. It might work...
Like what we're doing? Support us at:
Image

User avatar
Therozin
Newbie
Posts: 5
Joined: Mon May 02, 2016 4:46 am
Github: Therozin
Contact:

Re: [Code] How to declare images within python code?

#4 Post by Therozin » Mon May 02, 2016 10:17 am

Thanks you for your answers, I tried your ideas and it worked.
Why define the images at all?
Indeed, it isn't a good idea. I'll be using memory without having any need to. Thanks you for your advice.

Post Reply

Who is online

Users browsing this forum: Google [Bot]