A little problem with instances[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
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

A little problem with instances[SOLVED]

#1 Post by DannyGMaster »

I'm trying to make an Elemental Affinity system for use in a battle system, where every character and every skill have their own elemental properties (something like Chrono Cross).

I have some basic knowledge of Python, but it stills doesn't seem enough as this is probably a simple error but I just can't figure out (or remember) a workaround for this.

Code: Select all

init python:
    
    #This is the element class, here I create some attributes for every element.
    # Element resistances, weaknesses, etc. are passed as a list to be able to
    # give an element more than one weakness or resistance.
    class Element(object):
    
        def __init__(self, name, resist=[], weak=[]):
            self.name = name
            self.resist = resist
            self.weak = weak

    #Element instances
    neutral = Element(name='Neutral', resist=[], weak=[]) 
    water = Element(name='Water', resist=[fire], weak=[]) #Bug ocurrs here, because fire isn't defined yet
    fire = Element(name='Fire', resist=[], weak=[water])    
# But if i change the order, the same would occur, as water would not be defined.
I'm missing a step or something, can anybody help?
Last edited by DannyGMaster on Thu Jun 08, 2017 2:00 pm, edited 1 time in total.
The silent voice within one's heart whispers the most profound wisdom.

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

Re: A little problem with instances

#2 Post by xela »

You can use strings instead of Element instances. It's touch to use an instance before it actually exists...

Doesn't look like it's gonna be an issue here, but keep this in mind: https://twitter.com/raymondh/status/822296239156920321
Like what we're doing? Support us at:
Image

User avatar
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

Re: A little problem with instances

#3 Post by DannyGMaster »

Thanks, I figures it would be better with strings.

And thanks for warning me of the empty lists, it does look cleaner the way raymond suggests
The silent voice within one's heart whispers the most profound wisdom.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: A little problem with instances

#4 Post by Remix »

Also remember that when you do

Code: Select all

reference_name = Element(arguments)
you are instantiating an object with that reference, so anything later in the code that uses that reference is just pointing at that single one instance of the object.

Think of it like making some word docs in one global folder, calling one of them fire.doc (fire.doc = new WordDoc(contents) ) means you cannot have a second fire.doc unless you use a different pointer/name such as fire2.doc. Even if the contents of the .docs are the same, their filenames/references have to be different... so, if something is weak to fire.doc it does not automatically become weak to fire2.doc.
Hope that kind of made sense.

Also, try to think of how and when you will need the objects, it should help you design the class nicely.... e.g. (if you wanted a name and type)
enemy1 = Element('Bob the Firey Enemy', 'fire', ... some data)
friend1 = Element('Drippy the umm drippy watery friend', 'water')

On an aside, you could extend the classes using a base class and children inheriting from that...

Code: Select all

class Element(object):
        def __init__(self, name):
            self.name = name
            self.type = None
            self.resist = []
            self.weak = []

        # this method also appears in any child class based on Element
        # Most methods (functions within classes) will appear in this parent class
        def test_resist(self, attacker_type):
            return attacker_type in self.resist

class FireElement(Element): # Class based on Element 
        def __init__(self, name):
            # This line basically just calls the parent __init__ as though it existed in this object
            # It would set self.name then set up empty .type, .resist and .weak variables
            super(FireElement, self).__init__(name)
            # We can then amend them after we call it to make objects of this class slightly different
            self.type = 'fire'
            self.resist = ['fire', 'lava']
            self.weak = ['water', 'ice']

# then define IceElement, WaterElement... etc

enemy1 = FireElement('Bob the pest')   
resisted = enemy1.test_resist('lava')
Typed on the fly, ergo untested.
Hopefully it gives some ideas for possible options though.

User avatar
DannyGMaster
Regular
Posts: 113
Joined: Fri Sep 02, 2016 11:07 am
Contact:

Re: A little problem with instances

#5 Post by DannyGMaster »

Wow, @Remix, that could actually be way better, thank you too!
The silent voice within one's heart whispers the most profound wisdom.

Post Reply

Who is online

Users browsing this forum: elcharlo