Ren'Py Help with Inventory - Energy level

pobrecito007

Newbie
Dec 18, 2019
21
1
Good morning,

I have a problem with the inventory system regarding the energy level.
The code itself works perfectly but i want to add in the removeItem paragraph that the energy level goes up when you drink an energydrink. In renpy i would just add :
if player_energy <= 100:
$ player_energy += 10
What do i have to do here to make it work?

This is my code for the inventory class :
init python:

class Item:




def __init__(self, code, name, desc, img = "noimage", value = 0, iType = "item", uses = -1, unique = False):
self.code = code
self.name = name
self.desc = desc
self.img = img
self.value = value
self.iType = iType
self.uses = uses



class Inventory:




def __init__(self, name, money = 0, background = "images/inventory/inventory_panel.png", slots = 40):
self.name = name
self.money = money
self.background = background
self.slots = slots
self.items = []





def addItem(self, item):
if self.freeSlots():
if self.hasItem(item) is not False and item.unique == True:
return False
else:
self.items.append([item, item.uses])
self.items.sort(key=lambda i: i[0].name)
#renpy.notify("New Item!")
return True
else:
return False
return





def hasItem(self, item):
for i in self.items:
if i[0] == item:
return self.items.index(i)
return False




def freeSlots(self):
tmp = self.slots - len(self.items)
if tmp <= 0:
return False
else:
return tmp


def removeItem(self, item):
invIndex = self.hasItem(item)
if invIndex is not False and self.items[invIndex][1] != -1:
if self.items[invIndex][1] > 1:
if self.items[invIndex][0].iType == "energydrink":
self.items[invIndex][1] -= 1


if self.items[invIndex][0].iType == "usable":
self.items[invIndex][1] -= 1

return
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,611
2,258
Without some testing, I'm not 100%... but I think you're looking to add something like this:

Python:
    store.player_energy += 10
    if store.player_energy > 100:
        store.player_energy = 100

When using an energy drink.
(I've coded it slightly different from your example, because if energy was 95 then the value would end up being 105 - which I'm guessing isn't what you intended).

store. is the surefire way of referencing a normal RenPy variable within a function.

The resulting code being....
(If I've got all the indenting correct).

Python:
init python:

    class Item:

        def __init__(self, code, name, desc, img = "noimage", value = 0, iType = "item", uses = -1, unique = False):
            self.code = code
            self.name = name
            self.desc = desc
            self.img = img
            self.value = value
            self.iType = iType
            self.uses = uses


    class Inventory:

        def __init__(self, name, money = 0, background = "images/inventory/inventory_panel.png", slots = 40):
            self.name = name
            self.money = money
            self.background = background
            self.slots = slots
            self.items = []


        def addItem(self, item):
            if self.freeSlots():
                if self.hasItem(item) is not False and item.unique == True:
                    return False
                else:
                    self.items.append([item, item.uses])
                    self.items.sort(key=lambda i: i[0].name)
                    #renpy.notify("New Item!")
                    return True
            else:
                return False
            return


        def hasItem(self, item):
            for i in self.items:
                if i[0] == item:
                    return self.items.index(i)
            return False


        def freeSlots(self):
            tmp = self.slots - len(self.items)
            if tmp <= 0:
                return False
            else:
                return tmp


        def removeItem(self, item):
            invIndex = self.hasItem(item)
            if invIndex is not False and self.items[invIndex][1] != -1:
                if self.items[invIndex][1] > 1:
                    if self.items[invIndex][0].iType == "energydrink" and store.player_energy < 100:
                        self.items[invIndex][1] -= 1
                        store.player_energy += 10
                        if store.player_energy > 100:
                            store.player_energy = 100

                    if self.items[invIndex][0].iType == "usable":
                        self.items[invIndex][1] -= 1

            return
 
Last edited:

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,611
2,258
Oh man. You are the hero of the day!!!!! Thank you soooooo much! I really really apreciate it

It occurs to me that you wouldn't want the player to drink an energy drink if their energy was already 100. I've updated my code to reflect that.

With...

Python:
if self.items[invIndex][0].iType == "energydrink" and store.player_energy < 100:
 
  • Like
Reactions: Shadow Fiend

pobrecito007

Newbie
Dec 18, 2019
21
1
It occurs to me that you wouldn't want the player to drink an energy drink if their energy was already 100. I've updated my code to reflect that.

With...

Python:
if self.items[invIndex][0].iType == "energydrink" and store.player_energy < 100:
You had it already correct in your first answer. Its working perfectly
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,611
2,258
You had it already correct in your first answer. Its working perfectly

Almost, but not quite.

The main code stops the player's energy going above 100 when a energy drink is used.

The update was to stop players using an energy drink if they accidentally clicked it more than once while already at full energy. Effectively stopping the item count being reduced by 1 if the player energy didn't actually need to use it.

That is, unless you want the player to discard a energy drink if they stoopidly use one when they didn't need to.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,978
16,236
On a side note, you shouldn't use removeItem for that, but instead have a useItem.
The day you'll decide that the player will give his energy drink to someone, or loose it, you'll be stuck.
 
  • Like
Reactions: Shadow Fiend