Function arguments

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
Bartulisica
Regular
Posts: 93
Joined: Sun Aug 30, 2015 5:11 am
Organization: ArizonaIdentities
Location: Croatia
Discord: ArizonaIdentities
Contact:

Function arguments

#1 Post by Bartulisica »

Hello there, I have a problem with functions.. I tried searching in the documentation, but it didn't solve my problem..

Anyway..

Code: Select all

def remove_shirt (self, shirt):
            if shirt in self.equipped_shirt:
                for shirt in self.equipped_shirt:
                    self.equipped_shirt.remove(shirt)
This function says that it needs at least 2 arguments. I don't need a second argument, so could you please tell me is there another way I can write this function, without having this problem.

Thanks in advance.
- ArizonaIdentities

User avatar
mobychan
Veteran
Posts: 275
Joined: Fri Apr 24, 2015 6:31 am
Projects: The Chosen - Sakura Pink & Gentian Blue
Organization: Foresoft
Location: Germany
Contact:

Re: Function arguments

#2 Post by mobychan »

How do you call it?
the self argument is automatically added if you type

Code: Select all

my_object.remove_shirt(shirt)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Function arguments

#3 Post by xela »

It's a method (of a class), not a function. When method is bound to a class, "self" refers to an instance of a class. There is nothing about this in documentation, this is pure python.
Like what we're doing? Support us at:
Image

User avatar
Bartulisica
Regular
Posts: 93
Joined: Sun Aug 30, 2015 5:11 am
Organization: ArizonaIdentities
Location: Croatia
Discord: ArizonaIdentities
Contact:

Re: Function arguments

#4 Post by Bartulisica »

I call it with the imagebutton I ask you two before. :D

Code: Select all

imagebutton idle pic hover h_pic xpos a ypos b action inventory.remove_shirt, AddToSet(equip_shirt, Shirt)
AddToSet, what xela said before works perfectly, but I can't remove all of the items before adding another.

So, I defined a function to remove everything, and I called it before "AddToSet", so it does that every time.

How would you call that? If you understand what I'm trying to do here.. :) (By the way, thanks for fast replies, both of you.)
- ArizonaIdentities

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Function arguments

#5 Post by xela »

As I've said in previous post, your code looks like a mess :?

Do you care what kind of shirt is equipped? If so, you need to pass an argument when removing the shirt. If you don't and just want to have an empty list, why the heck do you have a "shirt" as a second argument when removing it?

You problem seems to be neither with Ren'Py nor with Python, more likely it's your own design. You previous "I have reasons" reply doesn't cut it because there is no reason for bad code, especially not in simple systems like inventories.
Like what we're doing? Support us at:
Image

User avatar
Bartulisica
Regular
Posts: 93
Joined: Sun Aug 30, 2015 5:11 am
Organization: ArizonaIdentities
Location: Croatia
Discord: ArizonaIdentities
Contact:

Re: Function arguments

#6 Post by Bartulisica »

Actually there is a reason.. For someone that started coding not long ago, and is doing his project just to practice, it's not so easy to know every possible way things work. I'm trying to come up with my own ways how to make something work, instead of copying other people's code.

So, it does look messy but it worked so far.
- ArizonaIdentities

User avatar
Bartulisica
Regular
Posts: 93
Joined: Sun Aug 30, 2015 5:11 am
Organization: ArizonaIdentities
Location: Croatia
Discord: ArizonaIdentities
Contact:

Re: Function arguments

#7 Post by Bartulisica »

Anyway, I got your point and instead of making a method as before, I used:

Code: Select all

def clean_list (self):
            self.equipped_shirt = []
But, now it doesn't add another after that.

Could you tell me how would you remove everything from the list, and add just one thing with a single imagebutton.

Both of the things work on their own, but I can't get them to work together, one after another..
- ArizonaIdentities

User avatar
mobychan
Veteran
Posts: 275
Joined: Fri Apr 24, 2015 6:31 am
Projects: The Chosen - Sakura Pink & Gentian Blue
Organization: Foresoft
Location: Germany
Contact:

Re: Function arguments

#8 Post by mobychan »

You shouldn't simply copy people's code and you definitely shouldn't try stuff until it somehow works.
You have to underestand what you're doing.
Look at code from others and try to figure out what each bit does, then go ahead and try using what you learned.

You previous method would work if you simply remove the "shirt" parameter.

User avatar
Bartulisica
Regular
Posts: 93
Joined: Sun Aug 30, 2015 5:11 am
Organization: ArizonaIdentities
Location: Croatia
Discord: ArizonaIdentities
Contact:

Re: Function arguments

#9 Post by Bartulisica »

Then, it says that I'm using a local variable before assignment..
- ArizonaIdentities

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Function arguments

#10 Post by xela »

Fair enough, but other people put in a lot of thought and time into designs that wish to share, not learning from that is more often a mistake that it is not.
Bartulisica wrote:Could you tell me how would you remove everything from the list, and add just one thing with a single imagebutton.
In Inventory class:

Code: Select all

def equip_shirt(self, shirt):
    self.equipped_shirt = [shirt]
In screen:

Code: Select all

imagebutton idle pic hover h_pic xpos a ypos b action Function(inventory.equip_shirt, Shirt)
There is no need to unequip anything since you do not seem to be adding unequipped shirt back to any inventory pool.

===
This does on no way explain why self.equipped_shirt is a list and not a plain property or a field in a dict like in most other inventories.
Like what we're doing? Support us at:
Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Function arguments

#11 Post by xela »

In fact:

SetField(inventory, "equipped_shirt", [Shirt])

is even more to the point...
Like what we're doing? Support us at:
Image

User avatar
Bartulisica
Regular
Posts: 93
Joined: Sun Aug 30, 2015 5:11 am
Organization: ArizonaIdentities
Location: Croatia
Discord: ArizonaIdentities
Contact:

Re: Function arguments

#12 Post by Bartulisica »

I know they do, and I'm trying to understand other people's codes, don't worry, still learning from everything I can get. :)

Oh, yeah I get it now, didn't even think that way. Thanks
- ArizonaIdentities

User avatar
Bartulisica
Regular
Posts: 93
Joined: Sun Aug 30, 2015 5:11 am
Organization: ArizonaIdentities
Location: Croatia
Discord: ArizonaIdentities
Contact:

Re: Function arguments

#13 Post by Bartulisica »

Thanks a bunch, both of you.
- ArizonaIdentities

Post Reply

Who is online

Users browsing this forum: Ocelot