[Solved] Why can't I declare this object using define?

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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] Why can't I declare this object using define?

#1 Post by henvu50 »

EDIT: Alex was right. The class needs to be inside an init -1 python block like this:

Code: Select all

#THIS FINALLY WORKS... Make sure the class is within a -1 python block?! This makes no sense. What a nightmare. Shoot me now.
define people32 = [
            aPerson("Bob", 34),
            aPerson("Zoe", 53),
        ]

init -1 python:
  
        class aPerson():
        def __init__ (self, str_name, str_age):
            self.s_name = str_name
            self.age = str_age
Last edited by henvu50 on Sat Jun 12, 2021 9:07 am, edited 2 times in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Why can't I declare this object using define?

#2 Post by Alex »

When defining 'triggerEventTest' try to set init offset greater than 0 (not tested but the issue might be the order of running lines of code).

https://www.renpy.org/doc/html/python.h ... -statement

https://www.renpy.org/doc/html/python.h ... -statement

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: Why can't I declare this object using define?

#3 Post by henvu50 »

EDIT: Thanks, you were right. The class needed to be inside an init -10 python block like this:

Code: Select all

#THIS FINALLY WORKS... Make sure the class is within a -10 python block?! This makes no sense. What a nightmare. Shoot me now.
define people32 = [
            aPerson("Bob", 34),
            aPerson("Zoe", 53),
        ]

init -10 python:
  
        class aPerson():
        def __init__ (self, str_name, str_age):
            self.s_name = str_name
            self.age = str_age
Last edited by henvu50 on Sat Jun 12, 2021 7:13 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Why can't I declare this object using define?

#4 Post by Ocelot »

henvu50 wrote: Sat Jun 12, 2021 5:44 am I need to find an example of someone declaring objects like this as define.
Here is an example of (very heavily cut) event system:

Code: Select all

init -10 python:
    class Event:
        def __init__(self, day, to_label=None, callback=None):
            self.day = day
            self.to_label = to_label
            self.callback = callback

        def possible(self, day):
            return self.day <= day

        def process(self):
            if self.to_label:
                renpy.call(*self.to_label)

        def finalize(self):
            if self.callback:
                args = self.callback[1:]
                self.callback[0](*args)

    class PendingEvents:
        def __init__(self, *events):
            self.events = []
            self.events.extend(events)

        def possible_events(self):
            return [event for event in self.events if event.possible(current_day)]

        def random_event(self):
            event_list = self.possible_events()
            event = renpy.random.choice(event_list)
            self.events.remove(event)
            return event

define main_events = PendingEvents( Event(1, to_label=("training",)), Event(2, callback=(do_stuff, "Hi")) )

init -10 python:
    def do_stuff(line):
        narrator(line)

default current_day = 1

label training:
    "Train"
    return

label start:
    $ current_event = main_events.random_event()
    $ current_event.process()
    $ current_event.finalize()
    $ current_day = 2
    $ current_event = main_events.random_event()
    $ current_event.process()
    $ current_event.finalize()
    return
< < insert Rick Cook quote here > >

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: Why can't I declare this object using define?

#5 Post by henvu50 »

Ocelot wrote: Sat Jun 12, 2021 6:57 am
henvu50 wrote: Sat Jun 12, 2021 5:44 am I need to find an example of someone declaring objects like this as define.
Here is an example of (very heavily cut) event system:

Code: Select all

init -10 python:
    class Event:
        def __init__(self, day, to_label=None, callback=None):
            self.day = day
            self.to_label = to_label
            self.callback = callback
.....
    return
.....................................
Oh, so Alex was right! I had to use init -10 python to get the define to declare.

Man, you're into some black belt level python. Can't wait to start studying what you shared. Thanks!

Post Reply

Who is online

Users browsing this forum: Google [Bot]