Page 1 of 2

A simple navigation map tutorial

Posted: Sun Jul 21, 2013 11:48 am
by Skarn
Hello,

This tutorial will cover a very basic Ren'Py feature: create a simple navigation map. But, despite its simplicity, I did not found any other cookbook on the matter, so I decided to write one. If this is a duplicate, please kill this thread.

Basic usage

Images

Let's go to space! A spatial exploration game, when YOU have to explore the solar system to prevent the earth's destruction. And what's better to choose your next destination than a totally off-scale map of the solar system?
Image
Name in code: planets.jpg
A map is cool, but you need to mark locations on it, spots on which the user can click to do something. An imagemap hotspot is a rectangular area defined by the coordinates of its upper-left corner, its width and its height. A picture is worth a thousand words:
Image
Demonstration picture, not actually used in the code
Next, most of the time, you want to give the user some kind of visual feedback when he or she rolls over a clickable area. For that, we need to create a second image where the hotspots are all in "hover mode".
Image
Name in code: planets-hover.jpg
Note that we only needs the hotspots for this layer, and we do not care at all about the rest of the picture.

Once we got this two images done, the actual coding can begin.

Code

Once you got all the images ready, the code is pretty straightforward.

For each hotspots, we feed the function with its coordinates (in order: x position of upper-left corner, y-position of upper-left corner, width, height), and an action to be performed on click. Here, the action is to jump to the specified label. Note that you must use the syntax `Jump("label")`, with quotes, uppercase and brackets instead of the standard `jump mercury`.

Code: Select all

screen planets: #Preparing the imagemap
    imagemap:
        ground "planets.jpg"
        hover "planets-hover.png"
        
        hotspot (62, 399, 90, 91) clicked Jump("mercury")
        hotspot (227, 302, 141, 137) clicked Jump("venus")
        hotspot (405, 218, 164, 118) clicked Jump("earth")
        hotspot (591, 78, 123, 111) clicked Jump("mars")

# The game starts here.
label start:
    "This is an imagemap tutorial."
    jump solar_system

label solar_system:
    call screen planets #Displaying the imagemap

label mercury:
    "It is Mercury."
    jump solar_system

label venus:
    "It is Venus."
    jump solar_system

label earth:
    "It is Earth."
    jump solar_system
    
label mars:
    "It is Mars."
    jump solar_system
And that's it. We have a functional map.

Unlockable locations

Sometimes, some locations are not available from start, or are disable after some time. Here, let's imagine the game continues after the earth has been destroyed.

Images

First, we need to prepare two new images.

One complete image, where all the locations which can be unavailable at some point in the game must be drawn in their unavailable state. This will become our new default image.
Image
Name in code: planets-no-earth.jpg
One partial image, where all the locations which can be unavailable are in their available state.
Image
Name in code: earth.png
The hover image stays the same.

Code

To activate or deactivate a hotspot, we just need to add a simple condition in the imagemap statement. Note that we changed the default image for the one where the earth is destroyed (inactive).

Code: Select all

screen planets: #Preparing the imagemap
    imagemap:
        ground "planets-no-earth.jpg"
        hover "planets-hover.png"
        idle "earth.png"
        
        hotspot (62, 399, 90, 91) clicked Jump("mercury")
        hotspot (227, 302, 141, 137) clicked Jump("venus")
        if not earth_destroyed:
            hotspot (405, 218, 164, 118) clicked Jump("earth")
        hotspot (591, 78, 123, 111) clicked Jump("mars")

init python:
    earth_destroyed = False

# The game starts here.
label start:
    "This is an imagemap tutorial."
    jump solar_system

label solar_system:
    call screen planets #Displaying the imagemap

label mercury:
    "It is Mercury."
    jump solar_system

label venus:
    "It is Venus."
    jump solar_system

label earth:
    "It is Earth."
    "As soon as you quit it, it is destroyed!"
    $ earth_destroyed = True
    jump solar_system
    
label mars:
    "It is Mars."
    jump solar_system
The rest is Ren'Py magic.

References:
Image source: http://commons.wikimedia.org/wiki/File:Solar_sys.jpg (public domain)
Technical documentation: http://www.renpy.org/doc/html/screens.h ... statements

PS: What is the difference between creating a recipe here or on the Ren'Py wiki?

Re: A simple navigation map tutorial

Posted: Sun Jul 21, 2013 10:56 pm
by PyTom
Skarn wrote:PS: What is the difference between creating a recipe here or on the Ren'Py wiki?
The wiki is massively out of date, and you need to create an account to post there, and I have to approve that account. (Since spam. Tons and tons of spam.)

I'd also suggest that this tutorial could be a bit better if it used screens, rather than renpy.imagemap.

Re: A simple navigation map tutorial

Posted: Mon Jul 22, 2013 4:14 pm
by Skarn
Sorry, I did miss that renpy.imagemap was deprecated. I have updated the code to use the screen syntax (thanks to Aleema Customizing Menus Tutorial). Screens being a lot more powerful than old renpy.imagemap, I will probably make a bigger update later to cover some of the new possibilities they offer.

Re: A simple navigation map tutorial

Posted: Tue Jul 23, 2013 11:26 am
by Sslaxx
I have potentially rather stupid questions here: does the imagemap have to be the same size as the game's resolution? And does the "hover" image have to be just the "hovering" hotspot images?

EDIT: The answer seems to be "No" to both of my questions. Odd, because it wasn't working properly at first. Hmmm. Wonder if this is a persistent data issue biting me in the arse.

EDIT 2: Or is it the imagemap cache? Hmmm.

Re: A simple navigation map tutorial

Posted: Wed Feb 26, 2014 4:50 pm
by SBG_Eric
Hey, this is a really nifty piece of code but I have a question about working with it.

Code: Select all

# The game starts here.
label start:
    "This is an imagemap tutorial."
    jump solar_system
Can I build this into a menu rather than using it directly in the game's Script.rpy file? The general idea would be to have an menu button that would take a player to the game's overland map for fast-travel. Thanks for your help, and great job with the tutorial!

Re: A simple navigation map tutorial

Posted: Thu Feb 27, 2014 2:20 pm
by noeinan
This is really cool, thank you for posting! Do you have any rules or requirements for how you would like the images or code used? I ask because I've been making a Ren'Py quickstart including a lot of features new users might like, but can't yet code and I think this would be a great addition.

Re: A simple navigation map tutorial

Posted: Thu Feb 27, 2014 2:25 pm
by SundownKid
PyTom wrote:
Skarn wrote:PS: What is the difference between creating a recipe here or on the Ren'Py wiki?
The wiki is massively out of date, and you need to create an account to post there, and I have to approve that account. (Since spam. Tons and tons of spam.)

I'd also suggest that this tutorial could be a bit better if it used screens, rather than renpy.imagemap.
I did a check on the Cookbook a while back and (almost) nothing there is so out of date as to be unusable. Personally I like the idea of an index page better since a forum is harder to navigate based on topic. If you do end up removing the rest of the Wiki documentation, I feel that the cookbook aspect of it should be kept and expanded.

Re: A simple navigation map tutorial

Posted: Mon Mar 03, 2014 3:26 pm
by marigoldsofthenight
Let's say I want each planet to be able to be nullified like the earth. Do I need to make separate screens for each individual one?

Re: A simple navigation map tutorial

Posted: Thu Apr 24, 2014 11:59 pm
by Eligar
I'm wondering, if I want more then 1 overworld map, how would I go about doing that? Seeing as

"screen planets: #Preparing the imagemap
imagemap:
ground "planets.jpg"
hover "planets-hover.png"
etc.

Happeneds 'before' your labels. I want 2 overworld maps. 1 located in town, the other located within a building. Is that possible?

EDIT: Oh wait, of course you can, I'm just being a dummy. Just add another:

"screen town: #Preparing the second imagemap
imagemap:
ground "town.jpg"
hover "town-hover.png"

Re: A simple navigation map tutorial

Posted: Sun Apr 27, 2014 5:37 pm
by Eligar
Right, so I'm still having trouble with this. In your tutorial you give a specific set of coordinates. However if I move those coordinates, it takes the Hover map with it. (See picture)

Image

I moved the coordinates from their original position and moved the y axis up a bit. Seeing as in my world map the location is a bit higher.
I expected it worked like this:

ground = underlying map
hover = overlying map with user feedback (like a glow, or a change of color, or in your example a circle around it.) which is hidden until you mouse over it.
hotspot = Picking 1 spot on the map which reveals the hover when you mouse over it.

Any help is greatly appreciated.

Re: A simple navigation map tutorial

Posted: Sun Apr 27, 2014 5:50 pm
by Alex
I moved the coordinates from their original position and moved the y axis up a bit.
Have you changed all the images used for the imagemap to make them fit the new coordinates?
If so, try to clear the cache - http://www.renpy.org/wiki/renpy/FAQ#Why ... correct.3F

Re: A simple navigation map tutorial

Posted: Sun Apr 27, 2014 5:56 pm
by Eligar
Alex wrote:
I moved the coordinates from their original position and moved the y axis up a bit.
Have you changed all the images used for the imagemap to make them fit the new coordinates?
If so, try to clear the cache - http://www.renpy.org/wiki/renpy/FAQ#Why ... correct.3F
Oh man, I tried removing my reply the moment you posted haha. Yeah it was a cache issue. Okay, that'll help me one step further. Thanks :)
By the way, the wiki reads:

"You can disable caching all together by using this code:

config.imagemap_cache = False"

Where would in my code would I put that line?

Edit: By the way, Thanks for the fast reply!

Re: A simple navigation map tutorial

Posted: Sun Apr 27, 2014 6:03 pm
by Alex
You can put it inside any init python block or inside any init block (but don't forget about $-sign) - http://www.renpy.org/doc/html/python.html

Re: A simple navigation map tutorial

Posted: Thu Feb 12, 2015 12:51 am
by steve_T
Thanks this is exactly what I was looking for. :D

Re: A simple navigation map tutorial

Posted: Sat Feb 28, 2015 10:24 am
by Ishigreensa
Question: Can I use?
screen choice:

imagemap:

if grab == 'xxx':
hotspot ( ) clicked Return('xxx')

or can I only use IF ... True in screen language?