Can I modify a global object inside a CDD?

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
newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Can I modify a global object inside a CDD?

#1 Post by newbiemate »

I know for screens, we should NOT be modifying any kind of data. ie,

Code: Select all

# THIS IS BAD
screen bad_screen(offset):
    $ hp = hp + offset
    text "%s" % hp
Are there any rules for Creator Defined Displayables? I have an object that I create globally, and then passed to my cdd. The cdd will modify the state of that object inside the events()

ie,

Code: Select all

init 1 python:

    class Character():
        def __init__(self):
            self.hp = 100

    player = Character()
     
    class MyDisplayable(renpy.Displayable):
    
        def __init__(self, player):
            # ....
            self.player = player

        def event(self, ev, x, y, st):
            if ev.type == some_event:
                self.player.hp = self.player.hp + 1000
                

screen doit():
    add MyDisplayable(player)
So basically if some event happens inside the cdd, it will modify the player's hp. I plan on having more complex interactions, but this is the gist of it.

Is this a valid way of modifying some global object from inside the cdd? If not, what would be the correct way, if it's even possible? Will this cause side-effects like with the "bad_screen" code in my first example?

Post Reply

Who is online

Users browsing this forum: Google [Bot]