[Solved]Method not returning prompt

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
Killverin
Newbie
Posts: 10
Joined: Wed May 25, 2022 4:45 pm
Contact:

[Solved]Method not returning prompt

#1 Post by Killverin »

Hey. I'm pretty new and I'm trying to get my Item class's Get method to do a weight check on the player's inventory and return an error if overweight or if the item would put them over. Everything else works but I cant get the method to return a prompt, and I think it's because I'm trying to use Renpy code in the Python block. I tried a few other ways but here we are. Been having fun figuring out things out so far, but I don't know what I don't know about this one, lol.

Here's my script. The hashtags are the temp fix, showed me that the logic works outside of the class method at least.

$ Testing = True
label testing:
while Testing:
menu:
"[MCR.name]'s weight is [MCR.crnt_weight] and her current Mag Cell number is [MCRI.crnt_mag_cell]"
"Pick up Mag Cell.":
#if MCR.crnt_weight + MagCell.weight > MCR.max_weight:
#n "You can't carry anymore weight."
$ MagCell.Gain()

jump testing
This Is my Method. When ran, everything works as though the weight checking lines aren't there. The last hashtag is me trying to figure how, if the item class's ID attribute matches the inventory class's attribute's name, I can make the method handle the two classes interactions without my input. That's not the point of this post, but pointers on that would be gravy :P
class Item(object):
def __init__(self, name, val, weight, ID, amount):
self.name = name
self.val = val
self.weight = weight
self.ID = ID
self.amount = amount

def Gain(self):
if MCR.is_active:
if MCR.crnt_weight + self.weight > MCR.max_weight:
print "You' can't carry anymore weight." #Check

else:
MCR.crnt_weight += self.weight
MCRI.crnt_mag_cell += 1 #(MCRI.crnt_[self.ID]) ?????? #Check bad method code
return

return
I'm sure I'm going about this all wrong. Any pointers in the right direction would be greatly appreciated. Thanks for reading.
Last edited by Killverin on Thu May 26, 2022 5:56 pm, edited 1 time in total.

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

Re: Method not returning prompt

#2 Post by philat »

Use [ code] tags instead of [ quote] tags for code.

Code: Select all

    def Gain(self):
        if MCR.is_active:
            if MCR.crnt_weight + self.weight > MCR.max_weight:
                narrator("You can't carry any more weight.")

            else:
                MCR.crnt_weight += self.weight
                getattr(MCR, "crnt_"+self.ID) += 1 # I'm assuming self.ID is "mag_cell" - flytyped, so there might be an error, but look up getattr and you can get it to work. Sidebar, I wouldn't recommend having crnt_mag_cell and crnt_whatever1 and crnt_whatever2 as properties (use a list or dictionary instead) but that's up to you :shrug: 

Killverin
Newbie
Posts: 10
Joined: Wed May 25, 2022 4:45 pm
Contact:

Re: Method not returning prompt

#3 Post by Killverin »

Thank you 😊

Killverin
Newbie
Posts: 10
Joined: Wed May 25, 2022 4:45 pm
Contact:

Re: [Solved]Method not returning prompt

#4 Post by Killverin »

getattr and setattr are great tools I haven't looked at much until now, thank you again. A list of dictionaries would be prefect for my second question, and I'll look into the code for increasing a key value by one, but I did a poor job explaining the second question in my post. I get the feeling the answer is just as simple as the first, if out of my reach, lol. I get the feeling it will come in handy in future.

In my example, I was trying to get a method of the class [Item] to increment a property of an instance of my inventory class, [MCRI.crnt_mag_cell]. The ID of every Item instance matches the end of every corresponding property in the Inventory class, The Mag Cell items ID being [mag_cell]. I was hoping to have the Get method of the item class insert its ID value in the end. Instead of MCRI.crnt_mag_cell, which functions but is too specific to be in the item class, the plan was to have it be (MCRI.crnt_(self.ID) += 1) so that the instantiated item could increment even the instance MCRI_crnt_smoke_bomb, The Smoke Bomb's ID being [smoke_bomb].

Sorry if my explanation wasn't very graceful, I'm still trying to memorize terms and a dictionary list is probably my next steps for this issue, but being able to phrase this correctly will probably help me out a tone later on.

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

Re: [Solved]Method not returning prompt

#5 Post by philat »

Sorry, brainfart in previous reply: should be basically along the lines of setattr(MCR, "crnt_"+self.ID, getattr(MCR, "ctrn_"+self.ID+1) - but basically, getattr and setattr will get you where you want. Again, though, please look into something else, if only to avoid having many duplicate attributes for every item.

Killverin
Newbie
Posts: 10
Joined: Wed May 25, 2022 4:45 pm
Contact:

Re: [Solved]Method not returning prompt

#6 Post by Killverin »

Will do! Thanks again.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]