Problem with creating a fight function (using screens)

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
ultimate-saiyajin
Newbie
Posts: 10
Joined: Sun Apr 23, 2017 8:46 am
Contact:

Problem with creating a fight function (using screens)

#1 Post by ultimate-saiyajin »

Hello everyone!

I’m working on a Ren’Py game where I want to create fights and I’m really stuck on a problem. My wish is to create a single function that will allow to start a fight by implementing the different parameters like, fight scene image, buttons etc, and the image that will return to the story. That way I will launch a fight and when it’s done, jump right back to the story.

I have tried to create a function which calls a screen “fight”. The function takes two strings in parameters (the background image of the fight AND the background to display after the fight’s end, when the history starts again).

Code: Select all


image img0 = "images/img0.jpg"
image img1 = "images/img1.jpg"
image img2 = "images/img2.jpg"

image fight = ConditionSwitch(
    "fight_place == 1", "images/img1.jpg",
    "fight_place == 2", "images/img2.jpg",
    "True", "images/img0.jpg"
    )


init python:

    def start_fight(f,h):
        fight_place = f
        history_place = h
        renpy.jump("fight") 


label start:
    
    scene img0
    e "Blahblahblah"

    $ start_fight(img1,img2)


In another script file:

Code: Select all

label fight:
	scene fight
	call screen fight_screen
	screen fight_screen:
        	imagebutton idle "images/other_image.png" hover "images/other_image.png" xpos 500 ypos 500 focus_mask True action Jump('start')
	scene history_place
During my test, I had some problems. Ren’Py / Python constrained me to declare the variable “fight_place” before I could use it, but the image take only the initialization value even if I modify it after that with the function. My function doesn’t seem to change my fight_place variable.

I tested the choice function ATL (https://www.renpy.org/doc/html/atl.html ... -statement) also, but got the same result.

I’m trying to create a clean code for my game. Maybe I’m doing something wrong and not using the right Ren’Py or Python methods. If you have any ideas or solutions, I’m all ears!

Thank you for your help!

User avatar
SuperbowserX
Veteran
Posts: 270
Joined: Sat Jan 07, 2017 3:09 pm
Contact:

Re: Problem with creating a fight function (using screens)

#2 Post by SuperbowserX »

Define the fight_screen screen at the top of your script, before label start.

Then start the game. If it doesn't work, you should get a traceback error, or Ren'py will pop up with a window telling you what your problem is.

Provide us that problem/traceback and that can help us help you.

If the game does start but doesn't provide expected results, then tell us what's wrong and we'll try to help. :)

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Problem with creating a fight function (using screens)

#3 Post by trooper6 »

I'm writing a speech that I have to give in an hour so I can't go into a lot of details right now...but there is a bunch going on with this code that could be different.

First few things I'd note:
1) Don't have more than one thing called the same thing. It confuses the computer. You have a label called fight and you have an image called fight. Don't do that.
2) Declare the initial state of all your variables before the game starts using "default"
3) You can pass variables into a screen, so you don't actually need that function at all
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

ultimate-saiyajin
Newbie
Posts: 10
Joined: Sun Apr 23, 2017 8:46 am
Contact:

Re: Problem with creating a fight function (using screens)

#4 Post by ultimate-saiyajin »

SuperbowserX and trooper6, thank you for helping me.

I tried to follow your advice and I get the following code:

Code: Select all


define e = Character('Eileen', color="#c8ffc8")

image img0 = "images/img0.jpg"
image img1 = "images/img1.jpg"
image img2 = "images/img2.jpg"
image img3 = "images/img3.jpg"


init python:
    
    def fight_screen(fight_place,history_place):
        renpy.scene()
        renpy.show(fight_place)
        renpy.pause() # fight
        renpy.scene()
        renpy.show(history_place)
    
        renpy.define_screen("fight", fight_screen)

label start:
    
    scene img0
    e "Blah blah blah"

    $ fight_screen("img1","img2")

	return

It is much cleaner than what I had initially and it works but I do not know if it is still very clean and if I can do better.

I do not understand how this line is used and I do not know if I really need it:

Code: Select all

    renpy.define_screen("fight", fight_screen)

Just to test, I tried to use it with that:

Code: Select all

    show screen fight("img2","img3")
But I get the following error : TypeError: fight_screen() got an unexpected keyword argument ‘_name’

What do you think of that?

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Problem with creating a fight function (using screens)

#5 Post by trooper6 »

Yeah...this is not how I'd do this. I'm about to go off to work and won't be home for about 15 hours. I'll see what I can do when I get back. But why do you need history_place at all? If you have an image as the background, you can show or call a fight screen, and when you hide the screen you he old background will still be there.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

ultimate-saiyajin
Newbie
Posts: 10
Joined: Sun Apr 23, 2017 8:46 am
Contact:

Re: Problem with creating a fight function (using screens)

#6 Post by ultimate-saiyajin »

Ok^^’
So I do not see how... :?

I use history_place because I consider the possibility that the background of the story may change depending on how the combat progresses. I was planning to use it with a condition. But you're right, if the background does not change, I just can hide the screen.

In creating this screen, I wanted to avoid writing this code for each fight (I foresee a fifty or more):

Code: Select all


scene img1
call screen fight #OR $fight_screen()
scene img2

While I think it is possible to factorize and write it in one line.


By placing my screen before the start label, I noticed that I had difficulty doing what I wanted. For example, just creating an imagebutton, I have not found any equivalent statements in the documentation. (https://www.renpy.org/doc/html/statemen ... lents.html)

In all the scripts that I saw on the web, I did not see much where the screens were placed before the start label. If it is the right method, I lack a concrete example. If not, how do I proceed?

User avatar
SuperbowserX
Veteran
Posts: 270
Joined: Sat Jan 07, 2017 3:09 pm
Contact:

Re: Problem with creating a fight function (using screens)

#7 Post by SuperbowserX »

In general, why are you defining your screen in a Python block?

Currently, you are using:

Code: Select all

    def fight_screen(fight_place,history_place):
        renpy.scene()
        renpy.show(fight_place)
        renpy.pause() # fight
        renpy.scene()
        renpy.show(history_place)
   
        renpy.define_screen("fight", fight_screen)
Why can't you just use, at the top of your script, and not in a python block, do this:

Code: Select all

    label fight_screen(fight_place,history_place):
        scene fight_place #note from here https://www.renpy.org/doc/html/statement_equivalents.html#renpy.scene that this is less lines of code
        renpy.pause() # fight
        scene history_place
        renpy.define_screen("fight", fight_screen) #if you could explain, why are you using this line of code?
        return #this, once this label is done, will return to what it was called from.
Then, when you want to use this screen, run this line:

Code: Select all

call fight_screen(parameter you want for fight_place, parameter you want for history_place

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Problem with creating a fight function (using screens)

#8 Post by trooper6 »

Really, I want to know how the fight itself is supposed to be work. There is so much missing information it is hard to help.

Are there character classes? What are they?
How are fights actually supposed to work?
Does the player make choices in the fight? No?
I don't even really know if any of this is needed without knowing what is supposed to be on this screen and how the battle is supposed to work.
In plain English, with all the detail.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

ultimate-saiyajin
Newbie
Posts: 10
Joined: Sun Apr 23, 2017 8:46 am
Contact:

Re: Problem with creating a fight function (using screens)

#9 Post by ultimate-saiyajin »

SuperbowserX :

Sorry, I misunderstood your advice.

Code: Select all

renpy.define_screen("fight", fight_screen)
I was just wondering how to use this line of code but as I am no longer in the python init. I was inspired by the documentation (which I sometimes find difficult to translate): https://www.renpy.org/doc/html/screen_p ... and-python

So now I have the following code:

Code: Select all

define e = Character('Eileen', color="#c8ffc8")

image img0 = "images/img0.jpg"
image img1 = "images/img1.jpg"
image img2 = "images/img2.jpg"
image img3 = "images/img3.jpg"

label fight_screen(fight_place,history_place):
    scene fight_place
    $ renpy.pause() # fight
    scene history_place
    return

label start:
    scene img0
    e "Blah blah blah"
    call fight_screen("img1","img2")
    return
With this line of code:

Code: Select all

call fight_screen("img1","img2")
Ren'Py shows me the default gray backgrounds with "fight_place" and "history_place" written at the top. I tried with and without the quote, and with the direct path but it does not work. I cannot move my pictures as a parameter.

Some ideas? ^^’



trooper6 :

Okay, I'll try to be as clear as possible. I apologize for my English, it is not my first language, I am French ^^ '

So I need a screen that I can call between two lines of dialogue to start a fight. I have a Fighter class in which life points, attack points, defense points and stamina are managed. There are parameters to define its name, level and race. There are also functions that call the Inventory class to use the hero's objects (example: to regenerate life).

For the functioning of the fighting, what comes closest to what I want to achieve are the Pokémon fights :
http://vignette2.wikia.nocookie.net/pok ... 1219214712

That is, by turn, the hero then the opponent and so on. Except in one case: the opponent's attacks being calculated in advance, if the two protagonists carry out a powerful attack from a distance then there is a duel. For the player, this duel consists of tapping on the keys indicated on the screen before a given time, also displayed on the screen. If he is wrong or takes too long, he loses the duel.

On the screen, the hero and opponent's name, level, health points, defense and stamina points must be displayed. The lower part will contain in the first part buttons (imagebutton?) allowing the player to determine his next action:
- Attack (the points of attack by attack are displayed)
- Defend (same for defense points)
- Browse its inventory (indication of the effects of the objects)
- Leak attempt (random function, integer switch to parameters)


In the second part, indications will be given to the player or else the submenu for each button will be displayed in this frame.

I need a second place (history_place) in a particular case. For example, the protagonist begins their fight on a planet and if the most powerful attack is triggered by the player then the planet explodes and they find themselves in space, thus even after the fight.

If I am not wrong, I must pass in parameters of my screen:
- The place of battle
- The place of post-combat (condition)
- Hero stats
- The opponent's stats

I do not know if it's clearer, if you need precision do not hesitate. =)

ultimate-saiyajin
Newbie
Posts: 10
Joined: Sun Apr 23, 2017 8:46 am
Contact:

Re: Problem with creating a fight function (using screens)

#10 Post by ultimate-saiyajin »

Hey there!
Have you found something for my problem?

I think that I only need to know how to pass an image in parameters of a function, but I don't know how to do that. :(

Post Reply

Who is online

Users browsing this forum: Google [Bot]