battlecore::net

The net module reports whether the radio link is up and sends messages to the other taggers in the game. Include its declarations with include "./battlecore/net.bst";.

getIsConnected

Returns whether the radio link is up. In an ESP-NOW mode this is always true once the radio has started, because ESP-NOW needs no connection. In plain WiFi mode it is true while the device is connected to an access point. It does not say whether the device is in a game. Use battlecore::game.getIsConnected for that.

native bool battlecore::net.getIsConnected ();

send

Broadcasts one of the mesh commands defined in protocol_p2p.bst, such as PLAYER_COMMAND_PERMA_DEATH, to the other devices. commandNameis the command's name. The radio always broadcasts, so to address one player put a field such as byId in the payload and have the receiving script check it. Other devices raise it as module P2P, event commandName.

native void battlecore::net.send (string commandName, any payload, bool shouldRebroadcast = false, bool isDurable = false, bool shouldAck = false);

The firmware adds the sender's player id, a message id and the game id. The three flags are your decision. The firmware never works them out from the command:

  • shouldRebroadcast — other devices relay the message one hop, which roughly doubles its reach. It costs a radio frame for every device in range, so it is off unless you ask for it.
  • isDurable — receivers keep the message in their mesh log as well as delivering it. Only the mesh test screen reads that log today. The scoreboard comes from the player records, and nothing replays a missed message to a script.
  • shouldAck — reserved. The flag is sent, but nothing acts on it yet.

A device that missed a message when it was sent never sees it. Anything a player who was out of range, or rebooted, must still know belongs in the player record (see battlecore::player.setRecordValue), not in a message.

sendEvent

Sends the game's own message to every other tagger in the game. The name is anything from 1 to 16 characters. The data is a flat object of numbers, strings and bools. The firmware reads neither, so a new message needs no firmware or protocol change.

native bool battlecore::net.sendEvent (string name, object data, bool shouldRebroadcast = false, bool isDurable = false);

Every other tagger in the game raises it for its script as module NET, with the name as the event. The data's keys arrive on evt.data, beside senderPlayerId and gameId. The flags mean the same as for send. Returns false, and sends nothing, when the name is the wrong length or the message would not fit in one radio frame.

// The sender
battlecore::net.sendEvent("FLAG_CAPTURED", {"flag": 2, "team": 1}, true);

// Every other tagger. FLAG_CAPTURED is declared in the script's own EventEnum.
state _ {
	module ModuleEnum.NET {
		event EventEnum.FLAG_CAPTURED (EventData evt) => {
			if (evt.data.team == battlecore::player.getTeamId()) {
				battlecore::audio.trackStart("your-team-scored.wav");
			}
		}
	}
}

See Device Events for what arrives on the other taggers.