[ RESEOLVED ] Renp'y Classes / Game objects

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
Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

[ RESEOLVED ] Renp'y Classes / Game objects

#1 Post by Thundy »

Hey Everyone.

I'm still learning renpy and my background is mainly c#, c++ and back in the day when they were still relevant Pascal and Basic.

Im trying to work out if object oriented programming is possible in renpy as all the guides that I've seen for some of the things I want to code seem to use nested arrays which to me seems like messy coding.

Is it possible to define a class in Renpy in the same way that I would do in another language and then instantiate it? for example if I wanted to define an NPC in another compiler all i would do is define the NPC class with a set of properties such as level, Hit points, attack power and then just instantiate it for every npc I cant work out how to do that in renpy,

any help would be greatly appreciated
Last edited by Thundy on Fri Dec 29, 2017 10:52 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1910
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Renp'y Classes / Game objects

#2 Post by philat »

Sure. It's all python. General example: viewtopic.php?f=51&t=44730

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Renp'y Classes / Game objects

#3 Post by Thundy »

awesome thanks :D

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Renp'y Classes / Game objects

#4 Post by Thundy »

[Deleted]
Last edited by Thundy on Fri Dec 29, 2017 6:17 am, edited 1 time in total.

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Renp'y Classes / Game objects

#5 Post by Thundy »

Ok so it turns out my array wasnt working properly. THe code was compiling but threw an exception later on

here is the code im trying to make work

Code: Select all

   python:
        class quest(object):
            def __init__(self, title, description, isActive, isComplete):
                self.title = title
                self.description = description
                self.isActive = isActive
                self.isComplete = isComplete
                
    $ goals[50] = quest('','', False, False)
                
    $ goals[1].title ="No place like home"
    $ goals[1].descrtiption="Find somewhere for your family to live"
    $ goals[1].isActive=True
    $ goals[1].isComplete=False
and then accessing it

Code: Select all

                    for i in goals:
                        if goals[i].isActive:
                            if goals[quest].isComplete:
                                text "{alpha=0.2}[goals[i].title]{vspace=12}[goals[i].description]{/alpha}{vspace=25}"
                            else:
                                text "[goals[i].title]{vspace=12}[goals[i].description]{vspace=25}"

Ive removed most of the text formatting to make it easier to read

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: Renp'y Classes / Game objects

#6 Post by Remix »

if goals[quest].isComplete:

I think you wanted goals[ i ] there, especially as quest is a mutable class object so cannot be a dictionary key... unless you have also defined quest as a variable name, which would not be an advised route

Personally, rather than using goals[ i ] all over the place, maybe

for q in goals.values(): # or goals.items() ... if goals is a dictionary (which I presume from the goals[ i ] bits)
or
for i,q in enumerate( goals ): # i = index, q = value (aka a quest object)... this is if goals is a list and not a dict

Also remember/note: dictionary keys are unsorted... for i in goals is not guaranteed to return them in the same order each time

Hard to say more without seeing more code
Frameworks & Scriptlets:

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Renp'y Classes / Game objects

#7 Post by Thundy »

{Deleted}
Last edited by Thundy on Fri Dec 29, 2017 10:10 am, edited 1 time in total.

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Renp'y Classes / Game objects

#8 Post by Thundy »

So my new declaration is as follows

Code: Select all

python:
        class quest(object):
            def __init__(self, title, description, isActive, isComplete):
                self.title = title
                self.description = description
                self.isActive = isActive
                self.isComplete = isComplete
                
        goals = [ ]
        
        goals.append(quest("No place like home", "Find somewhere for your family to live", True, False))
Which works. I was being a bonehead with my array declaration. I can now just add quests to the array but the function that reads them is throwing up an exception about indices needing to be integer and not unicode.

Code: Select all

for i,q in enumerate(goals):
                        if goals[i].isActive:
                            if goals[i].isComplete:
                                text "[goals[i].title]{vspace=12}[goals[i].description]"
                            else:
                                text "[goals[i].title][goals[i].description]"
the exception is in the text lines

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Renp'y Classes / Game objects

#9 Post by Thundy »

OK sorry for bumping my own thread but i solved my issue

The solution was simple enough, it was mostly me being stupid

the working code is as follows. please feel free to use it you stumbled on this trying to figure it out

Code: Select all

   python:
        class quest(object):
            def __init__(self, title, description, isActive, isComplete):
                self.title = title
                self.description = description
                self.isActive = isActive
                self.isComplete = isComplete
                
        goals = [ ]
        
        goals.append(quest("No place like home", "Find somewhere for your family to live", True, True))
                
and then

Code: Select all

                    for i,q in enumerate(goals):
                        if q.isActive:
                            if q.isComplete:
                                text "[q.title]{vspace=12}[q.description]{vspace=25}"
                            else:
                                text "[q.title]{vspace=12}[q.description]{vspace=25}"
obviously text formatting will be used to make the completed quest look different from the uncompleted one.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: [ RESEOLVED ] Renp'y Classes / Game objects

#10 Post by Milkymalk »

Glad you figured it out!

My advice is to say goodbye to the word "array". Like you, I came from Basic and Pascal and this word as well as "record" (shudder) kept me from understanding data structured in Python.

There are no arrays, there are only lists. ;)
And dictionaries, And tuples. And sets.

Lists basically work like arrays, but they bring their own methods to manipulate them. Also, arrays in Basic and Pascal need to be declared and have a fixed length. A list is the most flexible way to store data that I have ever seen.

Also, make it a habit to capitalize class names, it's how it's usually done. That way you and everybody who reads your code will know that "Quest" is the class name and "quest" is just one instance of that class (if you decide to do "quest = Quest()").

You don't need to enumerate if you never use that number. Just do:

Code: Select all

for q in goals:
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: [ RESEOLVED ] Renp'y Classes / Game objects

#11 Post by Thundy »

Thanks Milky. Sorry for the slow reply. life etc

Post Reply

Who is online

Users browsing this forum: No registered users