Beautiful Scripts - applying clean code principles to events

The process of building a game has gotten me staring at event scripts for hours on end, bugging and de-bugging the systems in an endless loop. But - while there is a lot of helpful advice out there on RPG Maker - there's very little that deals specifically with the architecture of the systems that are (in theory) the main attraction in our games. If you want to learn what a switch does, there's a tutorial. Need a plugin? There's a thread. But virtually nothing talks about writing scripts that are effective, easy to read, easy to maintain etc....seeking out that information led me deep into the rabbit-hole of software engineering.

Programmers take this stuff very seriously, and it's been instructive to seek out the likes of Uncle Bob and other giants in the world of code.....but I am not a computer programmer, and what works in Javascript, Python, Ruby and C# doesn't always apply directly to what I'm doing.

I'm interested in what advice the RPG Maker community has for creating event scripts that read like software poetry. Advice on expanding my ability to make evocative, effective systems....not lines of code that solve one specific problem, but coding principles that will help me solve thousands of problems in the future.
coding principles that will help me solve thousands of problems in the future.

you mean..... *programming*
There's paradigms in programming call declarative programming and imperative programming. There's endless debates over which is "better", but people who debate that are generally missing the point. The point is that you need both, but how you split up which is which can make a huge difference in how *clean* your code is.

The same concept can be applied to events.

You can create Common Events that are imperative, as in they list out every step that's needed to do a certain action. Then you name that Common Event declaratively, as in you name it "Do Something".

When you want to use that Common Event later in your on-map events (or other Common Events), you don't have to worry about how "Do Something" works. You don't even care how it works. In your on-map event, you can just look at it and you know what it's doing, the implementation details of the "Something" are hidden to you. Your event is declarative.

(If you want to read more about declarative and imperative in a way that actually makes sense to a regular person)
OK, taking an example from my game, I have hand-placed fights that trigger on player touch (rather than random battles), and each event is rigged up with:
A sound effect
A "you found a thing" message
Its own boolean (nullifying the event after it's completion)
A variable (counting how many of each enemy you've killed)
A boolean which triggers a secret boss fight (based on the variable)

Your suggestion would fold that into:

A variable (identifying which nullify boolean to operate)
A common event "Rattlesnake Fight" (which plays the sound effect, counts the variable, and triggers the optional boss)

That makes a lot of sense! Thanks for the tip and the article. This is an easy thing to internalize and put into practice....and it will make my life easier (I just spend an hour changing one line in each of the 14 snake fights....)
not lines of code that solve one specific problem, but coding principles that will help me solve thousands of problems in the future.

Then what you'd want to do is actually to improve on your logical thinking skills and problem analysis..

Coding itself is practically making a solution to a certain problem/idea so if you know how to solve problems, it becomes easier to know what kind of code you need to solve it. This skill will also hold true no matter which language you use.

Next, familiarize yourself with the features of the language/system that you are using so that you can translate your solution into actual code that works on your language/system
@ExquisiteCorpse Yup so that's also known as D.R.Y. (Don't Repeat Yourself). If you find yourself doing something more than once, instead write a generalized version and then each time call that general version with the specific parameters. In programming, this would be writing a function, call that function with different arguments. In eventing, that would be create a Common Event that uses certain variables, and call that common event each time.

The imperative and declarative distinctions can be used together with D.R.Y. but the concept is a little different. Let's say you have a point before the final boss that you want to auto-save the player's progress so they don't have to watch the long cutscene again if they die on the final boss.

This example is a one-time thing, so the D.R.Y. principle doesn't apply here.

You could create an event like this:

upload_2019-4-19_10-5-29.png


This is fine for the D.R.Y. principle, you're not repeating yourself, since it's a one-time thing (there's only one final boss). But maybe you'll work a lot on other parts of this final boss event, refining your epic dialogue. Each time you want to remember where the autosave is, you gotta read each line of that code. It's imperative, so it describes each step of HOW to autosave. Sure, it's only 5 lines of code so it's probably fine, but with more complex examples it can be harder to read.

The declarative approach is to move that code to a Common Event, even though it's only called once. Then in your final boss event:

upload_2019-4-19_10-10-5.png


Now the autosave is declaring WHAT to do. The details of how the autosave is implemented are hidden in the Common Event, you don't care anymore about those details.

You'll notice that basically all the event commands are declarative - take the 'Show Picture' for example. In your eventing, you don't say "load an image to memory, create a bitmap from the image, add the bitmap to a sprite, show the sprite in the scene". You just say Show Picture, and it does exactly that - the imperative details are hidden from you.

Using Common Events has the benefit of making your events more declarative (as in, more readable) as well as allowing you to not repeat yourself. Not repeating yourself leads to fewer lines of code (or fewer event commands), which makes it more readable. So, doing both D.R.Y. and splitting imperative and declarative code will improve readability.
Can I have a little clarity here please. There is no such thing as an event script.
Are we talking about event commands (in which case this goes to the main Support forum), or are we talking about scripts, (in which case it goes to 'Learning Ruby and RGSSx')? It has to go somewhere else, as this section is explicitly not about implementation.
Which version of RPG Maker are you using? I highly recommend looking into YEP's Event Spawners. Here's why:
Say you want to make a bed event that runs commands to choose whether to just sleep, read a book, and handles a chance for ambush. You'll logically be putting a clone of this event in many different maps.

Sure you can put all the code in a common event, but that list of common events is 1D and can quickly become unwieldy. What you can do instead is create a templates map with the bed event there, complete with all its event code. Then have the spawner plugin spawn the bed events automatically using name links. Not only is this event only in one place in your project (meaning any changes there will affect every instance of it), but it's also in 2D space, reducing visual clutter. You can even use multiple maps for templates if you also want to categorise them.
I'm using MV for the present project.

The event spawner idea looks like it could save a lot of hassle. There are common events all over my maps that do things like bug-fix audio cues, and my common event list is already at 200 and filling up fast.
Although you neglected to answer my question, your last post makes it clear that this is about implementation.

Moved to: RPG Maker MV
@Kes No disrespect was meant...I just don't have a grasp on the precise definitions of all these terms (the concept of my thread contains something that doesn't exist; syntax is not my strong suit).

I have here one of my more complicated common events; it's a vital system that is referenced often and will grow as the game gets bigger; I'm not going to say what it does because I would like others to read it and critique it for clarity and ease of expansion. It's as simple as I can make it at my current skill level...but I have a feeling it can be made simpler.

Thanks again, folks! This thread has been very helpful

Spoiler: Common event: Django recover
◆Comment:Drinking tolerance: 6
: :Blackout tolerance: 12
: :HP+ per lvl: 3

◆Comment:Bourbon 5 hlth
: :Fine wine 3 hlth
: :Dead cat 1 hlth
◆If:Django is in the party
◆Jump to Label:FULL HEALTH?
◆
:Else
◆Jump to Label:EXIT
◆
:End
◆Label:FULL HEALTH?
◆Control Variables:#0014 Current Health = HP of Django
◆Control Variables:#0015 Max Health = 3
◆Control Variables:#0015 Max Health *= Level of Django
◆Control Variables:#0015 Max Health += 27
◆If:Current Health ≥ Max Health
◆Jump to Label:EXIT
◆
:Else
◆Jump to Label:OLD DOG
◆
:End
◆Label:DRUNK
◆If:Django Drunk ≥ 12
◆Change State:Django, + Knockout
◆Text:Django smug(0), Transparent, Top
: :Collapses drunk
◆
:Else
◆If:Django Drunk ≥ 6
◆Change State:Django, + Drunk
◆
:End
◆Jump to Label:OUT
◆
:End
◆Label:OLD DOG
◆If:Django has equipped Old Dog Bourbon
◆Control Variables:#0004 Django Drunk += 2
◆Change HP:Django, + 5
◆Control Variables:#0013 Gained Health = 5
◆Jump to Label:DRUNK
◆
:End
◆Label:FINE WINE
◆If:Django has equipped "Fine" Wine
◆Control Variables:#0004 Django Drunk += 1
◆Change HP:Django, + 3
◆Control Variables:#0013 Gained Health = 3
◆Jump to Label:DRUNK
◆
:End
◆Label:DEAD CAT
◆If:Django has equipped Dead-Cat Ale
◆Control Variables:#0004 Django Drunk += 1
◆Change HP:Django, + 1
◆Control Variables:#0013 Gained Health = 1
◆Jump to Label:DRUNK
◆
:End
◆Comment:*****************************(new script before output)***********************
◆Label:OUT
◆If:Heal Sound is OFF
◆Play SE:Ice1 (50, 70, 0)
◆Control Switches:#0021 Heal Sound = ON
◆
:End
◆Text:Django face(0), Transparent, Top
: :+\v[13] health!
◆Control Variables:#0013 Gained Health = 0
◆Label:EXIT
◆Exit Event Processing

Original thread: https://forums.rpgmakerweb.com/threads/beautiful-scripts-applying-clean-code-principles-to-events.108110/