renpy.random.randint in a python Class?

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.
Message
Author
User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

renpy.random.randint in a python Class?

#1 Post by jack_norton »

Is not possible to use it? I always get an error message:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While executing init code:
  File "game/engine.rpy", line 1, in script
    init python:
  File "game/engine.rpy", line 155, in python
            Hero[i].LevelUP()    
  File "game/engine.rpy", line 92, in python
                self.Attack=renpy.random.randint(1,100); self.Accuracy=renpy.random.randint(1,100); self.Defense=renpy.random.randint(1,100); self.Evasion=renpy.random.randint(1,100); 
AttributeError: 'NoneType' object has no attribute 'random'

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

Full traceback:
  File "C:\- indie dev -\games\renpy\renpy\execution.py", line 261, in run
  File "C:\- indie dev -\games\renpy\renpy\ast.py", line 630, in execute
  File "C:\- indie dev -\games\renpy\renpy\python.py", line 960, in py_exec_bytecode
  File "game/engine.rpy", line 155, in <module>
  File "game/engine.rpy", line 92, in LevelUP
  File "random.pyo", line 228, in randint
  File "random.pyo", line 202, in randrange
  File "C:\- indie dev -\games\renpy\renpy\python.py", line 436, in random
AttributeError: 'NoneType' object has no attribute 'random'

Windows-post2008Server-6.1.7601-SP1
Ren'Py 6.12.1.1501
 
my code is supersimple:

Code: Select all

    class HeroClass(object):
        def __init__(self):
            self.Level=0; self.Class=0; self.SkPt=0; self.Skills=[]; self.JustLeveled=False; self.Effects=[ ]
            self.HP=0; self.SP=0; self.Stats=[0,0,0,]; self.Attack=0; self.Accuracy=0; self.Defense=0; self.Evasion=0; 
            self.tmpHP=0; self.tmpSP=0; self.tmpStats=[0,0,0,]; self.tmpAttack=0; self.tmpAccuracy=0; self.tmpDefense=0; self.tmpEvasion=0;
        def LevelUP(self):
            self.Level+=1;self.SkPt=3;self.JustLeveled=True;
            if self.Class==0:#warrior
                self.HP=100;self.SP=50;hb=3;sb=1
            elif self.Class==1:#thief
                self.HP=70;self.SP=70;hb=2;sb=2
            elif self.Class==2:#mage
                self.HP=50;self.SP=100;hb=1;sb=3
            self.HP+=self.Stats[0]*self.Level*hb;self.SP+=self.Stats[2]*self.Level*sb;
            self.tmpHP=self.HP;self.tmpSP=self.SP;
            self.Attack=renpy.random.randint(1,100); self.Accuracy=renpy.random.randint(1,100); self.Defense=renpy.random.randint(1,100); self.Evasion=renpy.random.randint(1,100); 
is not possible to assign a random number inside a Class? seems strange to me :oops:
follow me on Image Image Image
computer games

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: renpy.random.randint in a python Class?

#2 Post by Anima »

No, that's definitely possible. I used it just today.

This is the line that causes the problem in Ren'Pys source code.

Code: Select all

renpy.game.log.current.random.append(rv)
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: renpy.random.randint in a python Class?

#3 Post by jack_norton »

True, I created a new project and works. Ugh I wonder what the hell I did in my code to trigger that?
LOL I commented that line in python.py and now works fine :D but I suspect is not the right way to do it!
follow me on Image Image Image
computer games

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: renpy.random.randint in a python Class?

#4 Post by PyTom »

The problem here is that renpy.random.randint only makes sense once a game has started. You're running it during the init phase, which is a bad idea - who wants to deal with a game that initializes differently each time? How would save games work in that situation?

If you really want to, you can import random and use random.randint - but it's a bad idea, honest.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: renpy.random.randint in a python Class?

#5 Post by jack_norton »

Uhm, ok so a stupid question from someone that knows little of python classes... that function deals with levelup of character in a RPG. How can I have it work inside the class but not during init ?
I mean, the levelup is not called on init, during the game I wanted to simply do: Hero1.LevelUP() but since I must define the class on init python... ?
I am a bit confused :D probably is not possible at all?
follow me on Image Image Image
computer games

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: renpy.random.randint in a python Class?

#6 Post by Anima »

According to the traceback, you did call the LevelUp method during the init phase.
You might take a look at line 155 in your engine file and move the method call to a label.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: renpy.random.randint in a python Class?

#7 Post by jack_norton »

Ahh yes!! LOL thanks, that's exactly the reason :oops: now should work just fine!
follow me on Image Image Image
computer games

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: renpy.random.randint in a python Class?

#8 Post by jack_norton »

Ok I have one last problem, I'm about to give up using Classes :D When I do shift+R to reload, the values are reset to the __init__ of the class, even if I assign different ones through the game. What I'm missing ?
follow me on Image Image Image
computer games

User avatar
Sexo Grammaticus
Regular
Posts: 69
Joined: Thu Jun 09, 2011 1:54 am
Projects: human instrumentality and vocals section
Location: butts
Contact:

Re: renpy.random.randint in a python Class?

#9 Post by Sexo Grammaticus »

...nothing? Init values are set when the class is first created, so it makes perfect sense that reloading the script (which is presume is unloading everything and then re-starting the python instance) would re-default them back to whatever you've set them in the __init__.
i have no signature and i must obscure game reference

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: renpy.random.randint in a python Class?

#10 Post by Anima »

First thing first, don't ever give up on classes. You'll regret it, seriously. :wink:
About your problem, I'm not sure. Did you call your constructor in the init phase or in a label?
Without seeing the code, I can only guess. But classes work, I assure you. (And I'm kinda scared thinking that you made Planet Stronghold without them.)
Sexo Grammaticus wrote:...nothing? Init values are set when the class is first created,
Slight correction, init values are set when an instance of the class is created, not the class itself. Sorry, for the nitpick.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: renpy.random.randint in a python Class?

#11 Post by jack_norton »

Planet Stronghold is a big mess of lists and arrays :D
As said several times I'm really not a good coder but I work fast so manage to make complex games anyway...

About the problem, here is the code (warning a BIG MESS):
game.zip
(36.61 KiB) Downloaded 85 times
Maybe the problem is how I used the class?

Code: Select all

    for i in range(50):
        Hero.append(HeroClass())
I don't want to do:
Loren=HeroClass()
Elenor=HeroClass()

and so on since later in the game I might want to use for i in range(len(Party)) to make "bulk" operations on many characters!
But I cannot believe there isn't a method so that when I reload, I can still keep the character actual HP/SP... debugging would be a nightmare!
follow me on Image Image Image
computer games

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: renpy.random.randint in a python Class?

#12 Post by Anima »

Okay, mess is an understatement. Where did you reload?

For your second problem.
Throw all your character objects in a list and run the for loop.

Code: Select all

heroes = [ABC(), DEF(), GHI()]
for entry in heroes:
    entry.takeDamage(5)
Besides, you may want to make separate python-classes for each character and make them python-subclasses of their character-classes python-class.

No wonder you found Planet Stronghold to be a nightmare. I'm still hesitant to believe it.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: renpy.random.randint in a python Class?

#13 Post by jack_norton »

I reloaded just after the start label, I put a loop so every time you "exit" the character info screen, it levels Up. If I reload while in it, resets value to zero (I see level 0, HP 0, SP 0 etc).

Why do you suggest to have a class for every character?

My problem is that I've read Classes related docs, but perhaps because of language barrier, or because I don't get it, the only way for me to understand something is to see source code. Really I should have been just a game designer! :D
(one day will have enough money to just hire coders, I swear)
follow me on Image Image Image
computer games

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: renpy.random.randint in a python Class?

#14 Post by Anima »

Well, it's not strictly necessary, but it does straighten up the code a bit.
Since all your characters have a unique skill tree, and therefore are pretty unique themselves, bundling up all that unique code in a class seems like a good idea.
The classes would look like this.
The classes would look like this.
class-hierarchy.png (11.57 KiB) Viewed 3949 times
The classes would look like in the attached picture. The char class would hold all the attributes and methods every character, player or enemy needs.
The player class would hold all the additional methods only a player character needs, for example the level Up stuff.
And finally the individual classes would hold things like the skill tree and the individual attributes, for example the picture files.

The actual character class could either be realised by inserting another class between the player and individual level or by using a side class. On the enemy side, introducing an additional level would also make sense, to group enemies that use the same ai patterns for example.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: renpy.random.randint in a python Class?

#15 Post by jack_norton »

Well, I understood "the theory" and makes sense. But I have no idea how to put this into practice :D
Thanks also about the PM with code example!
follow me on Image Image Image
computer games

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]