the purpose of calling scan() in onScannedRobot()

51 views
Skip to first unread message

Kent Tong

unread,
Dec 2, 2023, 10:42:06 AM12/2/23
to Robocode
Hi,

I understand that calling scan() in onScannedRobot() will restart the event handler if a robot is indeed detected, but the event handler seems to be called in the next turn instead of immediately as stated by the api doc.

This issue aside, my primary question is with the purpose of calling scan() in onScannedRobot(). Is it to prevent the movement in run()? 

Many thanks in advance

fnl

unread,
Dec 2, 2023, 1:42:08 PM12/2/23
to Robocode
Hi Kent,

You are referring to the documentation for scan() here:
https://robocode.sourceforge.io/docs/robocode/robocode/Robot.html#scan--

A scan() contains an execute() method implicitly, which means that it executes its turn now by sending all its pending actions to the game, and "waiting" for the event handlers to be called.
Hence, the current onScannedRobot() handler is "interrupted" now due to scan() being called (executed), and the remaining code in the handler will not be called, at least not in the current turn.
And called scan() in the handler prevents commands following scan() from being called.

I see that the documentation is not very clear here, so I will need to update it.

For info, calling a scan() inside an onScannedRobot() handler is treated as a rescan. The code is here:

https://github.com/robo-code/robocode/blob/main/robocode.host/src/main/java/net/sf/robocode/host/proxies/BasicRobotProxy.java?plain=1#L321

public void rescan() {
    boolean reset = false;
    boolean origInterruptableValue = false;

    if (eventManager.getCurrentTopEventPriority() == eventManager.getScannedRobotEventPriority()) {
        reset = true;
        origInterruptableValue = eventManager.isInterruptible(eventManager.getScannedRobotEventPriority());
        eventManager.setInterruptible(eventManager.getScannedRobotEventPriority(), true);
    }

    commands.setScan(true);
    executeImpl();

    if (reset) {
        eventManager.setInterruptible(eventManager.getScannedRobotEventPriority(), origInterruptableValue);
    }
}


Reply all
Reply to author
Forward
0 new messages