(Solved) How to Move a Heritage class in an other file ?

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
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

(Solved) How to Move a Heritage class in an other file ?

#1 Post by renardjap » Sat Jul 04, 2020 8:13 am

Hey !
I did a class ResourcesFields in a specific file named ResourcesFields_class.rpy And now I want to create a child class Buildings inheriting from ResourcesFields but I want to place it in an other file (in the same folder).
When I put my child class Buildings in a new file Buildings_class.rpy the game return an error

Code: Select all

## Here we are on a specific file name Buildings_class.rpy
## The ResourcesFields() is in another file name ResourcesFields_class.rpy  in the same folder. The path from the main folder => /class/ResourcesFields_class.rpy

init python:
    class Buildings(ResourcesFields):
            def __init__(self, name, wood_init_cost=0, rock_init_cost=0, iron_init_cost=0, food_init_cost=0, workers = 0):
                ResourcesFields.__init__(self, name, wood_init_cost, rock_init_cost, iron_init_cost, food_init_cost, workers) [code]

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/class/buildings_class.rpy", line 1, in script
    init python:
  File "game/class/buildings_class.rpy", line 3, in <module>
    class Buildings(ResourcesFields):
NameError: name 'ResourcesFields' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/class/buildings_class.rpy", line 1, in script
    init python:
  File "/home/moi/renpy-7.3.5-sdk/renpy/ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/home/moi/renpy-7.3.5-sdk/renpy/python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/class/buildings_class.rpy", line 3, in <module>
    class Buildings(ResourcesFields):
NameError: name 'ResourcesFields' is not defined

Linux-5.4.0-40-generic-x86_64-with-debian-bullseye-sid
Ren'Py 7.3.5.606
 
Sat Jul  4 13:57:42 2020
I think I need to use an Import from but I don't have any idea how it works. I tried this:

Code: Select all

init python:
    from resourcesField_class.rpy import ResourcesFields
    class Buildings(ResourcesFields):
            def __init__(self, name, wood_init_cost=0, rock_init_cost=0, iron_init_cost=0, food_init_cost=0, workers = 0):
                ResourcesFields.__init__(self, name, wood_init_cost, rock_init_cost, iron_init_cost, food_init_cost, workers) 
But it doesn't work either...

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/class/buildings_class.rpy", line 4, in <module>
    from resourcesField_class.rpy import ResourcesFields
ImportError: No module named ResourcesFields

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/class/buildings_class.rpyc", line 3, in script
    ���v��;�>�;W�?Nh��.��#��d���ڦw1|H��A��u��\d�8~V�&�^B�XV1�ۿ~�{�
  File "/home/moi/renpy-7.3.5-sdk/renpy/ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/home/moi/renpy-7.3.5-sdk/renpy/python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/class/buildings_class.rpy", line 4, in <module>
    from resourcesField_class.rpy import ResourcesFields
ImportError: No module named ResourcesFields

Linux-5.4.0-40-generic-x86_64-with-debian-bullseye-sid
Ren'Py 7.3.5.606
 
Sat Jul  4 14:08:14 2020
Any solutions ?
Last edited by renardjap on Sat Jul 04, 2020 9:11 am, edited 1 time in total.

nananame
Regular
Posts: 72
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

Re: How to Move a Heritage class in an other file ?

#2 Post by nananame » Sat Jul 04, 2020 8:20 am

I think the problem here is renpy reading files in its own order so it gets to Buildings before it gets to ResourceFields. I suggest you add a lower init value to resource fields (e.g. init python -1:) that should solve your problem

User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: How to Move a Heritage class in an other file ?

#3 Post by renardjap » Sat Jul 04, 2020 9:11 am

I wrote this

Code: Select all

init 1 python:
And finally it's working!
Thanks for your help

Post Reply

Who is online

Users browsing this forum: Google [Bot], TioNick