Code organization: what's wrong and how to improve?

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
bbg
Newbie
Posts: 2
Joined: Thu May 24, 2018 2:05 am
Contact:

Code organization: what's wrong and how to improve?

#1 Post by bbg »

Are there some best practices of code organization for Ren'Py?

For now (I'm new in Ren'Py) it seems to be handy to have:
1) one (or several in 2-3 layers if the game is big) of "manager" files containg neigther text nor media content, but just main plot logic: they contain "start" label, keep and change main variable, implement high level forks depending on decisions done in "content" files (see below) and call all other files in corresponding time dependingly on plot logic (jumps use seems very strange and confusing to me, probably because of my previous Java background)
2) content files, each of them covering one part of plot (one dialog, one evening at home, one dream etc) and divided into "logic" section (starting with main file label, ending with "return" and relatively short section where all plot structure is kept: forks, variables changes) and "content" section (containing linear sequences of content, like text, images, sounds, character pictures to show to the user, each sequence starting with a special label that should be called from logic section and ending with "return").
3) service files, defining all game characters, mini-games, effects etc and called probably at the very start of the game.

So far such a systems seems to me clear and easy to use except one detail: if I understood correct, all labels in the game are available globally, so I can't have just label like "ask_where:" but rather something like "day_2_sam_dialog_1_ask_where_is_my_dog:" (that is definitely ugly) to avoid labels duplication despite that labels are mostly used just in one file and have no need to be accessible outside of it.

I suspect that I just see and do something wrong here, but what? Are there some locally available labels or something? Or should the game code be organized different to avoid this problem ando another ones I did not face yet? What kind of Ren'Py code organization is considered by the community around it to be really good?


User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Code organization: what's wrong and how to improve?

#3 Post by ComputerArt.Club »

You could use the 'call' command instead of 'jump' to solve your problem related to:
"day_2_sam_dialog_1_ask_where_is_my_dog:"
Make it generic, e.g. walkdog
You can add some if statements to vary the content depending on how many times you've done it or whatever.

'Call' will return you to your previous position afterwards, 'jump' will play whatever label is under walkdog.

Organization can be done in whatever way suits your needs.

You can have local labels inside labels, check the docs. I use them but no time to explain any more right now.

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: Code organization: what's wrong and how to improve?

#4 Post by Remix »

There are local and global labels, though please note that variables within local labels are still global

Imagine, very basically, you opted to have one file per character, bob.rpy and sid.rpy...with each holding labels named act_NN

Code: Select all

## bob.rpy

# this defines the global label name
label bob:
    "Nothing here"

# note the . (dot/period) making this local to bob
label .act_1:
    "... no global referenced, so uses its own ( effectively bob.act_2 )"
    jump .act_2

label .act_2:
    "... now uses global reference from other file to jump there... "
    jump sid.act_5
Frameworks & Scriptlets:

bbg
Newbie
Posts: 2
Joined: Thu May 24, 2018 2:05 am
Contact:

Re: Code organization: what's wrong and how to improve?

#5 Post by bbg »

Thanks for all the answers! Seems that local labels are exactly what I need; too bad I haven't learnt about them by myself.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]