How to run python commands as the result of a screen language action?

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
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

How to run python commands as the result of a screen language action?

#1 Post by Chekhov »

I'm playing around with changing the cursor under certain conditions and am able to do so with the following:

Code: Select all

screen cursor_changer:

    if cur_new != "def_cur":
        $ config.mouse = {"default" :[cur_new]}
    else:
        $ config.mouse = None   
        
#And then I can change the cursor image with the following:
$ cur_new = ("cursor-point.png", 0, 0)        

What I'd like to do, is change the cursor when hovering over certain hotspots. I know I can do things with "action", but I don't know how to use python scripts/commands in that place.

https://www.renpy.org/doc/html/screen_actions.html

Code: Select all




screen test_girl():
    imagemap:
        ground "images/testgirl.jpg"
	hotspot (105, 325, 309, 400) clicked Jump("inventory") hovered ##### SCRIPT GOES HERE##### 
	
screen the_img(img):
    add img pos (354, 80)
    
   
Can I do it this way? If not, any ideas on how I can change a cursors when hovering over certain hotspots?


----


Now there is another thing I've ran into. I'm trying to figure out how images are resized. It seems like when I use photoshop to find the exacty borders of the hotspot, it does not correspond accurately with inside the game. Now this may be because my images are rather big and being resized automatically? Any ideas on how to do that accurately?

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to run python commands as the result of a screen language action?

#2 Post by Alex »

For buttons and hotspots one can set the mouse property - https://www.renpy.org/doc/html/style_pr ... erty-mouse
This will change mouse pointer while it hovers over this button/hotspot. To make it work you need to specify all the neccessary pointers, like

Code: Select all

define config.mouse{"default" :("cursor-point.png", 0, 0), "hand":("cursor-hand.png", 0, 0), "thumb":("cursor-thumb.png", 0, 0)}
To run python function in a screen you should use Function action - https://www.renpy.org/doc/html/screen_a ... l#Function

As for resizing images - it is better to make them apropriate size in a photoshop-like program, so there won't be an issue with hotspots.
(Not tested, but you could try to resize the whole imagemap with all the hotspots, not the individual images used for it)

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: How to run python commands as the result of a screen language action?

#3 Post by Chekhov »

Alex wrote: Sat May 18, 2019 6:39 pm For buttons and hotspots one can set the mouse property - https://www.renpy.org/doc/html/style_pr ... erty-mouse
This will change mouse pointer while it hovers over this button/hotspot. To make it work you need to specify all the neccessary pointers, like

Code: Select all

define config.mouse{"default" :("cursor-point.png", 0, 0), "hand":("cursor-hand.png", 0, 0), "thumb":("cursor-thumb.png", 0, 0)}
This throws a number of errors:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 39: expected '=' not found.
    define config.mouse{"default" :("cursor-point.png", 0, 0), "pinch":("cursor-hand.png", 0, 0), "ungrab":("cursor-thumb.png", 0, 0)}
                       ^

Ren'Py Version: Ren'Py 7.1.3.1092
Mon May 20 02:12:13 2019
And if I change it to define config.mouse = ...(etc)... I get:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00start.rpy", line 198, in script call
    call _gl_test
  File "renpy/common/00gltest.rpy", line 442, in script
    $ __gl_test()
  File "renpy/common/00gltest.rpy", line 442, in <module>
    $ __gl_test()
  File "renpy/common/00gltest.rpy", line 359, in _m1_00gltest__gl_test
    _gl_performance_test()
  File "renpy/common/00gltest.rpy", line 384, in _gl_performance_test
    ui.interact(suppress_underlay=True, suppress_overlay=True)
ValueError: too many values to unpack
Finally, when it's correct, how do I invoke the change when using this way?
To run python function in a screen you should use Function action - https://www.renpy.org/doc/html/screen_a ... l#Function

As for resizing images - it is better to make them apropriate size in a photoshop-like program, so there won't be an issue with hotspots.
(Not tested, but you could try to resize the whole imagemap with all the hotspots, not the individual images used for it)
I don't understand at all how I could use Function to achieve this. I'm going to try around things to see if I can figure it out, but it's far from clear.

----


Finally I figured out what I was doing wrong with hotspots. I thought it was (x, y, x2, y2) but it's (x, y, width, height).

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to run python commands as the result of a screen language action?

#4 Post by Alex »

Uh, I've read your first post once again...
For the code from 1st post you just need to set a variable, so you can use SetVariable("cur_new", ("cursor-point.png", 0, 0)) action - https://www.renpy.org/doc/html/screen_a ... etVariable

Also, there is a way to change mouse pointer by setting config.mouse variable and adding "mouse" property to buttons/hotspots, as I mentioned. The error you've received is my fault, code should look like

Code: Select all

define config.mouse = {"default" :[("cursor-point.png", 0, 0)], "hand":[("cursor-hand.png", 0, 0)], "thumb":[("cursor-thumb.png", 0, 0)]}
(for each pointer name should be a list of (image, x, y) tuples (even if it's a single one), 'cause if there would be more than one tuple the pointer will be animated (20 images per second))

The Function action is used if you need to run your function by activating button/hotspot. As for your mouse pointer issue - you don't need to run any functions, 'cause all the changes are made by cursor_changer screen and happenes right when "cur_new" variable changes (assuming that cursor_changer screen is allways shown).
Last edited by Alex on Mon May 20, 2019 9:48 am, edited 3 times in total.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to run python commands as the result of a screen language action?

#5 Post by Remix »

*cough*

Still missed the = sign

Code: Select all

define config.mouse = {"default" :[("cursor-point.png", 0, 0)], "hand":[("cursor-hand.png", 0, 0)], "thumb":[("cursor-thumb.png", 0, 0)]}
# -----------------^
Frameworks & Scriptlets:

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to run python commands as the result of a screen language action?

#6 Post by Alex »

Remix wrote: Mon May 20, 2019 7:33 am *cough*
Oops, thnx...))

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: How to run python commands as the result of a screen language action?

#7 Post by Chekhov »

Alex wrote: Mon May 20, 2019 5:48 am Uh, I've read your first post once again...
For the code from 1st post you just need to set a variable, so you can use SetVariable("cur_new", ("cursor-point.png", 0, 0)) action - https://www.renpy.org/doc/html/screen_a ... etVariable

Also, there is a way to change mouse pointer by setting config.mouse variable and adding "mouse" property to buttons/hotspots, as I mentioned. The error you've received is my fault, code should look like

Code: Select all

define config.mouse = {"default" :[("cursor-point.png", 0, 0)], "hand":[("cursor-hand.png", 0, 0)], "thumb":[("cursor-thumb.png", 0, 0)]}
(for each pointer name should be a list of (image, x, y) tuples (even if it's a single one), 'cause if there would be more than one tuple the pointer will be animated (20 images per second))

The Function action is used if you need to run your function by activating button/hotspot. As for your mouse pointer issue - you don't need to run any functions, 'cause all the changes are made by cursor_changer screen and happenes right when "cur_new" variable changes (assuming that cursor_changer screen is allways shown).
Ah yes, using setvariable definitely works. It took me a couple of tries to get my head around it.

I am confused why running it as a function didn't work, though. I can define a function with python, but is there a seperate way to define a function in Ren'py? It doesn't seem to be the case. Any attempt at calling a python defined function with screen language is still failing. This isn't really a question about the original topic, since that is solved thanks to your advice, but how would I call a function with screen language in the first place? Could you show me a tiny example?

This is what I tried and failed with:

Code: Select all

python:
	def cursorfunctiontester():
        	cur_new = ( "cursor-wave.png", 0, 0)
        	return

screen test_girl():
    imagemap:
        ground "images/testgirl.jpg"
        hotspot (115, 285, 72, 110) clicked Function(cursorfunctiontester()) hovered SetVariable("cur_new", ("cursor-hand.png", 0, 0)) unhovered SetVariable("cur_new", ("cursor-point.png", 0, 0))         	
It would fail with a name error telling me that cursorfunctiontester is undefined.


Thank you for all the good advice so far!

philat
Eileen-Class Veteran
Posts: 1910
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How to run python commands as the result of a screen language action?

#8 Post by philat »

Probably because it's not if it's not in init python.

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: How to run python commands as the result of a screen language action?

#9 Post by Chekhov »

philat wrote: Mon May 20, 2019 9:38 pm Probably because it's not if it's not in init python.
I tried it.

Results in NoneType object is not callable.

philat
Eileen-Class Veteran
Posts: 1910
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How to run python commands as the result of a screen language action?

#10 Post by philat »

Hadn't looked at the whole thing, but. https://www.renpy.org/doc/html/screen_a ... l#Function

Code: Select all

init python:
    def foo(bar):
        print bar

screen screen_foo():
    textbutton "Test" action Function(foo, "print this")

Post Reply

Who is online

Users browsing this forum: voluorem