Specifying item in own definition? [Solved]

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
User avatar
Stapper
Regular
Posts: 96
Joined: Wed Feb 27, 2013 9:54 pm
Contact:

Specifying item in own definition? [Solved]

#1 Post by Stapper »

I have a very crude equip system that lets me see which items are equipped by the player.

Code: Select all

    def equip(item):
        global equip_melee
        equip_melee = item
        message = "Equiped!"
        renpy.show_screen("inventory_popup", message=message)
        renpy.restart_interaction()
And I define my items like this:

Code: Select all

$ melee_blunt1 = Item("Mace", "Melee weapon - level 1\nType: Blunt", "images/items/melee_blunt1.png", 5, act=equip(melee_blunt1), recipe=[[wood,2],[iron,1]])
Everything seems to be in place when I replace the "item" in the function with "melee_blunt1" but in order to make it work for all items I would need to specify the item that is going to be equipped in its own definition somehow. I cannot leave it like this because it gives me: NameError: name 'melee_blunt1' is not defined

Is it possible to somehow specify the item in its own definition, or will I have to make an equip function for every item seperately? :?
Last edited by Stapper on Sat May 17, 2014 6:54 pm, edited 1 time in total.

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Specifying item in own definition?

#2 Post by Asceai »

So is this Item class also yours, or what? Because what I see as being a problem is that act=equip(blah) doesn't do what you think it does. Specifically, this will call the function equip with melee_blunt1 at the time the code declaring the item is run. So even if this code worked, it seems that it would just try to equip the item then, and because the equip function doesn't return anything, it would pass act=None to the Item declaration. Not what you want at all. But I'd need more information about this Item class to determine what you actually do want.

User avatar
Stapper
Regular
Posts: 96
Joined: Wed Feb 27, 2013 9:54 pm
Contact:

Re: Specifying item in own definition?

#3 Post by Stapper »

Sorry for the confusion, I will try and explain it better... :o

I'm using the inventory system from saguaro as a base: http://lemmasoft.renai.us/forums/viewto ... 51&t=25579
He used the 'act' to call the crafting screen when you click an item in the inventory, it only triggers then. But I substituted this in order to equip items instead. Here is the item class (I removed the standard 'act' to show the message nothing happens:

Code: Select all

    class Item(store.object):
        def __init__(self, name, desc, icon=False, value=0, act=Hide("Null"), recipe=False):
            global cookbook
            self.name = name
            self.desc = desc
            self.icon = icon
            self.value = value
            self.act = act # action executed from the single inventory screen  
            self.recipe = recipe # nested list of [ingredient, qty]            
So all I try to do is that when a weapon with 'act=equip' is clicked in the inventory, the item is set as the variable 'equip_melee'. From there I can check in battles which weapon they have equipped.

I hope this makes more sense this way... my apologies if it doesn't but I'm not sure what else I can tell about it :?

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Specifying item in own definition?

#4 Post by Asceai »

Aha, so 'act' is an action tied to that item's button. Okay.

I'm not really familiar enough with Python to know the correct way of doing this, but here goes:

Code: Select all

$ melee_blunt1 = Item("Mace", "Melee weapon - level 1\nType: Blunt", "images/items/melee_blunt1.png", 5, act=renpy.curry(eval)("equip(melee_blunt1)"), recipe=[[wood,2],[iron,1]])
Notice how the code in inventory.rpy curries all the things that get called with arguments? You need to do that too if you want to pass arguments to functions that are being provided as actions. The eval is simply so that the code is run after melee_blunt1 exists- not sure if there's a better way to do that.

User avatar
Stapper
Regular
Posts: 96
Joined: Wed Feb 27, 2013 9:54 pm
Contact:

Re: Specifying item in own definition?

#5 Post by Stapper »

That's exactly what I needed to make the whole system work!! :D

Thanks a lot Asceai! It works perfectly like this :)

Post Reply

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot]