MV How to: check what function is calling a specific function

Hi!

In the past I was looking after how to find the name of a calling function. The answer was arguments.callee.name. But as many, or some of you know, this has no use if you're using it via prototypes. This wouldn't work for example:
JavaScript:
Window_Command.prototype.callOkHandler = function() {
    console.log(arguments.callee.name); //This simply returns "".
    //remaining code.
};

So what if you're testing something, and you need to use console.log like this:
JavaScript:
Window_Command.prototype.callOkHandler = function() {
    console.log("test");
    //remaining code.
};

And, then, upon hitting enter once, the console.log triggers three times. Indicating that the function itself is called three times. This would naturally make you want to investigate a potential efficiency leak. But if you have a bunch of plugins, even if you're just using Yanfly, there could be a lot of things causing these issues. Perhaps you're the one calling it twice?

Regardless, if you want to find that specific function that's calling you. It's not as easy as just opening all your plugins and all core files and then using a potential search all opened documents and using the search term "Window_Command.prototype.callOkHandler". Because it isn't gonna say that, it's gonna say "this.callOkHandler". And searching for this would give you, potentially, depends on the prototype, an insane amount of results. This could rack up to hours of exhausting work.

So, what you'd prefer to do is to just use the arguments.callee.name function, but sadly it just doesn't work.
And until today, I had never found a way to do this efficiently. Perhaps there is a way I don't know about, but I've searched many, many times, and never found anything.

So what do you do? (Dummy guide)

1. First, press f8 and write the function in your console and hit enter.
1637101000243.png

2. Now, the console will return the below, then you click the actual function reference. This will take you to the sources tab.
1637101081781.png


3. Now that you are in the sources tab, you will be automatically taken to the selected function.
1637101226501.png


4. Now, you're gonna want to click a specific line number within that function.
1637101297068.png


5. Now all you do is trigger the function, in whatever way you do it. This specific function will trigger by confirming a choice in a command window. And upon doing that, you will see, in the "Call Stack" a call stack trace, in the top right of the sources tab.
1637101527632.png


6. What happens now is that the game pauses it's execution of code completely, I think. Regardless, you are free to view the entire stack trace, and we can see that .processOk triggered the .callOkHandler. And notice that it's called from a different Window entirely.

And lastly, just press the blue play/stop button, located above the "Call Stack" tab.
1637101703891.png

7. And if the function is called by another function, the game will pause, yet again. Allowing you to view the stack trace and find out what functions are calling the function.


Lastly, this feature will remain upon reloading the game. So, it's quite effective for other testing reasons as well. It can cause some issues though, as in the example above. It actually causes the game to think the breakpoint is an OK situation, so the entire combat becomes autoplay.

I hope some people find it useful. I know I do 🙂

Best regards,
Yeah, breakpoints are super useful. I think I mentioned something similar in my post about getting the most out of the console a while back.
Yeah, breakpoints are super useful. I think I mentioned something similar in my post about getting the most out of the console a while back.
I am so happy I found them. I actually found them by accidentally click the line number, then getting really frustrated the game kept breaking without and error appearing. Then I saw the "paused" sectioned 😛
I am so happy I found them. I actually found them by accidentally click the line number, then getting really frustrated the game kept breaking without and error appearing. Then I saw the "paused" sectioned 😛
Do you know about watches as well? You can set it up to always track a particular property/variable so that no matter where you are in the code you can see its current value. I mean you can also mouse over any evaluated identifier in the code during a break to see the value, but it's quicker if you just want to keep track of something specific.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-check-what-function-is-calling-a-specific-function.142212/