Call another map event to avoid duplicating code

Hello, I want to know if there is any way to call another event in the map to avoid duplicating code.
For example, I have this bed:
Captura de pantalla -2023-03-01 16-36-17.png

And I want to make the player be able to sleep using any of those 2 events, but I don't want to replicate the code in both of them. Is there anyway to call one of them from the other? I tried this.character(event_id).start(); but it doesn't work in this case.

I know there is an option to call remote events in one Visustella plugin, but that plugin is to call a specific page from another event (It also adds a lot of other features that I do not want) and I don't want that, because I want the page to be automatically selected same as if the user were triggering the goal event

Thanks a lot in advance for your answers
Easy no plugin way: Have a temp variable, name it... x or something.

Have both events set a value to variable x, have a separate event with condition x (and a dummy page after with value + 1 as the condition so that you can reuse x on the same map) that autoruns and at the end of the event set variable x to 0 again.

Now both events activate the same event.
I may be misunderstanding the question, but is there a reason you can't just use a common event for that?

I mean one of the purposes of common events is exactly that; creating multiple events with the same event commands.
I may be misunderstanding the question, but is there a reason you can't just use a common event for that?

I mean one of the purposes of common events is exactly that; creating multiple events with the same event commands.
Yes, the absurd limit on common events, and also to have clean code: To be able to have the code related to a specific part of the map in that part of the map
After checking the code of the start function in the event prototype looks like
$gameMap.event(event_id)._starting=true
does the trick.

Anyone knows any contraindications?
This could be even better $gameMap.event(event_id).start()
After checking the code of the start function in the event prototype looks like

does the trick.

Anyone knows any contraindications?
That seems like a reasonable way to do it - I think it might introduce a one-frame delay until the interpreter notices there's another event it needs to run, though I'm not sure of it. If I'm correctly assuming that the events that call this function do nothing else it should be fine.
You could also put your code you want to trigger on page 2 of just one of the events, with the condition of a self switch.

That event has, on page 1 when you trigger it, a command to turn on that self switch and activate the sleeping stuff.

Then page 1 of the other event uses $gameSelfSwitches.setValue() to turn on the same self switch on the first event and trigger its page 2 code.

Even if you find it more efficient in this specific instance to use the start() method above, I find this a super useful way to keep event code contained in a map and not clutter up common events/variables/switches.
Yes, the absurd limit on common events, and also to have clean code: To be able to have the code related to a specific part of the map in that part of the map
I'll be real I think your over attachment to doing this in code is the problem. The user suggesting common events had the right answer IMO, even with its many restrictions in consideration
I'll be real I think your over attachment to doing this in code is the problem. The user suggesting common events had the right answer IMO, even with its many restrictions in consideration
I don't think that's a very helpful contribution.

There's nothing wrong or harmful with the way the OP wants to organize it, and there's no "over attachment" in evidence - if one wants to keep it contained to events on the current map without using global variables or switches or events, script calls are the only solution.

The concept being practiced here is generally recognized as a good one - it's basically the programming concept of encapsulation, which is that anything only affecting a specific object (here, the map) should be contained wholly within that object.
Yes, the absurd limit on common events
You know that MZ's limit on common events is 9999, right? I'm not trying to convince you to do things differently from how you've decided to do them, but I just thought that I'd point that out, since (to me) your diction implies that you're under the impression that the limit is much lower than it actually is.
You know that MZ's limit on common events is 9999, right? I'm not trying to convince you to do things differently from how you've decided to do them, but I just thought that I'd point that out, since (to me) your diction implies that you're under the impression that the limit is much lower than it actually is.
If any small thing needs (every bed... every item interaction) to be a common event, 10.000 is not even close enough. And definitely is not clean code
I'm actually doing exactly the same thing right now in all the repeated flavour events on the project I'm contributing to currently. $gameMap.event(id).start() is the approach I took.
If any small thing needs (every bed... every item interaction) to be a common event, 10.000 is not even close enough.
To be fair...it doesn't. You can put the code for every bed and every item directly into the event for that bed or item. There's no need, inherently, to call a common event for that at all.

I presumed your original post was asking all of this because you "didn't want to duplicate the code" that you were using to trigger some kind of cutscene or other story event and didn't want to allow the possibility of it happening twice.

If you're just walking up to a bed and sleeping in it, done...it takes almost zero effort to just copy the event. Whether that's copying the event page from another bed or clicking on another actual bed event on your map and copying that.

At that point, you're talking about saving kilobytes by "not duplicating" that code, it's supremely inconsequential.
If any small thing needs (every bed... every item interaction) to be a common event, 10.000 is not even close enough. And definitely is not clean code
I didn't advise you to make every small thing a common event. My point was simply that the limit isn't "absurdly" small. Unless you're making Elderscrolls VI, then "I'm worried about hitting the limit" is very unlikely to ever be a valid reason to avoid using a common event. If you use common events when it makes sense to use them, and don't use them when it doesn't make sense to use them, then it's unlikely that you'll even use 1/3 of the limit by the time your game is done.
I didn't advise you to make every small thing a common event. My point was simply that the limit isn't "absurdly" small. Unless you're making Elderscrolls VI, then "I'm worried about hitting the limit" is very unlikely to ever be a valid reason to avoid using a common event. If you use common events when it makes sense to use them, and don't use them when it doesn't make sense to use them, then it's unlikely that you'll even use 1/3 of the limit by the time your game is done.
But then you are saying "some map events are common events, and some map events are map events"
Then when you try to remember... "Where is the code of sleeping in beds?", is hell if just because of technical limitations and not real code design, some pieces of code are going to be in one place and others in another
At that point, you're talking about saving kilobytes by "not duplicating" that code, it's supremely inconsequential.
It is not to save bytes, it is because code requires maintenance and to do that you need to know exactly where each piece of code and it should be only once, because otherwise when you need to change the bed behaviour you are at high risk of introducing bugs: https://en.wikipedia.org/wiki/Don't_repeat_yourself

I may be new at RPGMKMZ but I'm making code for 22 years now
It is not to save bytes, it is because code requires maintenance and to do that you need to know exactly where each piece of code and it should be only once, because otherwise when you need to change the bed behaviour you are at high risk of introducing bugs: https://en.wikipedia.org/wiki/Don't_repeat_yourself

I may be new at RPGMKMZ but I'm making code for 22 years now
I understand what you're getting at, but it's just an event to go to bed. Both events are not only next to each other, but unless you're going into unnecessarily complicated territory, the events aren't gonna contain massive chains of event commands that run the risk of causing bugs. You are overthinking this.
I understand what you're getting at, but it's just an event to go to bed. Both events are not only next to each other, but unless you're going into unnecessarily complicated territory, the events aren't gonna contain massive chains of event commands that run the risk of causing bugs. You are overthinking this.
You have no idea what happens in my game when you go to bed, you just assume is something easy. Maybe in my game everything happens in a dream and then going to bed is the most complex event in the entire game. I'm going to end the discussion here because now is nonsense and all technical conversation is lost.
it is because code requires maintenance and to do that you need to know exactly where each piece of code and it should be only once, because otherwise when you need to change the bed behaviour you are at high risk of introducing bugs
Okay, but your RPG Maker project is not a pile of source code. It has graphical maps with a visual interface. If you're telling me that you decide to change how beds work and it's difficult to click through your list of maps and visually see where any beds are to paste over them, I just don't believe you 😛

For that matter, beds probably don't really matter at this point in your project. If you have some fear that they're going to change in development, just don't put code on them right now, or just put a placeholder message.

But then you are saying "some map events are common events, and some map events are map events"
Then when you try to remember... "Where is the code of sleeping in beds?", is hell
The intent is for it to be quite logical. Everything is on the map unless it doesn't pertain to one, or is duplicated so frequently that you put it into the common events.

No one has said that it might not make sense for you to put your beds into a common event. In fact, it was the very second response to you in post #3.

You are the one who extrapolated that into the silliness of
If...every item interaction...[needs] to be a common event
which is not what anyone said to you.

You really are coming off as being obstreperous here. Thousands of people have used the various RPG Maker engines and have no problem defining and keeping track of "this code is contained in a map and this code needs to be outsourced to a common event."

I did initially defend your right to ask about this and implement this way (and you absolutely still have the right to implement it however you like) but I think you're being intentionally ridiculous.

Good luck with your project.

Original thread: https://forums.rpgmakerweb.com/threads/call-another-map-event-to-avoid-duplicating-code.155419/