MV Code keeps running despite return

Code:
var dist = 10;
  var playerDirection = $gamePlayer._direction;
  var wallReg = 2;
  var playerX = Math.round($gamePlayer.x);
  var playerY = Math.round($gamePlayer.y);
  switch (playerDirection) {
    case 8:
     
    for (let i = 1; i < $dataMap.events.length - 1; i++) {
      var eventX = Math.round($dataMap.events[i].x);
      var eventY = Math.round($dataMap.events[i].y);
      for (let j = 0; j < dist; j++) {
        if (playerX === eventX && playerY - j === eventY) {
          ///shoot enemy
          console.log('is an enemy')
          return shotEnemy(i);
        }else if($gameMap.regionId(playerX, playerY - j) == wallReg){
          ///shot a wall
          console.log('is a wall')
          return shotWall(playerX, playerY - j);
        }
     
      }
     
    }

My issue is that the code that register the shot in the wall have priority despite being on the second conditional and despite the first condition being meet.
despite being on the second conditional and despite the first condition being meet.
What? I lost you there.
I think you may want a break; to exit out of your for loop and then return the value outside of the for loop.
What? I lost you there.
I think you may want a break; to exit out of your for loop and then return the value outside of the for loop.
a return would 'in theory' stop the function there calling the shot, anyways, break also don't work, nor labeled break to the first loop
Hi there!
I guess next time, it is better if you properly format your code to javascript, like this:
JavaScript:
    var dist = 10;
    var playerDirection = $gamePlayer._direction;
    var wallReg = 2;
    var playerX = Math.round($gamePlayer.x);
    var playerY = Math.round($gamePlayer.y);

    switch (playerDirection) {
        case 8:
        
        for (let i = 1; i < $dataMap.events.length - 1; i++) {
            var eventX = Math.round($dataMap.events[i].x);
            var eventY = Math.round($dataMap.events[i].y);

            for (let j = 0; j < dist; j++) {

                if (playerX === eventX && playerY - j === eventY) {
                    ///shoot enemy
                    console.log('is an enemy')
                    return shotEnemy(i);
                }else if($gameMap.regionId(playerX, playerY - j) == wallReg){
                    ///shot a wall
                    console.log('is a wall')
                    return shotWall(playerX, playerY - j);
                }
        
            }
        
        }

    }

Now, about your issue, we need to know where this code is running. If you are running this as an script call, in a parallel event, well, the code will not stop running as long as this event is running. So, what is the context in which this code is being executed?

Did you created this code?
Or this code is from some another plugin?
Or made with I.A?
Did you created this code? yeah

where this code is running? The code runs in a extension of the move by input function that will detect the player input A to aim and trigger S to call the shot fuction.

I kind of solved the situation by refactoring the code and passing a separated list with the eventIDs of enemies, using two different loops for walls and enemies passing each loop on it's own separated functions.

So basically now I have a situation where:
The first loop try to hit an enemy with HP > 0 THEN the second loop try got hit a wall BUT the second loop won't trigger on the first loop return
also from your code, you make it look like you can only shoot 8 when facing up?

as you only check for the dir, but I would rather check the case of "enemy"
or case "wall" where you put those in.

breaking or returning there will stop the switch case untill retriggered.

but if this is only part of the code, we can better see the entire code to see
where you made the mistake and correct that to make it work.

most codes can be used as plugin, and call the scriptcall or trigger function
there to use it everywhere needed.
also from your code, you make it look like you can only shoot 8 when facing up?

as you only check for the dir, but I would rather check the case of "enemy"
or case "wall" where you put those in.

breaking or returning there will stop the switch case untill retriggered.

but if this is only part of the code, we can better see the entire code to see
where you made the mistake and correct that to make it work.

most codes can be used as plugin, and call the scriptcall or trigger function
there to use it everywhere needed.
Well it's just a logical test, once shooting up (8) works the other directions will work without problems. And because of how RPGmaker perspective works the extra logic that is to be implemented like bullet holes are only needed when shooting up.

A switch don't accept complex expressions and some features planned need a more in dept handling what make a switch unnecessary to get the target.

The code is called by the movement function on button trigger, the code posted is the second function on the system and it only calls a function shooting the target, it don't interact with the rest so the only possibility I see was some shenanigan about JS being single thread or the way rpg maker itself handle it.

It was built on top of the move by input function, it's triggered on a combination of button press (a+s)
So, your code is detecting a wall hit when it should have registered an enemy being hit, is that correct? There are a bunch of ways this could happen, like making a mistake in setting up the wall region. Though my guess is that this event might be moving - and if it is, this code is not going to work, since $dataMap.events[i].x only checks the data in the map. That is, the position an event starts from, not where it currently is - you'd want $gameMap.events()[i].x for that (note that indexes on that one start from 0).
Alternatively, it could be the result of whatever is happening inside shotEnemy or shotWall.

What it definitely isn't is the code continuing after a return statement, since that's not a thing that JavaScript does.
So, your code is detecting a wall hit when it should have registered an enemy being hit, is that correct? There are a bunch of ways this could happen, like making a mistake in setting up the wall region. Though my guess is that this event might be moving - and if it is, this code is not going to work, since $dataMap.events[i].x only checks the data in the map. That is, the position an event starts from, not where it currently is - you'd want $gameMap.events()[i].x for that (note that indexes on that one start from 0).
Alternatively, it could be the result of whatever is happening inside shotEnemy or shotWall.

What it definitely isn't is the code continuing after a return statement, since that's not a thing that JavaScript does.
It cotinues even when the return is a simple console.log, I'm remaking the system to use a better approach based on distance to minimize the loops
Apparently the OP has fixed the issue by rewriting their code.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/code-keeps-running-despite-return.161179/