MV Transparent Buildings for MV

I can't imagine I'm the first one with this problem, but I could'nt find any other relevant post about this.

I want a Building to become transparent, when the player walks behind it.
So that the base of the building is still visible (maybe I draw it directly on the background map) but the upper floors become transparent so the player can see whats behind it.

Im creating a city with high buildings and narrow streets, so this is quite essential for me ^^
I'm not sure how this process would benefit from a plugin. People typically use events to show/remove images, or something like Shaz's Tile Changer.


I made a scriptcall with that kind of thing, and looks like this:
I this kind of thing you look for?
_Transparancy_.gif

as I made this thing as test. and require 2 things to make it work by than,
but not sure if you want to do that though.
I made a scriptcall with that kind of thing, and looks like this:
I this kind of thing you look for?
View attachment 254035
as I made this thing as test. and require 2 things to make it work by than,
but not sure if you want to do that though.
Thats exactly what I'm looking for. I've found an quite complicated way to do this, but I hope there is something a bit easier for this - how did you do this in the test?
roofs like this is made on the image and transparant below it.

the scriptcall is kinda complicated as I'm still looking for an easier way.
but I'm planning it into a plugin using a function like

sizeRoof = function() and use sizeRoof() to call it when possible.
I dont know if there is an easy way, but I use this scriptcall (you need to
set it in 1 line (as I use 8 lines in total to see where it starts, and where it ends.


JavaScript:
//CODE FOR PAGE 1
if ((((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+0) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-2) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-1) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+1) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+2))
&&
((($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-1) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-2) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-3) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-4) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-5) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-6)))
&&!
(((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-1)) ||
((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-6)) ||
((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-1)) ||
((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-6))))
   {
     $gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], true);
   }


    //CODE FOR PAGE 2:

if ((((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+0) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-2) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-1) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+1) ||
(($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+2))
&&
((($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-1) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-2) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-3) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-4) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-5) ||
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-6)))
&&!
(((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-1)) ||
((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)-2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-6)) ||
((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-1)) ||
((($gameVariables.value(1)) == (($gameMap.event(this.eventId())).x)+2) &&
(($gameVariables.value(2)) == (($gameMap.event(this.eventId())).y)-6))))
    {
    }
else
    {
        $gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], false);
    }

its a bit complicated to explain how it works, as I made it 2 years ago for
mechanics I make, but the numbers are 2 variables, that also run in a
parallel process of constant checking variable 1 and 2 for their x and y
position of the player.

the event ID.x and y checks if the player is INSIDE this roof size, if it is,
it makes it transparant, else it doesn't.

I still want to make it easier in a way to set 0 through -4 to read it all,
but I dont know if it's possible for that, but its what I use at the moment.

its also alot of error and trials for testing to see if it works, this one is made for
a 3 x 7 roof, but the more complex the roof is, the more code is added.

but you can go crazy on shapes.
This looks like... a good moment for a short lesson in code refactoring 😉

First useful thing: if you're using the same value multiple times, it's generally a good idea to assign it to a descriptive variable. You may or may not make your code shorter, but you'll definitely make it easier to understand. That will bring us to:

JavaScript:
let playerX = $gameVariables.value(1);
let playerY = $gameVariables.value(2);
let eventX = $gameMap.event(this.eventId()).x;
let eventY = $gameMap.event(this.eventId()).y;

if ((((playerX == eventX+0) || (playerX == eventX-2) || (playerX == eventX-1) || (playerX == eventX+1) || (playerX == eventX+2))
    &&
    ((playerY == eventY-1) || (playerY == eventY-2) || (playerY == eventY-3) || (playerY == eventY-4) || (playerY == eventY-5) || (playerY == eventY-6)))
    &&!
    (((playerX == eventX-2) && (playerY == eventY-1)) ||
    ((playerX == eventX-2) && (playerY == eventY-6)) ||
    ((playerX == eventX+2) && (playerY == eventY-1)) ||
    ((playerX == eventX+2) && (playerY == eventY-6)))) {
        $gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], true);
}

Next, look for repetitive patterns that can be expressed with something simpler. In this case, one thing that stands out is checking if playerX is equal to one of 5 nearby values. How would you describe a value that's equal to either x-2, x-1, x ,x+1 or x+2? It's a variable with a value between x-2 and x+2. This can be replaced with <= and >= operators, like so:

JavaScript:
let playerX = $gameVariables.value(1);
let playerY = $gameVariables.value(2);
let eventX = $gameMap.event(this.eventId()).x;
let eventY = $gameMap.event(this.eventId()).y;

if (playerX >= eventX-2 && playerX <= eventX+2 && playerY <= eventY-1 && playerY >= eventY-6
    &&!
    (((playerX == eventX-2) && (playerY == eventY-1)) ||
    ((playerX == eventX-2) && (playerY == eventY-6)) ||
    ((playerX == eventX+2) && (playerY == eventY-1)) ||
    ((playerX == eventX+2) && (playerY == eventY-6)))) {
    $gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], true);
}

Now, what about the rest? There's no such simple pattern here, and even if there was, we're trying to make this compatible with a wide range of shapes. So, here's another good idea when writing code: try to separate logic and data. That means that the logic that handles something (like checking if the roof should be displayed) should be separate from the data that it uses (like the tiles on which the roof exists). This will make it a lot easier when you want to change said data. There are multiple ways we could do that here, but let's store the exceptions in an array of two-element arrays.

JavaScript:
const exceptions = [[-2,-1], [-2,-6], [+2,-1], [+2,-6]];
let playerX = $gameVariables.value(1);
let playerY = $gameVariables.value(2);
let eventX = $gameMap.event(this.eventId()).x;
let eventY = $gameMap.event(this.eventId()).y;

if (playerX >= eventX-2 && playerX <= eventX+2 && playerY <= eventY-1 && playerY >= eventY-6){
    for (let i = 0; i < exceptions.length; i++) {
        if(playerX === eventX + exceptions[i][0] && playerY === eventY + exceptions[i][1]) return; 
    }
    $gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], true);
}

Granted, this might be more difficult if you're unfamiliar with arrays or for loops (in which case, I do recommend learning those 😉 ) - but this will basically check one by one every element in the exceptions array, and abort if it finds a match.
Small note: there's no difference between +2 and 2 in this context - it's just decorative.

All of this isn't just about making the code shorter - you can now much more easily change the dimensions of the object and the list of exceptions 🙂

EDIT: Fixed the indexes on exceptions
EDIT2: And also fixed the directions of comparisons. Whoops.
This looks like... a good moment for a short lesson in code refactoring 😉
this is something I made to try learning JS and find a way, but this is definitely helping in
shorten the codes, just need to get what you did by comparing them.

but its way shorter.

I use player x and y in common event, so its already constant checking, but eventXY is something
that could be added, but the other are 2 pages, 1 for ON, other for OFF, so it can help me, and the OP
to use it nicely 🙂

thank you HUGE for the nice explaination.

Small edit:
if (playerX <= eventX - 2 && playerX >= eventX + 2 && playerY >= eventY - 1 && playerY >= eventY - 6){
shouldn't this be >- event x -2 and <= eventX +2 as it checks inside the rage, as it or does it
those read inside and not outside? which confuses me now, which I think that should be correct
when you step out of it?

but if its correct, than all good 🙂 still try to get the formula you made. and its loop.
I'm glad you found this useful 🙂
shouldn't this be >- event x -2 and <= eventX +2 as it checks inside the rage, as it or does it
those read inside and not outside? which confuses me now, which I think that should be correct
when you step out of it?

Whoops, you are correct here 😅
I've fixed it in the post.
the same should for the Y than as well, but its still a nice learning thing for a much shorter code.
rather than a huge list of checks.

from 3 shapes of 20kb to 5kb, so its a huge different and more readable, so nice job ^^

edit:
on you second code is the same with double => , but its easy to spot that mistake if you know
at least a bit of JS or basic of operators. ^^

everyone make mistakes, so you only learn from it.

Original thread: https://forums.rpgmakerweb.com/threads/transparent-buildings-for-mv.155200/