Okay, check this out:
- Code: Select all
"Equip something":
python:
invlist = [(f.name,f) for f in me.inv] #<- THIS SHIT HERE. THE EQUIP FUNCTION, WHICH USES THE VARIABLE "x", WON'T RUN IF THIS ISN'T "invlist = [(x.name,x) for x in me.inv]"
pick = menu(invlist)
g("[pick.desc]") # this shows that it does in fact return an item and assign it to the variable 'pick'
me.equip(pick) # hand-off pick to the player's equip function
This is an option in a renpy menu to equip something from the player's inventory. The problem I ran into was that while I could equip items and they did correctly change stats, the function was not removing whatever item was already in the item slot. I went through a dozen or more iterations trying to figure out what I was doing wrong. Here's what part of the equip code looks like:
- Code: Select all
def equip(self,x):
if x.itemtype == "equipment": # check if the selected item is equippable
if x.bonus == "allure": # this item requires 'x' level of allure to wield, or, in other words, is a wand
if self.all >= x.lvl: # check if the player has the required stats to use the item
if len(self.weapon) > 1: # check if there are already items in the player's equipment slot
n = self.weapon.pop() # take off the current weapon and assign it to n
self.inv.append(n) # put the old weapon in the inventory
self.weapon.append(x) # put the new weapon in the player's weapon slot
g("You switch out the [n.name] for the [x.name]") # announce this to the player
else:
g("You equip the [x.name]")
self.inv.remove(x)
self.weapon.append(x)
(If you want all of it, here:)
- Code: Select all
def equip(self,x):
if x.itemtype == "equipment": # check if the selected item is equippable
if x.bonus == "allure": # this item requires 'x' level of allure to wield, or, in other words, is a wand
if self.all >= x.lvl: # check if the player has the required stats to use the item
if len(self.weapon) > 0: # check if there are already items in the player's equipment slot
n = self.weapon.pop() # take off the current weapon and assign it to n
self.inv.append(n) # put the old weapon in the inventory
self.weapon.append(x) # put the new weapon in the player's weapon slot
g("You switch out the [n.name] for the [x.name]") # announce this to the player
else:
g("You equip the [x.name]")
self.inv.remove(x)
self.weapon.append(x)
else:
g("You lack the ability to wield this...")
elif x.bonus == "dexterity":
if self.dex >= x.lvl:
if len(self.weapon) > 0:
n = self.weapon.pop()
self.inv.append(n)
self.weapon.append(x)
g("You switch out the [n.name] for the [x.name]")
else:
g("You equip the [x.name]")
self.inv.remove(x)
self.weapon.append(x)
else:
g("You lack the ability to wield this...")
elif x.bonus == "strength":
if self.str >= x.lvl:
if len(self.weapon) > 0:
n = self.weapon.pop()
self.inv.append(n)
self.weapon.append(x)
g("You switch out the [n.name] for the [x.name]")
else:
g("You equip the [x.name]")
self.inv.remove(x)
self.weapon.append(x)
else:
g("You lack the ability to wield this...")
else:
if len(self.armor) > 0:
n = self.armor.pop()
self.inv.append(n)
self.armor.append(x)
g("You switch out the [n.name] for the [x.name]")
else:
g("You equip the [x.name]")
self.inv.remove(x)
self.armor.append(x)
else:
g("That's not a valid item type!")
self.update()
So, what I found with some experimentation... was that Ren'Py was only running the equip function because I used the same name for the argument as an earlier variable. I found the problem because the "You equip (...) item" was always returning the final value in the inventory list. Slipping a "x = pick" right before running the equip function, dirty as that fix is, did seem to solve the problem. I also realized that the game wouldn't run the "switch" items because I had the check equal to len(self.weapon) > 1 instead of > 0.
So, first big question:
HOW THE FUCK DOES RENPY HANDLE MY SCOPE? Take this for example:
- Code: Select all
elif x.bonus == "strength":
if self.str >= x.lvl:
if len(self.weapon) > 0:
n = self.weapon.pop()
self.inv.append(n)
self.weapon.append(x)
for y in self.weapon:
g("[y.desc]")
narm = self.inv[-1]
g("You switch out the [narm.name] for the [x.name]")
What I'm doing here is checking that there is only the one item in the player's weapon slot. g("") is the Ren'Py syntax for making a character say something. I haven't found a better way to "print" strings to the main screen, which is bad, because Mr.G here is utter shite. It's returning an error here because it doesn't recognize what "y" is, so it can't print y's description. The same thing is happening lower down with narm, a leftover variable for testing Mr.G.
Anyways, somebody who's more experienced with Ren'Py, help would be appreciated.
1. Why the hell does the name of my function's argument matter when I put in a variable?
2. How do I make Mr.G cooperate and recognize the variables I'm trying to get it to print?
3. Explain this:
- Code: Select all
"Equip something":
python:
invlist = [(x.name,x) for x in me.inv]
pick = menu(invlist)
g("[x.desc]") # This returns the description of the last item in the inventory
x = pick # 5 bucks says this turns the last item in the inventory into whatever the user picks from the menu
g("[x.desc]") # this displays the description of the mysterious new variable x
g("[pick.desc]") # this displays the description of whatever the user picked from the same menu. In game, this is the same as the previous line. At least something here makes sense...
me.equip(pick) # hand-off pick to the player's equip function
What I believe is happening is Ren'Py is creating some variable "x" when I create the menu list, and rather than handing off pick to the equip function as argument x, every time I have an x.this or self.dothis(x), it's using variable x instead of pick.