From f3bc6e66ca4c1d1a387333257e76211f34be123a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Tue, 16 Sep 2025 14:52:38 +0200 Subject: [PATCH 01/14] Factorize a bit --- extensions/reviewed/Gamepads.json | 1248 +++++++++++++---------------- 1 file changed, 539 insertions(+), 709 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 44738ff85..292d8402a 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -100,83 +100,182 @@ "name": "onFirstSceneLoaded", "sentence": "", "events": [ - { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [], - "actions": [] - }, { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Define an new private object javascript for the gamepad extension\r", - "gdjs._extensionController = {\r", - " players: {\r", - " 0: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 1: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 2: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 3: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", + "if (gdjs._extensionController) {\r", + " return;\r", + "}\r", + "\r", + "const players = {\r", + " 0: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", + " 1: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", + " 2: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", + " 3: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", + "};\r", + "let lastActiveController = -1;\r", + "/**\r", + " * Associate controller button ids to button names\r", + " */\r", + "const controllerButtonNames = {\r", + " \"XBOX\": {\r", + " 0: \"A\",\r", + " 1: \"B\",\r", + " 2: \"X\",\r", + " 3: \"Y\",\r", + " 4: \"LB\",\r", + " 5: \"RB\",\r", + " 6: \"LT\",\r", + " 7: \"RT\",\r", + " 8: \"BACK\",\r", + " 9: \"START\",\r", + " 10: \"CLICK_STICK_LEFT\",\r", + " 11: \"CLICK_STICK_RIGHT\",\r", + " 12: \"UP\",\r", + " 13: \"DOWN\",\r", + " 14: \"LEFT\",\r", + " 15: \"RIGHT\",\r", + " 16: \"NONE\",\r", + " 17: \"NONE\"\r", " },\r", - " lastActiveController: -1, // Last active controller\r", - " controllerButtonNames: { //Map associating controller button ids to button names\r", - " \"XBOX\": {\r", - " 0: \"A\",\r", - " 1: \"B\",\r", - " 2: \"X\",\r", - " 3: \"Y\",\r", - " 4: \"LB\",\r", - " 5: \"RB\",\r", - " 6: \"LT\",\r", - " 7: \"RT\",\r", - " 8: \"BACK\",\r", - " 9: \"START\",\r", - " 10: \"CLICK_STICK_LEFT\",\r", - " 11: \"CLICK_STICK_RIGHT\",\r", - " 12: \"UP\",\r", - " 13: \"DOWN\",\r", - " 14: \"LEFT\",\r", - " 15: \"RIGHT\",\r", - " 16: \"NONE\",\r", - " 17: \"NONE\"\r", - " },\r", - " \"PS4\": {\r", - " 0: \"CROSS\",\r", - " 1: \"CIRCLE\",\r", - " 2: \"SQUARE\",\r", - " 3: \"TRIANGLE\",\r", - " 4: \"L1\",\r", - " 5: \"R1\",\r", - " 6: \"L2\",\r", - " 7: \"R2\",\r", - " 8: \"SHARE\",\r", - " 9: \"OPTIONS\",\r", - " 10: \"CLICK_STICK_LEFT\",\r", - " 11: \"CLICK_STICK_RIGHT\",\r", - " 12: \"UP\",\r", - " 13: \"DOWN\",\r", - " 14: \"LEFT\",\r", - " 15: \"RIGHT\",\r", - " 16: \"PS_BUTTON\",\r", - " 17: \"CLICK_TOUCHPAD\"\r", - " }\r", + " \"PS4\": {\r", + " 0: \"CROSS\",\r", + " 1: \"CIRCLE\",\r", + " 2: \"SQUARE\",\r", + " 3: \"TRIANGLE\",\r", + " 4: \"L1\",\r", + " 5: \"R1\",\r", + " 6: \"L2\",\r", + " 7: \"R2\",\r", + " 8: \"SHARE\",\r", + " 9: \"OPTIONS\",\r", + " 10: \"CLICK_STICK_LEFT\",\r", + " 11: \"CLICK_STICK_RIGHT\",\r", + " 12: \"UP\",\r", + " 13: \"DOWN\",\r", + " 14: \"LEFT\",\r", + " 15: \"RIGHT\",\r", + " 16: \"PS_BUTTON\",\r", + " 17: \"CLICK_TOUCHPAD\"\r", " }\r", "};\r", "\r", - "gdjs._extensionController.getInputString = function (type, buttonId) {\r", - " const controllerButtonNames = gdjs._extensionController.controllerButtonNames;\r", - " if (controllerButtonNames[type] !== undefined) {\r", - " return controllerButtonNames[type][buttonId];\r", + "/**\r", + " * @param {string} type\r", + " * @param {number} buttonId\r", + " */\r", + "function getInputString(type, buttonId) {\r", + " if (!controllerButtonNames[type]) {\r", + " return \"UNKNOWN_BUTTON\";\r", + " }\r", + " return controllerButtonNames[type][buttonId];\r", + "}\r", + "\r", + "function getButtonId(buttonName) {\r", + " switch (buttonName) {\r", + " case 'A':\r", + " case 'CROSS':\r", + " return 0;\r", + " case 'B':\r", + " case 'CIRCLE':\r", + " return 1;\r", + " case 'X':\r", + " case 'SQUARE':\r", + " return 2;\r", + " case 'Y':\r", + " case 'TRIANGLE':\r", + " return 3;\r", + " case 'LB':\r", + " case 'L1':\r", + " return 4;\r", + " case 'RB':\r", + " case 'R1':\r", + " return 5;\r", + " case 'LT':\r", + " case 'L2':\r", + " return 6;\r", + " case 'RT':\r", + " case 'R2':\r", + " return 7;\r", + " case 'UP':\r", + " return 12;\r", + " case 'DOWN':\r", + " return 13;\r", + " case 'LEFT':\r", + " return 14;\r", + " case 'RIGHT':\r", + " return 15;\r", + " case 'BACK':\r", + " case 'SHARE':\r", + " return 8;\r", + " case 'START':\r", + " case 'OPTIONS':\r", + " return 9;\r", + " case 'CLICK_STICK_LEFT':\r", + " return 10;\r", + " case 'CLICK_STICK_RIGHT':\r", + " return 11;\r", + " //PS4\r", + " case 'PS_BUTTON':\r", + " return 16;\r", + " case 'CLICK_TOUCHPAD':\r", + " return 17;\r", + " default:\r", + " console.error('The gamepad button: ' + buttonName + ' is not valid.');\r", + " break;\r", " }\r", + "}\r", "\r", - " return \"UNKNOWN_BUTTON\";\r", + "/**\r", + " * @param {number} playerId\r", + " * @param {string} directionName\r", + " * @param {number} axisValueX\r", + " * @param {number} axisValueY\r", + " */\r", + "function isAxisPushed(playerId, directionName, axisValueX, axisValueY) {\r", + " switch (directionName) {\r", + " case 'LEFT':\r", + " return getNormalizedAxisValue(axisValueX, playerId) < 0;\r", + " case 'RIGHT':\r", + " return getNormalizedAxisValue(axisValueX, playerId) > 0;\r", + " case 'UP':\r", + " return getNormalizedAxisValue(axisValueY, playerId) < 0;\r", + " case 'DOWN':\r", + " return getNormalizedAxisValue(axisValueY, playerId) > 0;\r", + " case 'ANY':\r", + " return getNormalizedAxisValue(axisValueX, playerId) < 0\r", + " || getNormalizedAxisValue(axisValueX, playerId) > 0\r", + " || getNormalizedAxisValue(axisValueY, playerId) < 0\r", + " || getNormalizedAxisValue(axisValueY, playerId) > 0\r", + " default:\r", + " console.error('The value stick direction is not valid.');\r", + " return false;\r", + " }\r", "}\r", "\r", - "gdjs._extensionController.axisToAngle = function (deltaX, deltaY) {\r", + "/**\r", + " * @param {number} playerId\r", + " */\r", + "function getGamepad(playerId) {\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + " return gamepads[playerId];\r", + "}\r", + "\r", + "/**\r", + " * @param {number} deltaX\r", + " * @param {number} deltaY\r", + " */\r", + "function axisToAngle(deltaX, deltaY) {\r", " const rad = Math.atan2(deltaY, deltaX);\r", " const deg = rad * (180 / Math.PI);\r", " return deg;\r", "}\r", "\r", - "gdjs._extensionController.isXbox = function (gamepad) {\r", + "/**\r", + " * @param {{id: string}} gamepad\r", + " */\r", + "function isXbox(gamepad) {\r", " return (gamepad ? (\r", " gamepad.id.toUpperCase().indexOf(\"XBOX\") !== -1\r", " // \"XINPUT\" cannot be used to check if it is a xbox controller is just a generic\r", @@ -185,44 +284,94 @@ " ) : false);\r", "}\r", "\r", - "//Returns the new value taking into account the dead zone for the player_ID given\r", - "gdjs._extensionController.getNormalizedAxisValue = function (v, player_ID) {\r", + "/**\r", + " * Returns the new value taking into account the dead zone for the player_ID given\r", + " * @param {number} value\r", + " * @param {number} playerID\r", + " */\r", + "function getNormalizedAxisValue(value, playerID) {\r", " // gdjs._extensionController = gdjs._extensionController || { deadzone: 0.2 };\r", "\r", " // Anything smaller than this is assumed to be 0,0\r", - " const DEADZONE = gdjs._extensionController.players[player_ID].deadzone;\r", - "\r", - " if (Math.abs(v) < DEADZONE) {\r", - " // In the dead zone, set to 0\r", - " v = 0;\r", - "\r", - " if (v == null) {\r", - " return 0;\r", - " } else {\r", - " return v;\r", - " }\r", + " const deadzone = players[playerID].deadzone;\r", "\r", + " if (Math.abs(value) < deadzone) {\r", + " return 0;\r", " } else {\r", " // We're outside the dead zone, but we'd like to smooth\r", " // this value out so it still runs nicely between 0..1.\r", " // That is, we don't want it to jump suddenly from 0 to\r", - " // DEADZONE.\r", + " // deadzone.\r", "\r", - " // Remap v from\r", - " // DEADZONE..1 to 0..(1-DEADZONE)\r", + " // Remap value from\r", + " // deadzone..1 to 0..(1-deadzone)\r", " // or from\r", - " // -1..-DEADZONE to -(1-DEADZONE)..0\r", + " // -1..-deadzone to -(1-deadzone)..0\r", + " value = value - Math.sign(value) * deadzone;\r", "\r", - " v = v - Math.sign(v) * DEADZONE;\r", - "\r", - " // Remap v from\r", - " // 0..(1-DEADZONE) to 0..1\r", + " // Remap value from\r", + " // 0..(1-deadzone) to 0..1\r", " // or from\r", - " // -(1-DEADZONE)..0 to -1..0\r", + " // -(1-deadzone)..0 to -1..0\r", + " return value / (1 - deadzone);\r", + " }\r", + "}\r", + "\r", + "function update() {\r", + " //Each time a player press a button i save the last button pressed for the next frame\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + "\r", + " //Get function parameter\r", + " let countPlayers = Object.keys(players).length;\r", "\r", - " return v / (1 - DEADZONE);\r", + " //Repeat for each players\r", + " for (let i = 0; i < countPlayers; i++) {\r", + " let gamepad = gamepads[i]; // Get the gamepad of the player\r", + "\r", + " //We have to keep this condition because if the user hasn't plugged in his controller yet, we can't get the controller in the gamepad variable.\r", + " if (gamepad == null) {\r", + " continue;\r", + " }\r", + "\r", + " for (let b = 0; b < Object.keys(gamepad.buttons).length; b++) { //For each buttons\r", + " if (gamepad.buttons[b].pressed) { //One of them is pressed\r", + " players[i].lastButtonUsed = b; //Save the button pressed\r", + "\r", + " //Save the state of the button for the next frame.\r", + " players[i].previousFrameStateButtons[b] = { pressed: true };\r", + "\r", + " // Update Last Active Controller\r", + " lastActiveController = i;\r", + " } else {\r", + " players[i].previousFrameStateButtons[b] = { pressed: false };\r", + " }\r", + " }\r", + "\r", + " players[i].rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;\r", + " if (\r", + " players[i].rumble.duration - players[i].rumble.elapsedTime <= 0 &&\r", + " (players[i].rumble.weakMagnitude || players[i].rumble.strongMagnitude)\r", + " ) {\r", + " players[i].rumble.weakMagnitude = 0;\r", + " players[i].rumble.strongMagnitude = 0;\r", + " }\r", " }\r", - "};" + "}\r", + "\r", + "gdjs._extensionController = {\r", + " players,\r", + " lastActiveController,\r", + " controllerButtonNames,\r", + " getInputString,\r", + " getButtonId,\r", + " axisToAngle,\r", + " isXbox,\r", + " getNormalizedAxisValue,\r", + " isAxisPushed,\r", + " getGamepad,\r", + " update,\r", + "}" ], "parameterObjects": "", "useStrict": true, @@ -238,56 +387,11 @@ "name": "onScenePostEvents", "sentence": "", "events": [ - { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [], - "actions": [] - }, { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Each time a player press a button i save the last button pressed for the next frame", - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", - "//Get function parameter", - "let countPlayers = Object.keys(gdjs._extensionController.players).length;", - "", - "//Repeat for each players", - "for (let i = 0; i < countPlayers; i++) {", - " let gamepad = gamepads[i]; // Get the gamepad of the player", - "", - " //We have to keep this condition because if the user hasn't plugged in his controller yet, we can't get the controller in the gamepad variable.", - " if (gamepad == null) {", - " continue;", - " }", - "", - " for (let b = 0; b < Object.keys(gamepad.buttons).length; b++) { //For each buttons", - " if (gamepad.buttons[b].pressed) { //One of them is pressed", - " gdjs._extensionController.players[i].lastButtonUsed = b; //Save the button pressed", - "", - " //Save the state of the button for the next frame.", - " gdjs._extensionController.players[i].previousFrameStateButtons[b] = { pressed: true };", - "", - " // Update Last Active Controller", - " gdjs._extensionController.lastActiveController = i;", - " } else {", - " gdjs._extensionController.players[i].previousFrameStateButtons[b] = { pressed: false };", - " }", - " }", - "", "", - " gdjs._extensionController.players[i].rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;", - " if (", - " gdjs._extensionController.players[i].rumble.duration - gdjs._extensionController.players[i].rumble.elapsedTime <= 0 &&", - " (gdjs._extensionController.players[i].rumble.weakMagnitude || gdjs._extensionController.players[i].rumble.strongMagnitude)", - " ) {", - " gdjs._extensionController.players[i].rumble.weakMagnitude = 0;", - " gdjs._extensionController.players[i].rumble.strongMagnitude = 0;", - " }", - "", - "", - "}", + "gdjs._extensionController.update();", "" ], "parameterObjects": "", @@ -725,9 +829,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const trigger = eventsFunctionContext.getArgument(\"trigger\").toUpperCase();\r", @@ -740,11 +841,13 @@ " console.error('Parameter trigger is not valid in expression: \"Pressure on a gamepad trigger\"');\r", " return;\r", "}\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", "\r", "switch (trigger) {\r", " case 'LT':\r", @@ -795,43 +898,29 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "\r", - "\r", "if (playerId < 0 || playerId > 4) {\r", " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a stick force\"');\r", " return;\r", "}\r", - "\r", "if (stick !== \"LEFT\" && stick !== \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a stick force\"');\r", " return;\r", "}\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", + "const axisValueX = stick === 'RIGHT' ? gamepad.axes[2] : gamepad.axes[0];\r", + "const axisValueY = stick === 'RIGHT' ? gamepad.axes[3] : gamepad.axes[1];\r", "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "\r", - "switch (stick) {\r", - " case 'LEFT':\r", - " eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[0], playerId)) + Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[1], playerId)), 0, 1);\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[2], playerId)) + Math.abs(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[3], playerId)), 0, 1);\r", - " break;\r", - "\r", - " default:\r", - " eventsFunctionContext.returnValue = -1;\r", - " break;\r", - "}" + "eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(Math.abs(gdjs._extensionController.getNormalizedAxisValue(axisValueX, playerId)) + Math.abs(gdjs._extensionController.getNormalizedAxisValue(axisValueY, playerId)), 0, 1);\r", + "" ], "parameterObjects": "", "useStrict": true, @@ -907,14 +996,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "\r", - "\r", "if (playerId < 0 || playerId > 4) {\r", " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a stick rotation\"');\r", " return;\r", @@ -923,11 +1008,12 @@ " console.error('Parameter stick is not valid in expression: \"Value of a stick rotation\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "switch (stick) {\r", " case 'LEFT':\r", " eventsFunctionContext.returnValue = gdjs._extensionController.axisToAngle(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[0], playerId), gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[1], playerId));\r", @@ -976,9 +1062,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", @@ -996,11 +1079,12 @@ " console.error('Parameter direction is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "let parameterError = false;\r", "switch (stick) {\r", " case 'LEFT':\r", @@ -1125,9 +1209,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", @@ -1140,11 +1221,12 @@ " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "const axisIndex = stick === 'right' ? 2 : 0;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[axisIndex], playerId);\r", "" @@ -1182,9 +1264,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", @@ -1197,11 +1276,12 @@ " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "const axisIndex = stick === 'right' ? 3 : 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[axisIndex], playerId);\r", "" @@ -1239,9 +1319,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", @@ -1254,102 +1331,23 @@ " console.error('Parameter button is not valid in condition: \"Gamepad button released\"');\r", " return;\r", "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "let buttonId;\r", - "\r", - "switch (button) {\r", - " case 'A':\r", - " case 'CROSS':\r", - " buttonId = 0;\r", - " break;\r", - " case 'B':\r", - " case 'CIRCLE':\r", - " buttonId = 1;\r", - " break;\r", - " case 'X':\r", - " case 'SQUARE':\r", - " buttonId = 2;\r", - " break;\r", - " case 'Y':\r", - " case 'TRIANGLE':\r", - " buttonId = 3;\r", - " break;\r", - " case 'LB':\r", - " case 'L1':\r", - " buttonId = 4;\r", - " break;\r", - " case 'RB':\r", - " case 'R1':\r", - " buttonId = 5;\r", - " break;\r", - " case 'LT':\r", - " case 'L2':\r", - " buttonId = 6;\r", - " break;\r", - " case 'RT':\r", - " case 'R2':\r", - " buttonId = 7;\r", - " break;\r", - "\r", - " case 'UP':\r", - " buttonId = 12;\r", - " break;\r", - " case 'DOWN':\r", - " buttonId = 13;\r", - " break;\r", - " case 'LEFT':\r", - " buttonId = 14;\r", - " break;\r", - " case 'RIGHT':\r", - " buttonId = 15;\r", - " break;\r", - "\r", - " case 'BACK':\r", - " case 'SHARE':\r", - " buttonId = 8;\r", - " break;\r", - " case 'START':\r", - " case 'OPTIONS':\r", - " buttonId = 9;\r", - " break;\r", - "\r", - " case 'CLICK_STICK_LEFT':\r", - " buttonId = 10;\r", - " break;\r", - " case 'CLICK_STICK_RIGHT':\r", - " buttonId = 11;\r", - " break;\r", - "\r", - " //PS4\r", - " case 'PS_BUTTON':\r", - " buttonId = 16;\r", - " break;\r", - " case 'CLICK_TOUCHPAD':\r", - " buttonId = 17;\r", - " break;\r", - "\r", - " default:\r", - " console.error('The button: ' + button + ' in condition: \"Gamepad button released\" is not valid.');\r", - " break;\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", "}\r", - "\r", + "let buttonId = gdjs._extensionController.getButtonId(button);\r", "if (buttonId === undefined) {\r", " console.error('There is no buttons valid in condition: \"Gamepad button released\"');\r", " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "\r", "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", " console.error('Buttons on the gamepad are not accessible in condition: \"Gamepad button released\"');\r", " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "\r", "//Define default value on pressed button or use previous value\r", "gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] || { pressed: false };\r", "\r", @@ -1439,9 +1437,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameter\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", @@ -1449,11 +1444,12 @@ " console.error('Parameter gamepad identifier in condition: \"Any gamepad button pressed\", is not valid number, must be between 0 and 4.');\r", " return;\r", "}\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "let buttonId;\r", "for (let i = 0; i < gamepad.buttons.length; i++) { //For each buttons\r", " if (gamepad.buttons[i].pressed) { //One of them is pressed\r", @@ -1505,9 +1501,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();\r", @@ -1520,20 +1513,15 @@ " console.error('Parameter controller type is not valid in string expression: \"Last pressed button (LastButtonString)\"');\r", " return;\r", "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "if (gamepad !== null) { //Gamepad exist\r", - " //Get last btn id\r", - " const lastButtonUsedID = gdjs._extensionController.players[playerId].lastButtonUsed;\r", - "\r", - " //Return last button as string \r", - " eventsFunctionContext.returnValue = gdjs._extensionController.getInputString(controllerType, lastButtonUsedID);\r", - "\r", - "} else { //Gamepad dosen't exist\r", - " console.error('Your controller is not supported or the gamepad wasn\\'t detected in string expression: \"Last pressed button (LastButtonString)\"');\r", - " eventsFunctionContext.returnValue = \"Gamepad not connected\";\r", - "}" + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", + "const lastButtonUsedID = gdjs._extensionController.players[playerId].lastButtonUsed;\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.getInputString(controllerType, lastButtonUsedID);\r", + "" ], "parameterObjects": "", "useStrict": true, @@ -1604,9 +1592,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", @@ -1620,105 +1605,23 @@ " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", - "let buttonId;\r", - "\r", - "switch (button) {\r", - " case 'A':\r", - " case 'CROSS':\r", - " buttonId = 0;\r", - " break;\r", - " case 'B':\r", - " case 'CIRCLE':\r", - " buttonId = 1;\r", - " break;\r", - " case 'X':\r", - " case 'SQUARE':\r", - " buttonId = 2;\r", - " break;\r", - " case 'Y':\r", - " case 'TRIANGLE':\r", - " buttonId = 3;\r", - " break;\r", - " case 'LB':\r", - " case 'L1':\r", - " buttonId = 4;\r", - " break;\r", - " case 'RB':\r", - " case 'R1':\r", - " buttonId = 5;\r", - " break;\r", - " case 'LT':\r", - " case 'L2':\r", - " buttonId = 6;\r", - " break;\r", - " case 'RT':\r", - " case 'R2':\r", - " buttonId = 7;\r", - " break;\r", - "\r", - " case 'UP':\r", - " buttonId = 12;\r", - " break;\r", - " case 'DOWN':\r", - " buttonId = 13;\r", - " break;\r", - " case 'LEFT':\r", - " buttonId = 14;\r", - " break;\r", - " case 'RIGHT':\r", - " buttonId = 15;\r", - " break;\r", - "\r", - " case 'BACK':\r", - " case 'SHARE':\r", - " buttonId = 8;\r", - " break;\r", - " case 'START':\r", - " case 'OPTIONS':\r", - " buttonId = 9;\r", - " break;\r", - "\r", - " case 'CLICK_STICK_LEFT':\r", - " buttonId = 10;\r", - " break;\r", - " case 'CLICK_STICK_RIGHT':\r", - " buttonId = 11;\r", - " break;\r", - "\r", - " //PS4\r", - " case 'PS_BUTTON':\r", - " buttonId = 16;\r", - " break;\r", - " case 'CLICK_TOUCHPAD':\r", - " buttonId = 17;\r", - " break;\r", - "\r", - " default:\r", - " console.error('The button: ' + button + ' in condition: \"Gamepad button pressed\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", - " break;\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", "}\r", - "\r", - "\r", - "\r", + "let buttonId = gdjs._extensionController.getButtonId(button);\r", "if (buttonId === undefined) {\r", " console.error('There is no buttons valid in condition: \"Gamepad button pressed\"');\r", " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "\r", "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", " console.error('Buttons on the gamepad are not accessible in condition: \"Gamepad button pressed\"');\r", " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "\r", "//When a button is pressed, save the button in lastButtonUsed for each players\r", "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", @@ -1842,9 +1745,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", @@ -1859,124 +1759,25 @@ " return;\r", "}\r", "if (direction != \"UP\" && direction != \"DOWN\" && direction != \"LEFT\" && direction != \"RIGHT\" && direction != \"ANY\") {\r", - " console.error('Parameter deadzone in condition: \"Gamepad stick pushed (axis)\", is not valid, must be UP, DOWN, LEFT or RIGHT');\r", + " console.error('Parameter direction in condition: \"Gamepad stick pushed (axis)\", is not valid, must be UP, DOWN, LEFT or RIGHT');\r", " return;\r", "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) {\r", - " eventsFunctionContext.returnValue = false;\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", " return;\r", "}\r", - "\r", - "\r", - "//Define in onFirstSceneLoaded function\r", - "const getNormalizedAxisValue = gdjs._extensionController.getNormalizedAxisValue;\r", - "\r", "switch (stick) {\r", " case 'LEFT':\r", - " switch (direction) {\r", - " case 'LEFT':\r", - " if (getNormalizedAxisValue(gamepad.axes[0], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " if (getNormalizedAxisValue(gamepad.axes[0], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'UP':\r", - " if (getNormalizedAxisValue(gamepad.axes[1], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'DOWN':\r", - " if (getNormalizedAxisValue(gamepad.axes[1], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'ANY':\r", - " if ( getNormalizedAxisValue(gamepad.axes[0], playerId) < 0\r", - " || getNormalizedAxisValue(gamepad.axes[0], playerId) > 0\r", - " || getNormalizedAxisValue(gamepad.axes[1], playerId) < 0 \r", - " || getNormalizedAxisValue(gamepad.axes[1], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " default:\r", - " console.error('The value Direction on stick Left on the condition: \"Gamepad stick pushed (axis)\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", - " break;\r", - " }\r", + " eventsFunctionContext.returnValue = gdjs._extensionController.isAxisPushed(playerId, direction, gamepad.axes[0], gamepad.axes[1]);\r", " break;\r", - "\r", " case 'RIGHT':\r", - " switch (direction) {\r", - " case 'LEFT':\r", - " if (getNormalizedAxisValue(gamepad.axes[2], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " if (getNormalizedAxisValue(gamepad.axes[2], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'UP':\r", - " if (getNormalizedAxisValue(gamepad.axes[3], playerId) < 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'DOWN':\r", - " if (getNormalizedAxisValue(gamepad.axes[3], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " case 'ANY':\r", - " if ( getNormalizedAxisValue(gamepad.axes[2], playerId) < 0\r", - " || getNormalizedAxisValue(gamepad.axes[2], playerId) > 0\r", - " || getNormalizedAxisValue(gamepad.axes[3], playerId) < 0 \r", - " || getNormalizedAxisValue(gamepad.axes[3], playerId) > 0) {\r", - " eventsFunctionContext.returnValue = true;\r", - " return;\r", - " }\r", - " break;\r", - "\r", - " default:\r", - " console.error('The value Direction on stick Right on the condition: \"Gamepad stick pushed (axis)\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", - " break;\r", - " }\r", + " eventsFunctionContext.returnValue = gdjs._extensionController.isAxisPushed(playerId, direction, gamepad.axes[2], gamepad.axes[3]);\r", " break;\r", - "\r", " default:\r", - " console.error('The value Stick on the condition: \"Gamepad stick pushed (axis)\" is not valid.');\r", - " eventsFunctionContext.returnValue = false;\r", " break;\r", "}\r", - "\r", - "eventsFunctionContext.returnValue = false;\r", "" ], "parameterObjects": "", @@ -2048,9 +1849,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", "//Get function parameter", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "", @@ -2058,12 +1856,12 @@ " console.error('Parameter gamepad identifier in string expression: \"Gamepad type\", is not valid number, must be between 0 and 4');", " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", + "}", "eventsFunctionContext.returnValue = (gamepad && gamepad.id) ? gamepad.id : \"No information for player \" + (playerId + 1)", "" ], @@ -2094,9 +1892,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", "//Get function parameters", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();", @@ -2109,13 +1904,12 @@ " console.error('Parameter type in condition: \"Gamepad type\", is not a string.');", " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", - "", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", + "}", "if (controllerType == \"XBOX\") {", " eventsFunctionContext.returnValue = gdjs._extensionController.isXbox(gamepad);", "} else {", @@ -2190,10 +1984,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "//Vibration work only on game in browser.", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", "//Get function parameters", "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", @@ -2202,13 +1992,14 @@ " console.error('Parameter gamepad identifier in action: \"Gamepad connected\", is not valid number, must be between 0 and 4.');", " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", - "if (gamepad && gamepad.vibrationActuator) {", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", + "}", + "//Vibration work only on game in browser.", + "if (gamepad.vibrationActuator) {", " gamepad.vibrationActuator.playEffect(\"dual-rumble\", {", " startDelay: 0,", " duration: duration * 1000,", @@ -2246,10 +2037,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "//Vibration work only on game in browser.", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", "//Get function parameters", "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", @@ -2268,13 +2055,14 @@ " console.error('Parameter strongRumble identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 1.');", " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug the controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", - "if (gamepad && gamepad.vibrationActuator) {", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", + "}", + "//Vibration work only on game in browser.", + "if (gamepad.vibrationActuator) {", " gamepad.vibrationActuator.playEffect(\"dual-rumble\", {", " startDelay: 0,", " duration: duration * 1000,", @@ -2327,10 +2115,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "//Vibration work only on game in browser.", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", - "", "//Get function parameters", "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", "const elapsedTime = gdjs._extensionController.players[playerId].rumble.elapsedTime || 0;", @@ -2338,7 +2122,6 @@ "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", "", - "", "if (playerId < 0 || playerId > 4) {", " console.error('Parameter gamepad identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 4.');", " return;", @@ -2351,15 +2134,15 @@ " console.error('Parameter strongRumble identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 1.');", " return;", "}", - "", - "const gamepad = gamepads[playerId];", - "", - "//we need keep this condition because when use have not yet plug the controller we can't get the controller in the gamepad variable.", - "if (gamepad == null) return;", - "", + "/** @type {Gamepad} */", + "const gamepad = gdjs._extensionController.getGamepad(playerId);", + "if (!gamepad) {", + " // The gamepad is not connected.", + " return;", + "}", "if (originalDuration - elapsedTime <= 0) return;", - "", - "if (gamepad && gamepad.vibrationActuator) {", + "//Vibration work only on game in browser.", + "if (gamepad.vibrationActuator) {", " gamepad.vibrationActuator.playEffect(\"dual-rumble\", {", " startDelay: 0,", " duration: 1000 * (originalDuration - elapsedTime),", @@ -2410,29 +2193,24 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "\r", "if (playerId < 0 || playerId > 4) {\r", "\tconsole.error('Parameter gamepad identifier in condition: \"Any gamepad button released\", is not valid number, must be between 0 and 4.');\r", "\treturn;\r", "}\r", - "\r", - "const gamepad = gamepads[playerId];\r", - "\r", - "//we need keep this condition because when use have not yet plug her controller we can't get the controller in the gamepad variable.\r", - "if (gamepad == null) return;\r", - "\r", + "/** @type {Gamepad} */\r", + "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", + "if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + "}\r", "for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) { //For each buttons on current frame.\r", "\r", "\tif (buttonId === undefined) {\r", "\t\teventsFunctionContext.returnValue = false;\r", "\t\treturn;\r", "\t}\r", - "\r", "\t//Get previous value or define value by default for the current button\r", "\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] || { pressed: false };\r", "\r", @@ -2454,7 +2232,6 @@ "\t\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = false;\r", "\t\teventsFunctionContext.returnValue = false;\r", "\t}\r", - "\r", "\tif (currentFrameStateButtonIsPressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", "}\r", "" @@ -3028,64 +2805,77 @@ "value": "", "type": "Behavior", "label": "Platformer character behavior", - "description": "", - "group": "", "extraInformation": [ "PlatformBehavior::PlatformerObjectBehavior" ], + "choices": [], "name": "PlatformerCharacter" }, { "value": "1", "type": "Number", "label": "Gamepad identifier (1, 2, 3 or 4)", - "description": "", - "group": "", - "extraInformation": [], "name": "GamepadIdentifier" }, { "value": "true", "type": "Boolean", "label": "Use directional pad", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseArrows" }, { "value": "true", "type": "Boolean", "label": "Use left stick", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseLeftStick" }, { "value": "", "type": "Boolean", "label": "Use right stick", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseRightStick" }, { "value": "A or Cross", "type": "Choice", "label": "Jump button", - "description": "", "group": "Controls", - "extraInformation": [ - "A or Cross", - "B or Circle", - "X or Square", - "Y or Triangle", - "LB or L1", - "RB or R1", - "LT or L2", - "RT or R2" + "choices": [ + { + "label": "A or Cross", + "value": "A or Cross" + }, + { + "label": "B or Circle", + "value": "B or Circle" + }, + { + "label": "X or Square", + "value": "X or Square" + }, + { + "label": "Y or Triangle", + "value": "Y or Triangle" + }, + { + "label": "LB or L1", + "value": "LB or L1" + }, + { + "label": "RB or R1", + "value": "RB or R1" + }, + { + "label": "LT or L2", + "value": "LT or L2" + }, + { + "label": "RT or R2", + "value": "RT or R2" + } ], "name": "JumpButton" } @@ -3214,31 +3004,32 @@ "value": "", "type": "Behavior", "label": "3D physics character", - "description": "", - "group": "", "extraInformation": [ "Physics3D::PhysicsCharacter3D" ], + "choices": [], "name": "PhysicsCharacter3D" }, { "value": "1", "type": "Number", "label": "Gamepad identifier (1, 2, 3 or 4)", - "description": "", - "group": "", - "extraInformation": [], "name": "GamepadIdentifier" }, { "value": "Left", "type": "Choice", "label": "Walk joystick", - "description": "", "group": "Controls", - "extraInformation": [ - "Left", - "Right" + "choices": [ + { + "label": "Left", + "value": "Left" + }, + { + "label": "Right", + "value": "Right" + } ], "name": "JoystickIdentifier" }, @@ -3246,17 +3037,40 @@ "value": "A or Cross", "type": "Choice", "label": "Jump button", - "description": "", "group": "Controls", - "extraInformation": [ - "A or Cross", - "B or Circle", - "X or Square", - "Y or Triangle", - "LB or L1", - "RB or R1", - "LT or L2", - "RT or R2" + "choices": [ + { + "label": "A or Cross", + "value": "A or Cross" + }, + { + "label": "B or Circle", + "value": "B or Circle" + }, + { + "label": "X or Square", + "value": "X or Square" + }, + { + "label": "Y or Triangle", + "value": "Y or Triangle" + }, + { + "label": "LB or L1", + "value": "LB or L1" + }, + { + "label": "RB or R1", + "value": "RB or R1" + }, + { + "label": "LT or L2", + "value": "LT or L2" + }, + { + "label": "RT or R2", + "value": "RT or R2" + } ], "name": "JumpButton" } @@ -3374,31 +3188,32 @@ "value": "", "type": "Behavior", "label": "3D physics character", - "description": "", - "group": "", "extraInformation": [ "Physics3D::PhysicsCharacter3D" ], + "choices": [], "name": "PhysicsCharacter3D" }, { "value": "1", "type": "Number", "label": "Gamepad identifier (1, 2, 3 or 4)", - "description": "", - "group": "", - "extraInformation": [], "name": "GamepadIdentifier" }, { "value": "Left", "type": "Choice", "label": "Walk joystick", - "description": "", "group": "Controls", - "extraInformation": [ - "Left", - "Right" + "choices": [ + { + "label": "Left", + "value": "Left" + }, + { + "label": "Right", + "value": "Right" + } ], "name": "WalkStick" }, @@ -3406,11 +3221,16 @@ "value": "Right", "type": "Choice", "label": "Camera joystick", - "description": "", "group": "Controls", - "extraInformation": [ - "Left", - "Right" + "choices": [ + { + "label": "Left", + "value": "Left" + }, + { + "label": "Right", + "value": "Right" + } ], "name": "CameraStick" }, @@ -3418,17 +3238,40 @@ "value": "A or Cross", "type": "Choice", "label": "Jump button", - "description": "", "group": "Controls", - "extraInformation": [ - "A or Cross", - "B or Circle", - "X or Square", - "Y or Triangle", - "LB or L1", - "RB or R1", - "LT or L2", - "RT or R2" + "choices": [ + { + "label": "A or Cross", + "value": "A or Cross" + }, + { + "label": "B or Circle", + "value": "B or Circle" + }, + { + "label": "X or Square", + "value": "X or Square" + }, + { + "label": "Y or Triangle", + "value": "Y or Triangle" + }, + { + "label": "LB or L1", + "value": "LB or L1" + }, + { + "label": "RB or R1", + "value": "RB or R1" + }, + { + "label": "LT or L2", + "value": "LT or L2" + }, + { + "label": "RT or R2", + "value": "RT or R2" + } ], "name": "JumpButton" } @@ -4361,31 +4204,31 @@ "value": "", "type": "Behavior", "label": "3D capability", - "description": "", - "group": "", "extraInformation": [ "Scene3D::Base3DBehavior" ], + "choices": [], "name": "Object3D" }, { "value": "1", "type": "Number", "label": "Gamepad identifier (1, 2, 3 or 4)", - "description": "", - "group": "", - "extraInformation": [], "name": "GamepadIdentifier" }, { "value": "Right", "type": "Choice", "label": "Camera joystick", - "description": "", - "group": "", - "extraInformation": [ - "Left", - "Right" + "choices": [ + { + "label": "Left", + "value": "Left" + }, + { + "label": "Right", + "value": "Right" + } ], "name": "CameraStick" }, @@ -4394,27 +4237,21 @@ "type": "Number", "unit": "AngularSpeed", "label": "Maximum rotation speed", - "description": "", "group": "Horizontal rotation", - "extraInformation": [], "name": "HorizontalRotationSpeedMax" }, { "value": "360", "type": "Number", "label": "Rotation acceleration", - "description": "", "group": "Horizontal rotation", - "extraInformation": [], "name": "HorizontalRotationAcceleration" }, { "value": "720", "type": "Number", "label": "Rotation deceleration", - "description": "", "group": "Horizontal rotation", - "extraInformation": [], "name": "HorizontalRotationDeceleration" }, { @@ -4422,27 +4259,21 @@ "type": "Number", "unit": "AngularSpeed", "label": "Maximum rotation speed", - "description": "", "group": "Vertical rotation", - "extraInformation": [], "name": "VerticalRotationSpeedMax" }, { "value": "240", "type": "Number", "label": "Rotation acceleration", - "description": "", "group": "Vertical rotation", - "extraInformation": [], "name": "VerticalRotationAcceleration" }, { "value": "480", "type": "Number", "label": "Rotation deceleration", - "description": "", "group": "Vertical rotation", - "extraInformation": [], "name": "VerticalRotationDeceleration" }, { @@ -4450,9 +4281,7 @@ "type": "Number", "unit": "DegreeAngle", "label": "Minimum angle", - "description": "", "group": "Vertical rotation", - "extraInformation": [], "name": "VerticalAngleMin" }, { @@ -4460,9 +4289,7 @@ "type": "Number", "unit": "DegreeAngle", "label": "Maximum angle", - "description": "", "group": "Vertical rotation", - "extraInformation": [], "name": "VerticalAngleMax" }, { @@ -4470,9 +4297,7 @@ "type": "Number", "unit": "Pixel", "label": "Z position offset", - "description": "", "group": "Position", - "extraInformation": [], "name": "OffsetZ" }, { @@ -4480,9 +4305,6 @@ "type": "Number", "unit": "AngularSpeed", "label": "Current rotation speed Z", - "description": "", - "group": "", - "extraInformation": [], "hidden": true, "name": "CurrentRotationSpeedZ" }, @@ -4491,9 +4313,6 @@ "type": "Number", "unit": "AngularSpeed", "label": "Current rotation speed Y", - "description": "", - "group": "", - "extraInformation": [], "hidden": true, "name": "CurrentRotationSpeedY" } @@ -4793,64 +4612,77 @@ "value": "", "type": "Behavior", "label": "3D physics car", - "description": "", - "group": "", "extraInformation": [ "Physics3D::PhysicsCar3D" ], + "choices": [], "name": "PhysicsCar3D" }, { "value": "1", "type": "Number", "label": "Gamepad identifier (1, 2, 3 or 4)", - "description": "", - "group": "", - "extraInformation": [], "name": "GamepadIdentifier" }, { "value": "true", "type": "Boolean", "label": "Use directional pad", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseArrows" }, { "value": "true", "type": "Boolean", "label": "Use left stick", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseLeftStick" }, { "value": "", "type": "Boolean", "label": "Use right stick", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseRightStick" }, { "value": "B or Circle", "type": "Choice", "label": "Hand brake button", - "description": "", "group": "Controls", - "extraInformation": [ - "A or Cross", - "B or Circle", - "X or Square", - "Y or Triangle", - "LB or L1", - "RB or R1", - "LT or L2", - "RT or R2" + "choices": [ + { + "label": "A or Cross", + "value": "A or Cross" + }, + { + "label": "B or Circle", + "value": "B or Circle" + }, + { + "label": "X or Square", + "value": "X or Square" + }, + { + "label": "Y or Triangle", + "value": "Y or Triangle" + }, + { + "label": "LB or L1", + "value": "LB or L1" + }, + { + "label": "RB or R1", + "value": "RB or R1" + }, + { + "label": "LT or L2", + "value": "LT or L2" + }, + { + "label": "RT or R2", + "value": "RT or R2" + } ], "name": "HandBrakeButton" } @@ -5444,59 +5276,57 @@ "value": "", "type": "Behavior", "label": "Top-down movement behavior", - "description": "", - "group": "", "extraInformation": [ "TopDownMovementBehavior::TopDownMovementBehavior" ], + "choices": [], "name": "TopDownMovement" }, { "value": "1", "type": "Number", "label": "Gamepad identifier (1, 2, 3 or 4)", - "description": "", - "group": "", - "extraInformation": [], "name": "GamepadIdentifier" }, { "value": "true", "type": "Boolean", "label": "Use directional pad", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseArrows" }, { "value": "true", "type": "Boolean", "label": "Use left stick", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseLeftStick" }, { "value": "", "type": "Boolean", "label": "Use right stick", - "description": "", "group": "Controls", - "extraInformation": [], "name": "UseRightStick" }, { "value": "Analog", "type": "Choice", "label": "Stick mode", - "description": "", "group": "Controls", - "extraInformation": [ - "Analog", - "360°", - "8 Directions" + "choices": [ + { + "label": "Analog", + "value": "Analog" + }, + { + "label": "360°", + "value": "360°" + }, + { + "label": "8 Directions", + "value": "8 Directions" + } ], "name": "StickMode" } @@ -5505,4 +5335,4 @@ } ], "eventsBasedObjects": [] -} +} \ No newline at end of file From 52c5374d5e263aeb0a2ed0d6db6e86a4a47e638d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Tue, 16 Sep 2025 16:23:23 +0200 Subject: [PATCH 02/14] Encapsulate players access --- extensions/reviewed/Gamepads.json | 117 +++++++++++++++++------------- 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 292d8402a..56adde6f9 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -107,12 +107,29 @@ " return;\r", "}\r", "\r", - "const players = {\r", - " 0: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 1: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 2: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - " 3: { mapping: 'DEFAULT', lastButtonUsed: -1, deadzone: 0.2, previousFrameStateButtons: {}, rumble: {} },\r", - "};\r", + "/** @type {{[number]: Player}} */\r", + "const players = {};\r", + "\r", + "class Player {\r", + " mapping = 'DEFAULT';\r", + " lastButtonUsed = -1;\r", + " deadzone = 0.2;\r", + " previousFrameStateButtons = {};\r", + " rumble = {};\r", + "}\r", + "\r", + "/**\r", + " * @param {number} playerId\r", + " */\r", + "function getPlayer(playerId) {\r", + " let player = players[playerId];\r", + " if (!player) {\r", + " player = new Player();\r", + " players[playerId] = player;\r", + " }\r", + " return player;\r", + "}\r", + "\r", "let lastActiveController = -1;\r", "/**\r", " * Associate controller button ids to button names\r", @@ -293,7 +310,7 @@ " // gdjs._extensionController = gdjs._extensionController || { deadzone: 0.2 };\r", "\r", " // Anything smaller than this is assumed to be 0,0\r", - " const deadzone = players[playerID].deadzone;\r", + " const deadzone = getPlayer(playerID).deadzone;\r", "\r", " if (Math.abs(value) < deadzone) {\r", " return 0;\r", @@ -321,46 +338,41 @@ " //Each time a player press a button i save the last button pressed for the next frame\r", " /** @type {Gamepad[]} */\r", " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - "\r", - " //Get function parameter\r", - " let countPlayers = Object.keys(players).length;\r", - "\r", - " //Repeat for each players\r", - " for (let i = 0; i < countPlayers; i++) {\r", + " for (let i = 0; i < gamepads.length; i++) {\r", " let gamepad = gamepads[i]; // Get the gamepad of the player\r", - "\r", - " //We have to keep this condition because if the user hasn't plugged in his controller yet, we can't get the controller in the gamepad variable.\r", " if (gamepad == null) {\r", + " // The gamepad is not connected.\r", " continue;\r", " }\r", + " const player = getPlayer(i);\r", "\r", " for (let b = 0; b < Object.keys(gamepad.buttons).length; b++) { //For each buttons\r", " if (gamepad.buttons[b].pressed) { //One of them is pressed\r", - " players[i].lastButtonUsed = b; //Save the button pressed\r", + " player.lastButtonUsed = b; //Save the button pressed\r", "\r", " //Save the state of the button for the next frame.\r", - " players[i].previousFrameStateButtons[b] = { pressed: true };\r", + " player.previousFrameStateButtons[b] = { pressed: true };\r", "\r", " // Update Last Active Controller\r", " lastActiveController = i;\r", " } else {\r", - " players[i].previousFrameStateButtons[b] = { pressed: false };\r", + " player.previousFrameStateButtons[b] = { pressed: false };\r", " }\r", " }\r", "\r", - " players[i].rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;\r", + " player.rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;\r", " if (\r", - " players[i].rumble.duration - players[i].rumble.elapsedTime <= 0 &&\r", - " (players[i].rumble.weakMagnitude || players[i].rumble.strongMagnitude)\r", + " player.rumble.duration - player.rumble.elapsedTime <= 0 &&\r", + " (player.rumble.weakMagnitude || player.rumble.strongMagnitude)\r", " ) {\r", - " players[i].rumble.weakMagnitude = 0;\r", - " players[i].rumble.strongMagnitude = 0;\r", + " player.rumble.weakMagnitude = 0;\r", + " player.rumble.strongMagnitude = 0;\r", " }\r", " }\r", "}\r", "\r", "gdjs._extensionController = {\r", - " players,\r", + " getPlayer,\r", " lastActiveController,\r", " controllerButtonNames,\r", " getInputString,\r", @@ -1349,21 +1361,22 @@ " return;\r", "}\r", "//Define default value on pressed button or use previous value\r", - "gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] || { pressed: false };\r", + "const player = gdjs._extensionController.getPlayer(playerId)", + "player.previousFrameStateButtons[buttonId] = player.previousFrameStateButtons[buttonId] || { pressed: false };\r", "\r", "//Get state of button at previous frame\r", - "const previousStateButton = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed;\r", + "const previousStateButton = player.previousFrameStateButtons[buttonId].pressed;\r", "\r", "//When previousStateButton is true and actual button state is not pressed\r", "//Player have release the button\r", "if (previousStateButton === true && gamepad.buttons[buttonId].pressed === false) {\r", " // Save the last button used for the player \r", - " gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", - " gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = true;\r", + " player.lastButtonUsed = buttonId;\r", + " player.previousFrameStateButtons[buttonId].pressed = true;\r", " eventsFunctionContext.returnValue = true;\r", "\r", "} else {\r", - " gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = false;\r", + " player.previousFrameStateButtons[buttonId].pressed = false;\r", " eventsFunctionContext.returnValue = false;\r", "}\r", "" @@ -1408,7 +1421,7 @@ "}\r", "\r", "//Return the last button used by the player\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].lastButtonUsed;" + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).lastButtonUsed;" ], "parameterObjects": "", "useStrict": true, @@ -1471,7 +1484,7 @@ "}\r", "\r", "//When a button is pressed, save the button in lastButtonUsed for each players\r", - "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", + "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.getPlayer(playerId).lastButtonUsed = buttonId;\r", "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", "\r", "\r", @@ -1519,7 +1532,7 @@ " // The gamepad is not connected.\r", " return;\r", "}\r", - "const lastButtonUsedID = gdjs._extensionController.players[playerId].lastButtonUsed;\r", + "const lastButtonUsedID = gdjs._extensionController.getPlayer(playerId).lastButtonUsed;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getInputString(controllerType, lastButtonUsedID);\r", "" ], @@ -1623,7 +1636,7 @@ " return;\r", "}\r", "//When a button is pressed, save the button in lastButtonUsed for each players\r", - "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", + "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.getPlayer(playerId).lastButtonUsed = buttonId;\r", "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", "\r", "\r", @@ -1673,7 +1686,7 @@ " return;\r", "}\r", "///Return the deadzone value for a given player\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].deadzone;" + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).deadzone;" ], "parameterObjects": "", "useStrict": true, @@ -1713,7 +1726,7 @@ "\r", "// clamp the newDeadzone in range [0, 1].\r", "// https://github.com/4ian/GDevelop-extensions/pull/33#issuecomment-618224857\r", - "gdjs._extensionController.players[playerId].deadzone = gdjs.evtTools.common.clamp(newDeadzone, 0, 1);\r", + "gdjs._extensionController.getPlayer(playerId).deadzone = gdjs.evtTools.common.clamp(newDeadzone, 0, 1);\r", "" ], "parameterObjects": "", @@ -2070,11 +2083,11 @@ " strongMagnitude: strongRumbleMagnitude", " });", "}", - "", - "gdjs._extensionController.players[playerId].rumble.duration = duration;", - "gdjs._extensionController.players[playerId].rumble.elapsedTime = 0;", - "gdjs._extensionController.players[playerId].rumble.weakMagnitude = weakRumbleMagnitude;", - "gdjs._extensionController.players[playerId].rumble.strongMagnitude = strongRumbleMagnitude;" + "const player = gdjs._extensionController.getPlayer(playerId)", + "player.rumble.duration = duration;", + "player.rumble.elapsedTime = 0;", + "player.rumble.weakMagnitude = weakRumbleMagnitude;", + "player.rumble.strongMagnitude = strongRumbleMagnitude;" ], "parameterObjects": "", "useStrict": true, @@ -2117,8 +2130,9 @@ "inlineCode": [ "//Get function parameters", "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", - "const elapsedTime = gdjs._extensionController.players[playerId].rumble.elapsedTime || 0;", - "const originalDuration = gdjs._extensionController.players[playerId].rumble.duration || 1;", + "const player = gdjs._extensionController.getPlayer(playerId);", + "const elapsedTime = player.rumble.elapsedTime || 0;", + "const originalDuration = player.rumble.duration || 1;", "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", "", @@ -2151,8 +2165,8 @@ " });", "}", "", - "gdjs._extensionController.players[playerId].rumble.weakMagnitude = weakRumbleMagnitude;", - "gdjs._extensionController.players[playerId].rumble.strongMagnitude = strongRumbleMagnitude;" + "player.rumble.weakMagnitude = weakRumbleMagnitude;", + "player.rumble.strongMagnitude = strongRumbleMagnitude;" ], "parameterObjects": "", "useStrict": true, @@ -2212,10 +2226,11 @@ "\t\treturn;\r", "\t}\r", "\t//Get previous value or define value by default for the current button\r", - "\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId] || { pressed: false };\r", + "\tconst player = gdjs._extensionController.getPlayer(playerId)\r", + "\tplayer.previousFrameStateButtons[buttonId] = player.previousFrameStateButtons[buttonId] || { pressed: false };\r", "\r", "\t//Get state of the button at previous frame\r", - "\tconst previousStateButtonIsPressed = gdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed;\r", + "\tconst previousStateButtonIsPressed = player.previousFrameStateButtons[buttonId].pressed;\r", "\r", "\t//Get the state of the button on the current frame.\r", "\tconst currentFrameStateButtonIsPressed = gamepad.buttons[buttonId].pressed;\r", @@ -2223,16 +2238,16 @@ "\t//When previousStateButtonIsPressed is true and actual button state is not pressed\r", "\t//Player have release the button\r", "\tif (previousStateButtonIsPressed === true && currentFrameStateButtonIsPressed === false) {\r", - "\t\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = true;\r", + "\t\tplayer.previousFrameStateButtons[buttonId].pressed = true;\r", "\t\teventsFunctionContext.returnValue = true;\r", "\t\t//break;\r", "\t\treturn;\r", "\t} else {\r", "\t\t//The player didn't released the button yet, the previous frame state is still true\r", - "\t\tgdjs._extensionController.players[playerId].previousFrameStateButtons[buttonId].pressed = false;\r", + "\t\tplayer.previousFrameStateButtons[buttonId].pressed = false;\r", "\t\teventsFunctionContext.returnValue = false;\r", "\t}\r", - "\tif (currentFrameStateButtonIsPressed) gdjs._extensionController.players[playerId].lastButtonUsed = buttonId;\r", + "\tif (currentFrameStateButtonIsPressed) player.lastButtonUsed = buttonId;\r", "}\r", "" ], @@ -2261,7 +2276,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].rumble.weakMagnitude;" + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).rumble.weakMagnitude;" ], "parameterObjects": "", "useStrict": true, @@ -2291,7 +2306,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;\r", - "eventsFunctionContext.returnValue = gdjs._extensionController.players[playerId].rumble.strongMagnitude;" + "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).rumble.strongMagnitude;" ], "parameterObjects": "", "useStrict": true, From 98b88d55838dabcb8de9ec6742088ce0dd16e421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Tue, 16 Sep 2025 16:32:02 +0200 Subject: [PATCH 03/14] Remove the 4 players limit --- extensions/reviewed/Gamepads.json | 105 ++---------------------------- 1 file changed, 4 insertions(+), 101 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 56adde6f9..59764dc8e 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -402,7 +402,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "", "gdjs._extensionController.update();", "" ], @@ -845,10 +844,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const trigger = eventsFunctionContext.getArgument(\"trigger\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in expression: \"Pressure on a gamepad trigger\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "if (trigger != \"LT\" && trigger != \"RT\" && trigger != \"L2\" && trigger != \"R2\") {\r", " console.error('Parameter trigger is not valid in expression: \"Pressure on a gamepad trigger\"');\r", " return;\r", @@ -860,7 +855,6 @@ " return;\r", "}\r", "\r", - "\r", "switch (trigger) {\r", " case 'LT':\r", " case 'L2':\r", @@ -914,10 +908,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a stick force\"');\r", - " return;\r", - "}\r", "if (stick !== \"LEFT\" && stick !== \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a stick force\"');\r", " return;\r", @@ -1012,10 +1002,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a stick rotation\"');\r", - " return;\r", - "}\r", "if (stick !== \"LEFT\" && stick !== \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a stick rotation\"');\r", " return;\r", @@ -1079,10 +1065,6 @@ "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a gamepad axis\"');\r", - " return;\r", - "}\r", "if (stick != \"LEFT\" && stick != \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", @@ -1225,10 +1207,6 @@ "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a gamepad axis\"');\r", - " return;\r", - "}\r", "if (stick != \"left\" && stick != \"right\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", @@ -1280,10 +1258,6 @@ "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier is not valid in expression: \"Value of a gamepad axis\"');\r", - " return;\r", - "}\r", "if (stick != \"left\" && stick != \"right\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a gamepad axis\"');\r", " return;\r", @@ -1335,10 +1309,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Gamepad button released\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "if (button === \"\") {\r", " console.error('Parameter button is not valid in condition: \"Gamepad button released\"');\r", " return;\r", @@ -1361,7 +1331,7 @@ " return;\r", "}\r", "//Define default value on pressed button or use previous value\r", - "const player = gdjs._extensionController.getPlayer(playerId)", + "const player = gdjs._extensionController.getPlayer(playerId)\r", "player.previousFrameStateButtons[buttonId] = player.previousFrameStateButtons[buttonId] || { pressed: false };\r", "\r", "//Get state of button at previous frame\r", @@ -1414,12 +1384,6 @@ "//Get function parameter\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", - "//Player id is not valid\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in expression: \"Last pressed button (id)\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", - "\r", "//Return the last button used by the player\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).lastButtonUsed;" ], @@ -1453,10 +1417,6 @@ "//Get function parameter\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Any gamepad button pressed\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "/** @type {Gamepad} */\r", "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", "if (!gamepad) {\r", @@ -1518,10 +1478,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in string expression: \"Last pressed button (LastButtonString)\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "if (controllerType === \"\") {\r", " console.error('Parameter controller type is not valid in string expression: \"Last pressed button (LastButtonString)\"');\r", " return;\r", @@ -1609,10 +1565,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Gamepad button pressed\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "if (button === \"\") {\r", " console.error('Parameter button is not valid in condition: \"Gamepad button pressed\"');\r", " eventsFunctionContext.returnValue = false;\r", @@ -1681,10 +1633,6 @@ "//Get function parameter\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in expression: \"Gamepad deadzone for sticks\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "///Return the deadzone value for a given player\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).deadzone;" ], @@ -1719,11 +1667,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const newDeadzone = eventsFunctionContext.getArgument(\"deadzone\");\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in action: \"Set gamepad deadzone for sticks\", is not valid, must be between 0 and 4.');\r", - " return;\r", - "}\r", - "\r", "// clamp the newDeadzone in range [0, 1].\r", "// https://github.com/4ian/GDevelop-extensions/pull/33#issuecomment-618224857\r", "gdjs._extensionController.getPlayer(playerId).deadzone = gdjs.evtTools.common.clamp(newDeadzone, 0, 1);\r", @@ -1763,10 +1706,6 @@ "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", "\r", - "if (playerId < 0 || playerId > 4) {\r", - " console.error('Parameter gamepad identifier in condition: \"Gamepad stick pushed (axis)\", is not valid number, must be between 0 and 4.');\r", - " return;\r", - "}\r", "if (stick != \"LEFT\" && stick != \"RIGHT\") {\r", " console.error('Parameter stick in condition: \"Gamepad stick pushed (axis)\", is not valid, must be LEFT or RIGHT');\r", " return;\r", @@ -1781,16 +1720,9 @@ " // The gamepad is not connected.\r", " return;\r", "}\r", - "switch (stick) {\r", - " case 'LEFT':\r", - " eventsFunctionContext.returnValue = gdjs._extensionController.isAxisPushed(playerId, direction, gamepad.axes[0], gamepad.axes[1]);\r", - " break;\r", - " case 'RIGHT':\r", - " eventsFunctionContext.returnValue = gdjs._extensionController.isAxisPushed(playerId, direction, gamepad.axes[2], gamepad.axes[3]);\r", - " break;\r", - " default:\r", - " break;\r", - "}\r", + "const axisValueX = stick === 'RIGHT' ? gamepad.axes[2] : gamepad.axes[0];\r", + "const axisValueY = stick === 'RIGHT' ? gamepad.axes[3] : gamepad.axes[1];\r", + "eventsFunctionContext.returnValue = gdjs._extensionController.isAxisPushed(playerId, direction, axisValueX, axisValueY);\r", "" ], "parameterObjects": "", @@ -1865,10 +1797,6 @@ "//Get function parameter", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in string expression: \"Gamepad type\", is not valid number, must be between 0 and 4');", - " return;", - "}", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", @@ -1909,10 +1837,6 @@ "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();", "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in condition: \"Gamepad type\", is not valid number, must be between 0 and 4.');", - " return;", - "}", "if (controllerType === \"\") {", " console.error('Parameter type in condition: \"Gamepad type\", is not a string.');", " return;", @@ -1964,11 +1888,6 @@ "//Get function parameter", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in condition: \"Gamepad connected\", is not valid number, must be between 0 and 4.');", - " return;", - "}", - "", "// If gamepad was disconnected it will be null (so this will return false)", "// If gamepad was never connected it will be undefined (so this will return false)", "eventsFunctionContext.returnValue = !!gamepads[playerId];" @@ -2001,10 +1920,6 @@ "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in action: \"Gamepad connected\", is not valid number, must be between 0 and 4.');", - " return;", - "}", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", @@ -2056,10 +1971,6 @@ "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 4.');", - " return;", - "}", "if (weakRumbleMagnitude < 0 || weakRumbleMagnitude > 1) {", " console.error('Parameter weakRumble identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 1.');", " return;", @@ -2136,10 +2047,6 @@ "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", "", - "if (playerId < 0 || playerId > 4) {", - " console.error('Parameter gamepad identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 4.');", - " return;", - "}", "if (weakRumbleMagnitude < 0 || weakRumbleMagnitude > 1) {", " console.error('Parameter weakRumble identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 1.');", " return;", @@ -2209,10 +2116,6 @@ "inlineCode": [ "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "if (playerId < 0 || playerId > 4) {\r", - "\tconsole.error('Parameter gamepad identifier in condition: \"Any gamepad button released\", is not valid number, must be between 0 and 4.');\r", - "\treturn;\r", - "}\r", "/** @type {Gamepad} */\r", "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", "if (!gamepad) {\r", From 4ee971f0997b5e0263f3ee3e50d8c4d8d5b1fcac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Wed, 17 Sep 2025 13:20:19 +0200 Subject: [PATCH 04/14] Add type declaration --- extensions/reviewed/Gamepads.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 59764dc8e..acdbc060d 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -107,15 +107,16 @@ " return;\r", "}\r", "\r", - "/** @type {{[number]: Player}} */\r", + "/** @type {{[playerId: number]: Player}} */\r", "const players = {};\r", "\r", "class Player {\r", " mapping = 'DEFAULT';\r", " lastButtonUsed = -1;\r", " deadzone = 0.2;\r", + " /** @type {{[buttonId: number]: {pressed: boolean}}} */\r", " previousFrameStateButtons = {};\r", - " rumble = {};\r", + " rumble = { elapsedTime: 0, duration: 0, weakMagnitude: 0, strongMagnitude: 0 };\r", "}\r", "\r", "/**\r", From 4c742b39aa6138de56c54da4ca8a2fce401cc1a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Wed, 17 Sep 2025 14:29:32 +0200 Subject: [PATCH 05/14] Start encapsulating button state --- extensions/reviewed/Gamepads.json | 62 +++++++++++++++---------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index acdbc060d..5a908e5e9 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -117,6 +117,22 @@ " /** @type {{[buttonId: number]: {pressed: boolean}}} */\r", " previousFrameStateButtons = {};\r", " rumble = { elapsedTime: 0, duration: 0, weakMagnitude: 0, strongMagnitude: 0 };\r", + "\r", + " /**\r", + " * @parame {number} buttonId\r", + " */\r", + " getButtonState(buttonId) {\r", + " let buttonState = previousFrameStateButtons[buttonId];\r", + " if (!buttonState) {\r", + " buttonState = new ButtonState();\r", + " previousFrameStateButtons[buttonId] = buttonState;\r", + " }\r", + " return buttonState;\r", + " }\r", + "}\r", + "\r", + "class ButtonState {\r", + " wasPressed = false;\r", "}\r", "\r", "/**\r", @@ -352,12 +368,12 @@ " player.lastButtonUsed = b; //Save the button pressed\r", "\r", " //Save the state of the button for the next frame.\r", - " player.previousFrameStateButtons[b] = { pressed: true };\r", + " player.getButtonState(b).wasPressed = true;\r", "\r", " // Update Last Active Controller\r", " lastActiveController = i;\r", " } else {\r", - " player.previousFrameStateButtons[b] = { pressed: false };\r", + " player.getButtonState(b).wasPressed = false;\r", " }\r", " }\r", "\r", @@ -1331,23 +1347,16 @@ " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "//Define default value on pressed button or use previous value\r", "const player = gdjs._extensionController.getPlayer(playerId)\r", - "player.previousFrameStateButtons[buttonId] = player.previousFrameStateButtons[buttonId] || { pressed: false };\r", - "\r", - "//Get state of button at previous frame\r", - "const previousStateButton = player.previousFrameStateButtons[buttonId].pressed;\r", - "\r", - "//When previousStateButton is true and actual button state is not pressed\r", - "//Player have release the button\r", - "if (previousStateButton === true && gamepad.buttons[buttonId].pressed === false) {\r", - " // Save the last button used for the player \r", + "const buttonState = player.getButtonState(buttonId);\r", + "if (buttonState.wasPressed && !gamepad.buttons[buttonId].pressed) {\r", + " // The button is released \r", " player.lastButtonUsed = buttonId;\r", - " player.previousFrameStateButtons[buttonId].pressed = true;\r", + " buttonState.wasPressed = true;\r", " eventsFunctionContext.returnValue = true;\r", "\r", "} else {\r", - " player.previousFrameStateButtons[buttonId].pressed = false;\r", + " buttonState.wasPressed = false;\r", " eventsFunctionContext.returnValue = false;\r", "}\r", "" @@ -2124,34 +2133,23 @@ " return;\r", "}\r", "for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) { //For each buttons on current frame.\r", - "\r", "\tif (buttonId === undefined) {\r", - "\t\teventsFunctionContext.returnValue = false;\r", "\t\treturn;\r", "\t}\r", - "\t//Get previous value or define value by default for the current button\r", "\tconst player = gdjs._extensionController.getPlayer(playerId)\r", - "\tplayer.previousFrameStateButtons[buttonId] = player.previousFrameStateButtons[buttonId] || { pressed: false };\r", - "\r", - "\t//Get state of the button at previous frame\r", - "\tconst previousStateButtonIsPressed = player.previousFrameStateButtons[buttonId].pressed;\r", - "\r", - "\t//Get the state of the button on the current frame.\r", - "\tconst currentFrameStateButtonIsPressed = gamepad.buttons[buttonId].pressed;\r", - "\r", - "\t//When previousStateButtonIsPressed is true and actual button state is not pressed\r", - "\t//Player have release the button\r", - "\tif (previousStateButtonIsPressed === true && currentFrameStateButtonIsPressed === false) {\r", - "\t\tplayer.previousFrameStateButtons[buttonId].pressed = true;\r", + "\tconst buttonState = player.getButtonState(buttonId);\r", + "\tconst isPressed = gamepad.buttons[buttonId].pressed;\r", + "\tif (buttonState.wasPressed && !isPressed) {\r", + "\t\t// The button is released\r", + "\t\tbuttonState.wasPressed = true;\r", "\t\teventsFunctionContext.returnValue = true;\r", - "\t\t//break;\r", "\t\treturn;\r", "\t} else {\r", "\t\t//The player didn't released the button yet, the previous frame state is still true\r", - "\t\tplayer.previousFrameStateButtons[buttonId].pressed = false;\r", + "\t\tbuttonState.wasPressed = false;\r", "\t\teventsFunctionContext.returnValue = false;\r", "\t}\r", - "\tif (currentFrameStateButtonIsPressed) player.lastButtonUsed = buttonId;\r", + "\tif (isPressed) player.lastButtonUsed = buttonId;\r", "}\r", "" ], From 9ae9895a21ea3e8080de98c8fe4f4299d5c502bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Wed, 17 Sep 2025 19:13:02 +0200 Subject: [PATCH 06/14] Fix last button pressed condition --- extensions/reviewed/Gamepads.json | 200 ++++++++++++------------------ 1 file changed, 77 insertions(+), 123 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 5a908e5e9..ac3aeb0b3 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -107,46 +107,6 @@ " return;\r", "}\r", "\r", - "/** @type {{[playerId: number]: Player}} */\r", - "const players = {};\r", - "\r", - "class Player {\r", - " mapping = 'DEFAULT';\r", - " lastButtonUsed = -1;\r", - " deadzone = 0.2;\r", - " /** @type {{[buttonId: number]: {pressed: boolean}}} */\r", - " previousFrameStateButtons = {};\r", - " rumble = { elapsedTime: 0, duration: 0, weakMagnitude: 0, strongMagnitude: 0 };\r", - "\r", - " /**\r", - " * @parame {number} buttonId\r", - " */\r", - " getButtonState(buttonId) {\r", - " let buttonState = previousFrameStateButtons[buttonId];\r", - " if (!buttonState) {\r", - " buttonState = new ButtonState();\r", - " previousFrameStateButtons[buttonId] = buttonState;\r", - " }\r", - " return buttonState;\r", - " }\r", - "}\r", - "\r", - "class ButtonState {\r", - " wasPressed = false;\r", - "}\r", - "\r", - "/**\r", - " * @param {number} playerId\r", - " */\r", - "function getPlayer(playerId) {\r", - " let player = players[playerId];\r", - " if (!player) {\r", - " player = new Player();\r", - " players[playerId] = player;\r", - " }\r", - " return player;\r", - "}\r", - "\r", "let lastActiveController = -1;\r", "/**\r", " * Associate controller button ids to button names\r", @@ -193,6 +153,78 @@ " 17: \"CLICK_TOUCHPAD\"\r", " }\r", "};\r", + "/** @type {{[playerId: number]: Player}} */\r", + "const players = {};\r", + "\r", + "class Player {\r", + " mapping = 'DEFAULT';\r", + " lastButtonUsed = -1;\r", + " deadzone = 0.2;\r", + " /** @type {{[buttonId: number]: ButtonState}} */\r", + " buttonStates = {};\r", + " rumble = { elapsedTime: 0, duration: 0, weakMagnitude: 0, strongMagnitude: 0 };\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " getButtonState(buttonId) {\r", + " let buttonState = this.buttonStates[buttonId];\r", + " if (!buttonState) {\r", + " buttonState = new ButtonState();\r", + " this.buttonStates[buttonId] = buttonState;\r", + " }\r", + " return buttonState;\r", + " }\r", + "}\r", + "\r", + "class ButtonState {\r", + " wasPressed = false;\r", + "}\r", + "\r", + "/**\r", + " * @param {number} playerId\r", + " */\r", + "function getPlayer(playerId) {\r", + " let player = players[playerId];\r", + " if (!player) {\r", + " player = new Player();\r", + " players[playerId] = player;\r", + " }\r", + " return player;\r", + "}\r", + "\r", + "function onScenePostEvents() {\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + " for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " let gamepad = gamepads[playerId];\r", + " if (gamepad == null) {\r", + " // The gamepad is not connected.\r", + " continue;\r", + " }\r", + " const player = getPlayer(playerId);\r", + "\r", + " for (let buttonId = 0; buttonId < Object.keys(gamepad.buttons).length; buttonId++) {\r", + " const buttonState = player.getButtonState(buttonId);\r", + " const isPressed = gamepad.buttons[buttonId].pressed;\r", + " if (isPressed) {\r", + " lastActiveController = playerId;\r", + " if (!buttonState.wasPressed) {\r", + " player.lastButtonUsed = buttonId;\r", + " }\r", + " }\r", + " buttonState.wasPressed = isPressed;\r", + " }\r", + " const rumble = player.rumble;\r", + " rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;\r", + " if (rumble.duration - rumble.elapsedTime <= 0 &&\r", + " (rumble.weakMagnitude || rumble.strongMagnitude)\r", + " ) {\r", + " rumble.weakMagnitude = 0;\r", + " rumble.strongMagnitude = 0;\r", + " }\r", + " }\r", + "}\r", "\r", "/**\r", " * @param {string} type\r", @@ -351,43 +383,6 @@ " }\r", "}\r", "\r", - "function update() {\r", - " //Each time a player press a button i save the last button pressed for the next frame\r", - " /** @type {Gamepad[]} */\r", - " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - " for (let i = 0; i < gamepads.length; i++) {\r", - " let gamepad = gamepads[i]; // Get the gamepad of the player\r", - " if (gamepad == null) {\r", - " // The gamepad is not connected.\r", - " continue;\r", - " }\r", - " const player = getPlayer(i);\r", - "\r", - " for (let b = 0; b < Object.keys(gamepad.buttons).length; b++) { //For each buttons\r", - " if (gamepad.buttons[b].pressed) { //One of them is pressed\r", - " player.lastButtonUsed = b; //Save the button pressed\r", - "\r", - " //Save the state of the button for the next frame.\r", - " player.getButtonState(b).wasPressed = true;\r", - "\r", - " // Update Last Active Controller\r", - " lastActiveController = i;\r", - " } else {\r", - " player.getButtonState(b).wasPressed = false;\r", - " }\r", - " }\r", - "\r", - " player.rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;\r", - " if (\r", - " player.rumble.duration - player.rumble.elapsedTime <= 0 &&\r", - " (player.rumble.weakMagnitude || player.rumble.strongMagnitude)\r", - " ) {\r", - " player.rumble.weakMagnitude = 0;\r", - " player.rumble.strongMagnitude = 0;\r", - " }\r", - " }\r", - "}\r", - "\r", "gdjs._extensionController = {\r", " getPlayer,\r", " lastActiveController,\r", @@ -399,7 +394,7 @@ " getNormalizedAxisValue,\r", " isAxisPushed,\r", " getGamepad,\r", - " update,\r", + " onScenePostEvents,\r", "}" ], "parameterObjects": "", @@ -419,7 +414,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "gdjs._extensionController.update();", + "gdjs._extensionController.onScenePostEvents();", "" ], "parameterObjects": "", @@ -1325,11 +1320,6 @@ "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", - "\r", - "if (button === \"\") {\r", - " console.error('Parameter button is not valid in condition: \"Gamepad button released\"');\r", - " return;\r", - "}\r", "/** @type {Gamepad} */\r", "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", "if (!gamepad) {\r", @@ -1349,17 +1339,7 @@ "}\r", "const player = gdjs._extensionController.getPlayer(playerId)\r", "const buttonState = player.getButtonState(buttonId);\r", - "if (buttonState.wasPressed && !gamepad.buttons[buttonId].pressed) {\r", - " // The button is released \r", - " player.lastButtonUsed = buttonId;\r", - " buttonState.wasPressed = true;\r", - " eventsFunctionContext.returnValue = true;\r", - "\r", - "} else {\r", - " buttonState.wasPressed = false;\r", - " eventsFunctionContext.returnValue = false;\r", - "}\r", - "" + "eventsFunctionContext.returnValue = buttonState.wasPressed && !gamepad.buttons[buttonId].pressed;" ], "parameterObjects": "", "useStrict": true, @@ -1426,7 +1406,6 @@ "inlineCode": [ "//Get function parameter\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "\r", "/** @type {Gamepad} */\r", "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", "if (!gamepad) {\r", @@ -1440,24 +1419,17 @@ " break;\r", " }\r", "}\r", - "\r", "if (buttonId === undefined) {\r", " // No buttons are pressed.\r", " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "\r", "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", " console.error('Buttons on the gamepad are not accessible in condition: \"Any gamepad button pressed\"');\r", " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "\r", - "//When a button is pressed, save the button in lastButtonUsed for each players\r", - "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.getPlayer(playerId).lastButtonUsed = buttonId;\r", "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", - "\r", - "\r", "" ], "parameterObjects": "", @@ -1574,19 +1546,13 @@ "//Get function parameters\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", - "\r", - "if (button === \"\") {\r", - " console.error('Parameter button is not valid in condition: \"Gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", "/** @type {Gamepad} */\r", "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", "if (!gamepad) {\r", " // The gamepad is not connected.\r", " return;\r", "}\r", - "let buttonId = gdjs._extensionController.getButtonId(button);\r", + "const buttonId = gdjs._extensionController.getButtonId(button);\r", "if (buttonId === undefined) {\r", " console.error('There is no buttons valid in condition: \"Gamepad button pressed\"');\r", " eventsFunctionContext.returnValue = false;\r", @@ -1597,12 +1563,7 @@ " eventsFunctionContext.returnValue = false;\r", " return;\r", "}\r", - "//When a button is pressed, save the button in lastButtonUsed for each players\r", - "if (gamepad.buttons[buttonId].pressed) gdjs._extensionController.getPlayer(playerId).lastButtonUsed = buttonId;\r", "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", - "\r", - "\r", - "\r", "" ], "parameterObjects": "", @@ -2132,24 +2093,17 @@ " // The gamepad is not connected.\r", " return;\r", "}\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", "for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) { //For each buttons on current frame.\r", "\tif (buttonId === undefined) {\r", "\t\treturn;\r", "\t}\r", - "\tconst player = gdjs._extensionController.getPlayer(playerId)\r", "\tconst buttonState = player.getButtonState(buttonId);\r", "\tconst isPressed = gamepad.buttons[buttonId].pressed;\r", "\tif (buttonState.wasPressed && !isPressed) {\r", - "\t\t// The button is released\r", - "\t\tbuttonState.wasPressed = true;\r", "\t\teventsFunctionContext.returnValue = true;\r", "\t\treturn;\r", - "\t} else {\r", - "\t\t//The player didn't released the button yet, the previous frame state is still true\r", - "\t\tbuttonState.wasPressed = false;\r", - "\t\teventsFunctionContext.returnValue = false;\r", "\t}\r", - "\tif (isPressed) player.lastButtonUsed = buttonId;\r", "}\r", "" ], From 4ff28392a9155f16aff6d76ca9b2ff7f5b6c8a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Thu, 18 Sep 2025 12:39:07 +0200 Subject: [PATCH 07/14] Encapsulate button state --- extensions/reviewed/Gamepads.json | 188 ++++++++++++++++-------------- 1 file changed, 98 insertions(+), 90 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index ac3aeb0b3..31388552d 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -156,7 +156,21 @@ "/** @type {{[playerId: number]: Player}} */\r", "const players = {};\r", "\r", + "/**\r", + " * @param {number} playerId\r", + " */\r", + "function getPlayer(playerId) {\r", + " let player = players[playerId];\r", + " if (!player) {\r", + " player = new Player(playerId);\r", + " players[playerId] = player;\r", + " }\r", + " return player;\r", + "}\r", + "\r", "class Player {\r", + " /** @type {number} */\r", + " playerId;\r", " mapping = 'DEFAULT';\r", " lastButtonUsed = -1;\r", " deadzone = 0.2;\r", @@ -165,6 +179,13 @@ " rumble = { elapsedTime: 0, duration: 0, weakMagnitude: 0, strongMagnitude: 0 };\r", "\r", " /**\r", + " * @param {number} playerId\r", + " */\r", + " constructor(playerId) {\r", + " this.playerId = playerId;\r", + " }\r", + "\r", + " /**\r", " * @param {number} buttonId\r", " */\r", " getButtonState(buttonId) {\r", @@ -175,24 +196,74 @@ " }\r", " return buttonState;\r", " }\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " isButtonPressed(buttonId) {\r", + " /** @type {Gamepad} */\r", + " const gamepad = getGamepad(playerId);\r", + " if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return false;\r", + " }\r", + " return gamepad.buttons[buttonId].pressed;\r", + " }\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " isButtonReleased(buttonId) {\r", + " /** @type {Gamepad} */\r", + " const gamepad = getGamepad(playerId);\r", + " if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return false;\r", + " }\r", + " const isPressed = gamepad.buttons[buttonId].pressed;\r", + " const buttonState = this.getButtonState(buttonId);\r", + " return buttonState.wasPressed && !isPressed;\r", + " }\r", + "\r", + " isAnyButtonReleased() {\r", + " /** @type {Gamepad} */\r", + " const gamepad = getGamepad(playerId);\r", + " if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + " }\r", + " for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {\r", + " if (buttonId === undefined) {\r", + " return;\r", + " }\r", + " const buttonState = this.getButtonState(buttonId);\r", + " const isPressed = gamepad.buttons[buttonId].pressed;\r", + " if (buttonState.wasPressed && !isPressed) {\r", + " return true;\r", + " }\r", + " }\r", + " }\r", + "\r", + " isAnyButtonPressed() {\r", + " /** @type {Gamepad} */\r", + " const gamepad = getGamepad(playerId);\r", + " if (!gamepad) {\r", + " // The gamepad is not connected.\r", + " return;\r", + " }\r", + " for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {\r", + " if (gamepad.buttons[buttonId].pressed) {\r", + " return true;\r", + " }\r", + " }\r", + " return false;\r", + " }\r", "}\r", "\r", "class ButtonState {\r", " wasPressed = false;\r", "}\r", "\r", - "/**\r", - " * @param {number} playerId\r", - " */\r", - "function getPlayer(playerId) {\r", - " let player = players[playerId];\r", - " if (!player) {\r", - " player = new Player();\r", - " players[playerId] = player;\r", - " }\r", - " return player;\r", - "}\r", - "\r", "function onScenePostEvents() {\r", " /** @type {Gamepad[]} */\r", " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", @@ -288,7 +359,7 @@ " return 17;\r", " default:\r", " console.error('The gamepad button: ' + buttonName + ' is not valid.');\r", - " break;\r", + " return null;\r", " }\r", "}\r", "\r", @@ -1317,29 +1388,16 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", - "/** @type {Gamepad} */\r", - "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", - "if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return;\r", - "}\r", + "\r", "let buttonId = gdjs._extensionController.getButtonId(button);\r", - "if (buttonId === undefined) {\r", - " console.error('There is no buttons valid in condition: \"Gamepad button released\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", - " console.error('Buttons on the gamepad are not accessible in condition: \"Gamepad button released\"');\r", - " eventsFunctionContext.returnValue = false;\r", + "if (buttonId === null) {\r", " return;\r", "}\r", "const player = gdjs._extensionController.getPlayer(playerId)\r", - "const buttonState = player.getButtonState(buttonId);\r", - "eventsFunctionContext.returnValue = buttonState.wasPressed && !gamepad.buttons[buttonId].pressed;" + "eventsFunctionContext.returnValue = player.isButtonReleased(buttonId);" ], "parameterObjects": "", "useStrict": true, @@ -1404,32 +1462,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "/** @type {Gamepad} */\r", - "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", - "if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return;\r", - "}\r", - "let buttonId;\r", - "for (let i = 0; i < gamepad.buttons.length; i++) { //For each buttons\r", - " if (gamepad.buttons[i].pressed) { //One of them is pressed\r", - " buttonId = i; //Save the button pressed\r", - " break;\r", - " }\r", - "}\r", - "if (buttonId === undefined) {\r", - " // No buttons are pressed.\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", - " console.error('Buttons on the gamepad are not accessible in condition: \"Any gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isAnyButtonPressed();\r", "" ], "parameterObjects": "", @@ -1543,27 +1579,16 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", - "/** @type {Gamepad} */\r", - "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", - "if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return;\r", - "}\r", + "\r", "const buttonId = gdjs._extensionController.getButtonId(button);\r", - "if (buttonId === undefined) {\r", - " console.error('There is no buttons valid in condition: \"Gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", + "if (buttonId === null) {\r", " return;\r", "}\r", - "if (gamepad.buttons == null || gamepad.buttons[buttonId] == null) {\r", - " console.error('Buttons on the gamepad are not accessible in condition: \"Gamepad button pressed\"');\r", - " eventsFunctionContext.returnValue = false;\r", - " return;\r", - "}\r", - "eventsFunctionContext.returnValue = gamepad.buttons[buttonId].pressed;\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isButtonPressed(buttonId);\r", "" ], "parameterObjects": "", @@ -2085,27 +2110,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "/** @type {Gamepad} */\r", - "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", - "if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return;\r", - "}\r", "const player = gdjs._extensionController.getPlayer(playerId)\r", - "for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) { //For each buttons on current frame.\r", - "\tif (buttonId === undefined) {\r", - "\t\treturn;\r", - "\t}\r", - "\tconst buttonState = player.getButtonState(buttonId);\r", - "\tconst isPressed = gamepad.buttons[buttonId].pressed;\r", - "\tif (buttonState.wasPressed && !isPressed) {\r", - "\t\teventsFunctionContext.returnValue = true;\r", - "\t\treturn;\r", - "\t}\r", - "}\r", - "" + "eventsFunctionContext.returnValue = player.isAnyButtonReleased();" ], "parameterObjects": "", "useStrict": true, From fbf4f1e79b593a4c20a56d4b597c9c9cc1b6fcb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Thu, 18 Sep 2025 14:58:19 +0200 Subject: [PATCH 08/14] Clean up --- extensions/reviewed/Gamepads.json | 95 ++++++++----------------------- 1 file changed, 24 insertions(+), 71 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 31388552d..59c7c113a 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -202,7 +202,7 @@ " */\r", " isButtonPressed(buttonId) {\r", " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(playerId);\r", + " const gamepad = getGamepad(this.playerId);\r", " if (!gamepad) {\r", " // The gamepad is not connected.\r", " return false;\r", @@ -215,7 +215,7 @@ " */\r", " isButtonReleased(buttonId) {\r", " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(playerId);\r", + " const gamepad = getGamepad(this.playerId);\r", " if (!gamepad) {\r", " // The gamepad is not connected.\r", " return false;\r", @@ -227,7 +227,7 @@ "\r", " isAnyButtonReleased() {\r", " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(playerId);\r", + " const gamepad = getGamepad(this.playerId);\r", " if (!gamepad) {\r", " // The gamepad is not connected.\r", " return;\r", @@ -246,7 +246,7 @@ "\r", " isAnyButtonPressed() {\r", " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(playerId);\r", + " const gamepad = getGamepad(this.playerId);\r", " if (!gamepad) {\r", " // The gamepad is not connected.\r", " return;\r", @@ -923,7 +923,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const trigger = eventsFunctionContext.getArgument(\"trigger\").toUpperCase();\r", "\r", @@ -937,7 +937,6 @@ " // The gamepad is not connected.\r", " return;\r", "}\r", - "\r", "switch (trigger) {\r", " case 'LT':\r", " case 'L2':\r", @@ -987,7 +986,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "\r", @@ -1081,7 +1080,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "\r", @@ -1143,7 +1142,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", @@ -1286,7 +1285,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", "\r", @@ -1337,7 +1336,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"Gamepad\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"Stick\").toLowerCase();\r", "\r", @@ -1429,10 +1428,8 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", - "//Return the last button used by the player\r", + "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).lastButtonUsed;" ], "parameterObjects": "", @@ -1492,14 +1489,9 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();\r", - "\r", - "if (controllerType === \"\") {\r", - " console.error('Parameter controller type is not valid in string expression: \"Last pressed button (LastButtonString)\"');\r", - " return;\r", - "}\r", "/** @type {Gamepad} */\r", "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", "if (!gamepad) {\r", @@ -1545,17 +1537,8 @@ "inlineCode": [ "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "//Get the last activated controller\r", "const controllerId = gdjs._extensionController.lastActiveController;\r", - "\r", - "// Check if controller is active\r", - "const gamepad = gamepads[controllerId];\r", - "if (gamepad == null) {\r", - " eventsFunctionContext.returnValue = 0;\r", - "} else {\r", - " // Return active controller id\r", - " eventsFunctionContext.returnValue = controllerId + 1;\r", - "}\r", + "eventsFunctionContext.returnValue = gamepads[controllerId] ? controllerId + 1 : 0;\r", "" ], "parameterObjects": "", @@ -1626,10 +1609,8 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "\r", - "///Return the deadzone value for a given player\r", + "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).deadzone;" ], "parameterObjects": "", @@ -1659,11 +1640,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const newDeadzone = eventsFunctionContext.getArgument(\"deadzone\");\r", "\r", - "// clamp the newDeadzone in range [0, 1].\r", "// https://github.com/4ian/GDevelop-extensions/pull/33#issuecomment-618224857\r", "gdjs._extensionController.getPlayer(playerId).deadzone = gdjs.evtTools.common.clamp(newDeadzone, 0, 1);\r", "" @@ -1697,7 +1677,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters\r", + "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", @@ -1790,9 +1770,8 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameter", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "", + "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", @@ -1829,14 +1808,9 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters", + "", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();", - "", - "if (controllerType === \"\") {", - " console.error('Parameter type in condition: \"Gamepad type\", is not a string.');", - " return;", - "}", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", @@ -1881,7 +1855,6 @@ "/** @type {Gamepad[]} */", "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", "", - "//Get function parameter", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "", "// If gamepad was disconnected it will be null (so this will return false)", @@ -1912,10 +1885,8 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters", "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", - "", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", @@ -1961,20 +1932,11 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters", + "const { clamp } = gdjs.evtTools.common;", "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", - "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", - "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", - "", - "if (weakRumbleMagnitude < 0 || weakRumbleMagnitude > 1) {", - " console.error('Parameter weakRumble identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 1.');", - " return;", - "}", - "if (strongRumbleMagnitude < 0 || strongRumbleMagnitude > 1) {", - " console.error('Parameter strongRumble identifier in action: \"Advanced gamepad vibration\", is not valid number, must be between 0 and 1.');", - " return;", - "}", + "const strongRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"StrongMagnitude\"), 0, 1);", + "const weakRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"WeakMagnitude\"), 0, 1);", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", @@ -2035,22 +1997,13 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "//Get function parameters", + "const { clamp } = gdjs.evtTools.common;", "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", "const player = gdjs._extensionController.getPlayer(playerId);", "const elapsedTime = player.rumble.elapsedTime || 0;", "const originalDuration = player.rumble.duration || 1;", - "const strongRumbleMagnitude = eventsFunctionContext.getArgument(\"StrongMagnitude\");", - "const weakRumbleMagnitude = eventsFunctionContext.getArgument(\"WeakMagnitude\");", - "", - "if (weakRumbleMagnitude < 0 || weakRumbleMagnitude > 1) {", - " console.error('Parameter weakRumble identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 1.');", - " return;", - "}", - "if (strongRumbleMagnitude < 0 || strongRumbleMagnitude > 1) {", - " console.error('Parameter strongRumble identifier in action: \"Change gamepad active vibration\", is not valid number, must be between 0 and 1.');", - " return;", - "}", + "const strongRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"StrongMagnitude\"), 0, 1);", + "const weakRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"WeakMagnitude\"), 0, 1);", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", From e3606a0e4f842a123a6f699832628716dd875ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Thu, 18 Sep 2025 21:51:54 +0200 Subject: [PATCH 09/14] Add a condition to check if a button was just pressed --- extensions/reviewed/Gamepads.json | 191 ++++++++++++++++++++---------- 1 file changed, 131 insertions(+), 60 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 59c7c113a..6ac1124fe 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -153,6 +153,18 @@ " 17: \"CLICK_TOUCHPAD\"\r", " }\r", "};\r", + "\r", + "/**\r", + " * @param {number} playerId\r", + " */\r", + "function getGamepad(playerId) {\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ?\r", + " navigator.getGamepads() :\r", + " (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + " return gamepads[playerId];\r", + "}\r", + "\r", "/** @type {{[playerId: number]: Player}} */\r", "const players = {};\r", "\r", @@ -201,58 +213,37 @@ " * @param {number} buttonId\r", " */\r", " isButtonPressed(buttonId) {\r", - " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(this.playerId);\r", - " if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return false;\r", - " }\r", - " return gamepad.buttons[buttonId].pressed;\r", + " return this.getButtonState(buttonId).isPressed;\r", + " }\r", + "\r", + " /**\r", + " * @param {number} buttonId\r", + " */\r", + " isButtonJustPressed(buttonId) {\r", + " return this.getButtonState(buttonId).isJustPressed();\r", " }\r", "\r", " /**\r", " * @param {number} buttonId\r", " */\r", " isButtonReleased(buttonId) {\r", - " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(this.playerId);\r", - " if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return false;\r", - " }\r", - " const isPressed = gamepad.buttons[buttonId].pressed;\r", - " const buttonState = this.getButtonState(buttonId);\r", - " return buttonState.wasPressed && !isPressed;\r", + " return this.getButtonState(buttonId).isReleased();\r", " }\r", "\r", " isAnyButtonReleased() {\r", - " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(this.playerId);\r", - " if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return;\r", - " }\r", - " for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {\r", - " if (buttonId === undefined) {\r", - " return;\r", - " }\r", - " const buttonState = this.getButtonState(buttonId);\r", - " const isPressed = gamepad.buttons[buttonId].pressed;\r", - " if (buttonState.wasPressed && !isPressed) {\r", + " for (const buttonId in this.buttonStates) {\r", + " const buttonState = this.buttonStates[buttonId];\r", + " if (buttonState.isReleased()) {\r", " return true;\r", " }\r", " }\r", + " return false;\r", " }\r", "\r", " isAnyButtonPressed() {\r", - " /** @type {Gamepad} */\r", - " const gamepad = getGamepad(this.playerId);\r", - " if (!gamepad) {\r", - " // The gamepad is not connected.\r", - " return;\r", - " }\r", - " for (let buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {\r", - " if (gamepad.buttons[buttonId].pressed) {\r", + " for (const buttonId in this.buttonStates) {\r", + " const buttonState = this.buttonStates[buttonId];\r", + " if (buttonState.isPressed) {\r", " return true;\r", " }\r", " }\r", @@ -262,8 +253,47 @@ "\r", "class ButtonState {\r", " wasPressed = false;\r", + " isPressed = false;\r", + "\r", + " isReleased() {\r", + " return this.wasPressed && !this.isPressed;\r", + " }\r", + "\r", + " isJustPressed() {\r", + " return !this.wasPressed && this.isPressed;\r", + " }\r", "}\r", "\r", + "// Async tasks are run before everything.\r", + "// This is a hack to make sure that button states are updated\r", + "// before mapping behavior events.\r", + "const frameBeginningTask = new class extends gdjs.AsyncTask {\r", + " update() {\r", + " /** @type {Gamepad[]} */\r", + " const gamepads = navigator.getGamepads ?\r", + " navigator.getGamepads() :\r", + " (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + " for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " const gamepad = gamepads[playerId];\r", + " if (gamepad == null) {\r", + " // The gamepad is not connected.\r", + " continue;\r", + " }\r", + " const player = getPlayer(playerId);\r", + "\r", + " for (let buttonId = 0; buttonId < Object.keys(gamepad.buttons).length; buttonId++) {\r", + " const buttonState = player.getButtonState(buttonId);\r", + " buttonState.wasPressed = buttonState.isPressed;\r", + " buttonState.isPressed = gamepad.buttons[buttonId].pressed;\r", + " if (buttonState.isJustPressed()) {\r", + " player.lastButtonUsed = buttonId;\r", + " }\r", + " }\r", + " }\r", + " return false;\r", + " }\r", + "}();\r", + "\r", "function onScenePostEvents() {\r", " /** @type {Gamepad[]} */\r", " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", @@ -274,18 +304,6 @@ " continue;\r", " }\r", " const player = getPlayer(playerId);\r", - "\r", - " for (let buttonId = 0; buttonId < Object.keys(gamepad.buttons).length; buttonId++) {\r", - " const buttonState = player.getButtonState(buttonId);\r", - " const isPressed = gamepad.buttons[buttonId].pressed;\r", - " if (isPressed) {\r", - " lastActiveController = playerId;\r", - " if (!buttonState.wasPressed) {\r", - " player.lastButtonUsed = buttonId;\r", - " }\r", - " }\r", - " buttonState.wasPressed = isPressed;\r", - " }\r", " const rumble = player.rumble;\r", " rumble.elapsedTime += runtimeScene.getElapsedTime(runtimeScene) / 1000;\r", " if (rumble.duration - rumble.elapsedTime <= 0 &&\r", @@ -391,15 +409,6 @@ "}\r", "\r", "/**\r", - " * @param {number} playerId\r", - " */\r", - "function getGamepad(playerId) {\r", - " /** @type {Gamepad[]} */\r", - " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", - " return gamepads[playerId];\r", - "}\r", - "\r", - "/**\r", " * @param {number} deltaX\r", " * @param {number} deltaY\r", " */\r", @@ -466,6 +475,7 @@ " isAxisPushed,\r", " getGamepad,\r", " onScenePostEvents,\r", + " frameBeginningTask,\r", "}" ], "parameterObjects": "", @@ -476,6 +486,28 @@ "parameters": [], "objectGroups": [] }, + { + "fullName": "", + "functionType": "Action", + "name": "onSceneLoaded", + "sentence": "", + "events": [ + { + "type": "BuiltinCommonInstructions::JsCode", + "inlineCode": [ + "// Async tasks are run before everything.\r", + "// This is a hack to make sure that button states are updated\r", + "// before mapping behavior events.\r", + "runtimeScene.getAsyncTasksManager().addTask(gdjs._extensionController.frameBeginningTask);" + ], + "parameterObjects": "", + "useStrict": true, + "eventsSheetExpanded": false + } + ], + "parameters": [], + "objectGroups": [] + }, { "fullName": "", "functionType": "Action", @@ -1418,6 +1450,47 @@ ], "objectGroups": [] }, + { + "description": "Check if a button was just pressed on a gamepad. Buttons can be:\n* Xbox: \"A\", \"B\", \"X\", \"Y\", \"LB\", \"RB\", \"LT\", \"RT\", \"BACK\", \"START\",\n* PS4: \"CROSS\", \"SQUARE\", \"CIRCLE\", \"TRIANGLE\", \"L1\", \"L2\", \"R1\", \"R2\", \"SHARE\", \"OPTIONS\", \"PS_BUTTON\", \"CLICK_TOUCHPAD\",\n* Other: \"UP\", \"DOWN\", \"LEFT\", \"RIGHT\", \"CLICK_STICK_LEFT\", \"CLICK_STICK_RIGHT\".", + "fullName": "Gamepad button just pressed", + "functionType": "Condition", + "name": "IsButtonJustPressed", + "sentence": "Button _PARAM2_ of gamepad _PARAM1_ was just pressed", + "events": [ + { + "type": "BuiltinCommonInstructions::JsCode", + "inlineCode": [ + "\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const button = eventsFunctionContext.getArgument(\"Button\").toUpperCase();\r", + "\r", + "let buttonId = gdjs._extensionController.getButtonId(button);\r", + "if (buttonId === null) {\r", + " return;\r", + "}\r", + "const player = gdjs._extensionController.getPlayer(playerId)\r", + "eventsFunctionContext.returnValue = player.isButtonJustPressed(buttonId);" + ], + "parameterObjects": "", + "useStrict": true, + "eventsSheetExpanded": true + } + ], + "parameters": [ + { + "description": "The gamepad identifier: 1, 2, 3 or 4", + "name": "PlayerId", + "type": "expression" + }, + { + "description": "Name of the button", + "name": "Button", + "supplementaryInformation": "[\"A\",\"Cross\",\"B\",\"Circle\",\"X\",\"Square\",\"Y\",\"Triangle\",\"LB\",\"L1\",\"RB\",\"R1\",\"LT\",\"L2\",\"RT\",\"R2\",\"Up\",\"Down\",\"Left\",\"Right\",\"Back\",\"Share\",\"Start\",\"Options\",\"Click_Stick_Left\",\"Click_Stick_Right\",\"PS_Button\",\"Click_Touchpad\"]", + "type": "stringWithSelector" + } + ], + "objectGroups": [] + }, { "description": "Return the index of the last pressed button of a gamepad.", "fullName": "Last pressed button (id)", @@ -1770,7 +1843,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", @@ -1808,7 +1880,6 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();", "/** @type {Gamepad} */", From 99e3c8d25619a35c4d45fbd73735a54e41d8b13c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Fri, 19 Sep 2025 12:12:46 +0200 Subject: [PATCH 10/14] Fix the "Connected gamepads count" expression --- extensions/reviewed/Gamepads.json | 33 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 6ac1124fe..993eea934 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -1608,11 +1608,16 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "const controllerId = gdjs._extensionController.lastActiveController;\r", - "eventsFunctionContext.returnValue = gamepads[controllerId] ? controllerId + 1 : 0;\r", - "" + "const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", + "let lastGamepadIndex = -1;\r", + "for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " // Gamepads can be disconnected and become null\r", + " if (gamepads[playerId]) {\r", + " lastGamepadIndex = playerId\r", + " }\r", + "}\r", + "eventsFunctionContext.returnValue = lastGamepadIndex + 1;" ], "parameterObjects": "", "useStrict": true, @@ -1802,24 +1807,24 @@ }, { "description": "Return the number of connected gamepads.", - "fullName": "Connected gamepads number", + "fullName": "Connected gamepads count", "functionType": "Expression", "name": "ConnectedGamepadsCount", "sentence": "", "events": [ - { - "type": "BuiltinCommonInstructions::Standard", - "conditions": [], - "actions": [] - }, { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */\r", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", "\r", - "// Gamepads can be disconnected and become null, so we have to filter them.\r", - "eventsFunctionContext.returnValue = Object.keys(gamepads).filter(key => !!gamepads[key]).length;\r", + "const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", + "let connectedGamepadCount = 0;\r", + "for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", + " // Gamepads can be disconnected and become null\r", + " if (gamepads[playerId]) {\r", + " connectedGamepadCount++;\r", + " }\r", + "}\r", + "eventsFunctionContext.returnValue = connectedGamepadCount;\r", "" ], "parameterObjects": "", From 8a3f99d8b68d3d95101245cab520694c44190e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Fri, 19 Sep 2025 12:20:13 +0200 Subject: [PATCH 11/14] Remove webkitGetGamepads fallback --- extensions/reviewed/Gamepads.json | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 993eea934..e3c77bffb 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -159,9 +159,7 @@ " */\r", "function getGamepad(playerId) {\r", " /** @type {Gamepad[]} */\r", - " const gamepads = navigator.getGamepads ?\r", - " navigator.getGamepads() :\r", - " (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", " return gamepads[playerId];\r", "}\r", "\r", @@ -270,9 +268,7 @@ "const frameBeginningTask = new class extends gdjs.AsyncTask {\r", " update() {\r", " /** @type {Gamepad[]} */\r", - " const gamepads = navigator.getGamepads ?\r", - " navigator.getGamepads() :\r", - " (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", " for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", " const gamepad = gamepads[playerId];\r", " if (gamepad == null) {\r", @@ -296,7 +292,7 @@ "\r", "function onScenePostEvents() {\r", " /** @type {Gamepad[]} */\r", - " const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);\r", + " const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];\r", " for (let playerId = 0; playerId < gamepads.length; playerId++) {\r", " let gamepad = gamepads[playerId];\r", " if (gamepad == null) {\r", @@ -1928,11 +1924,10 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "/** @type {Gamepad[]} */", - "const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);", "", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", - "", + "/** @type {Gamepad[]} */", + "const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];", "// If gamepad was disconnected it will be null (so this will return false)", "// If gamepad was never connected it will be undefined (so this will return false)", "eventsFunctionContext.returnValue = !!gamepads[playerId];" From 678e4092072617e2b6f5ad70b3639eb71481a65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Fri, 19 Sep 2025 12:45:57 +0200 Subject: [PATCH 12/14] Factorize a bit --- extensions/reviewed/Gamepads.json | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index e3c77bffb..9826ec342 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -9,7 +9,7 @@ "name": "Gamepads", "previewIconUrl": "https://resources.gdevelop-app.com/assets/Icons/gamepad-variant-outline.svg", "shortDescription": "Add support for gamepads (or other controllers) to your game, giving access to information such as button presses, axis positions, trigger pressure, etc...", - "version": "0.8.1", + "version": "0.9.0", "description": [ "Add support for gamepads (or other physical controllers).", "", @@ -41,7 +41,8 @@ "authorIds": [ "2OwwM8ToR9dx9RJ2sAKTcrLmCB92", "taRwmWxwAFYFL9yyBwB3cwBw0BO2", - "mnImQKdn8nQxwzkS5D6a1JB27V23" + "mnImQKdn8nQxwzkS5D6a1JB27V23", + "IWykYNRvhCZBN3vEgKEbBPOR3Oc2" ], "dependencies": [], "globalVariables": [], @@ -107,7 +108,6 @@ " return;\r", "}\r", "\r", - "let lastActiveController = -1;\r", "/**\r", " * Associate controller button ids to button names\r", " */\r", @@ -461,7 +461,6 @@ "\r", "gdjs._extensionController = {\r", " getPlayer,\r", - " lastActiveController,\r", " controllerButtonNames,\r", " getInputString,\r", " getButtonId,\r", @@ -1014,6 +1013,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ + "const { getNormalizedAxisValue } = gdjs._extensionController;\r", "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", @@ -1031,7 +1031,9 @@ "const axisValueX = stick === 'RIGHT' ? gamepad.axes[2] : gamepad.axes[0];\r", "const axisValueY = stick === 'RIGHT' ? gamepad.axes[3] : gamepad.axes[1];\r", "\r", - "eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(Math.abs(gdjs._extensionController.getNormalizedAxisValue(axisValueX, playerId)) + Math.abs(gdjs._extensionController.getNormalizedAxisValue(axisValueY, playerId)), 0, 1);\r", + "eventsFunctionContext.returnValue = gdjs.evtTools.common.clamp(\r", + " Math.abs(getNormalizedAxisValue(axisValueX, playerId)) +\r", + " Math.abs(getNormalizedAxisValue(axisValueY, playerId)), 0, 1);\r", "" ], "parameterObjects": "", @@ -1108,6 +1110,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ + "const { getNormalizedAxisValue } = gdjs._extensionController;\r", "\r", "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", @@ -1122,19 +1125,12 @@ " // The gamepad is not connected.\r", " return;\r", "}\r", - "switch (stick) {\r", - " case 'LEFT':\r", - " eventsFunctionContext.returnValue = gdjs._extensionController.axisToAngle(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[0], playerId), gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[1], playerId));\r", - " break;\r", - "\r", - " case 'RIGHT':\r", - " eventsFunctionContext.returnValue = gdjs._extensionController.axisToAngle(gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[2], playerId), gdjs._extensionController.getNormalizedAxisValue(gamepad.axes[3], playerId));\r", - " break;\r", + "const axisValueX = stick === 'RIGHT' ? gamepad.axes[2] : gamepad.axes[0];\r", + "const axisValueY = stick === 'RIGHT' ? gamepad.axes[3] : gamepad.axes[1];\r", "\r", - " default:\r", - " eventsFunctionContext.returnValue = -1;\r", - " break;\r", - "}" + "eventsFunctionContext.returnValue = gdjs._extensionController.axisToAngle(\r", + " getNormalizedAxisValue(axisValueX, playerId),\r", + " getNormalizedAxisValue(axisValueY, playerId));" ], "parameterObjects": "", "useStrict": true, From 50b2e7bb094d93c67494b12c014c46e621f0b755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Fri, 19 Sep 2025 13:00:58 +0200 Subject: [PATCH 13/14] Rename parameters --- extensions/reviewed/Gamepads.json | 106 +++++++++++++++--------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/extensions/reviewed/Gamepads.json b/extensions/reviewed/Gamepads.json index 9826ec342..8d2f3038f 100644 --- a/extensions/reviewed/Gamepads.json +++ b/extensions/reviewed/Gamepads.json @@ -951,8 +951,8 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const trigger = eventsFunctionContext.getArgument(\"trigger\").toUpperCase();\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const trigger = eventsFunctionContext.getArgument(\"Trigger\").toUpperCase();\r", "\r", "if (trigger != \"LT\" && trigger != \"RT\" && trigger != \"L2\" && trigger != \"R2\") {\r", " console.error('Parameter trigger is not valid in expression: \"Pressure on a gamepad trigger\"');\r", @@ -991,12 +991,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Trigger button", - "name": "trigger", + "name": "Trigger", "supplementaryInformation": "[\"LT\",\"RT\",\"L2\",\"R2\"]", "type": "stringWithSelector" } @@ -1015,8 +1015,8 @@ "inlineCode": [ "const { getNormalizedAxisValue } = gdjs._extensionController;\r", "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const stick = eventsFunctionContext.getArgument(\"Stick\").toUpperCase();\r", "\r", "if (stick !== \"LEFT\" && stick !== \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a stick force\"');\r", @@ -1047,12 +1047,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Stick: \"Left\" or \"Right\"", - "name": "stick", + "name": "Stick", "supplementaryInformation": "[\"Left\",\"Right\"]", "type": "stringWithSelector" } @@ -1076,7 +1076,7 @@ "value": "SetReturnNumber" }, "parameters": [ - "Gamepads::StickAngle(player_ID, stick)" + "Gamepads::StickAngle(PlayerId, Stick)" ] } ] @@ -1088,12 +1088,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Stick: \"Left\" or \"Right\"", - "name": "stick", + "name": "Stick", "supplementaryInformation": "[\"Left\",\"Right\"]", "type": "stringWithSelector" } @@ -1112,8 +1112,8 @@ "inlineCode": [ "const { getNormalizedAxisValue } = gdjs._extensionController;\r", "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const stick = eventsFunctionContext.getArgument(\"Stick\").toUpperCase();\r", "\r", "if (stick !== \"LEFT\" && stick !== \"RIGHT\") {\r", " console.error('Parameter stick is not valid in expression: \"Value of a stick rotation\"');\r", @@ -1143,12 +1143,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Stick: \"Left\" or \"Right\"", - "name": "stick", + "name": "Stick", "supplementaryInformation": "[\"Left\",\"Right\"]", "type": "stringWithSelector" } @@ -1167,7 +1167,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", "\r", @@ -1281,7 +1281,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -1412,8 +1412,8 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", - "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", + "const button = eventsFunctionContext.getArgument(\"Button\").toUpperCase();\r", "\r", "let buttonId = gdjs._extensionController.getButtonId(button);\r", "if (buttonId === null) {\r", @@ -1430,12 +1430,12 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { "description": "Name of the button", - "name": "button", + "name": "Button", "supplementaryInformation": "[\"A\",\"Cross\",\"B\",\"Circle\",\"X\",\"Square\",\"Y\",\"Triangle\",\"LB\",\"L1\",\"RB\",\"R1\",\"LT\",\"L2\",\"RT\",\"R2\",\"Up\",\"Down\",\"Left\",\"Right\",\"Back\",\"Share\",\"Start\",\"Options\",\"Click_Stick_Left\",\"Click_Stick_Right\",\"PS_Button\",\"Click_Touchpad\"]", "type": "stringWithSelector" } @@ -1494,7 +1494,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).lastButtonUsed;" ], "parameterObjects": "", @@ -1508,7 +1508,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -1525,7 +1525,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const player = gdjs._extensionController.getPlayer(playerId)\r", "eventsFunctionContext.returnValue = player.isAnyButtonPressed();\r", "" @@ -1538,7 +1538,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -1555,7 +1555,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();\r", "/** @type {Gamepad} */\r", "const gamepad = gdjs._extensionController.getGamepad(playerId);\r", @@ -1578,7 +1578,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -1633,7 +1633,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const button = eventsFunctionContext.getArgument(\"button\").toUpperCase();\r", "\r", "const buttonId = gdjs._extensionController.getButtonId(button);\r", @@ -1652,7 +1652,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -1680,7 +1680,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).deadzone;" ], "parameterObjects": "", @@ -1694,7 +1694,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -1711,7 +1711,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const newDeadzone = eventsFunctionContext.getArgument(\"deadzone\");\r", "\r", "// https://github.com/4ian/GDevelop-extensions/pull/33#issuecomment-618224857\r", @@ -1726,7 +1726,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -1748,7 +1748,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const stick = eventsFunctionContext.getArgument(\"stick\").toUpperCase();\r", "const direction = eventsFunctionContext.getArgument(\"direction\").toUpperCase();\r", "\r", @@ -1779,7 +1779,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -1840,7 +1840,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", "if (!gamepad) {", @@ -1861,7 +1861,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -1877,7 +1877,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "const controllerType = eventsFunctionContext.getArgument(\"controller_type\").toUpperCase();", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", @@ -1899,7 +1899,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -1921,7 +1921,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "/** @type {Gamepad[]} */", "const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];", "// If gamepad was disconnected it will be null (so this will return false)", @@ -1936,7 +1936,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -1952,7 +1952,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", "/** @type {Gamepad} */", "const gamepad = gdjs._extensionController.getGamepad(playerId);", @@ -1978,7 +1978,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -2000,7 +2000,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "const { clamp } = gdjs.evtTools.common;", - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "const duration = eventsFunctionContext.getArgument(\"Duration\") || 1;", "const strongRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"StrongMagnitude\"), 0, 1);", "const weakRumbleMagnitude = clamp(eventsFunctionContext.getArgument(\"WeakMagnitude\"), 0, 1);", @@ -2033,7 +2033,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -2065,7 +2065,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "const { clamp } = gdjs.evtTools.common;", - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;", "const player = gdjs._extensionController.getPlayer(playerId);", "const elapsedTime = player.rumble.elapsedTime || 0;", "const originalDuration = player.rumble.duration || 1;", @@ -2099,7 +2099,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" }, { @@ -2131,7 +2131,7 @@ "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ "\r", - "const playerId = eventsFunctionContext.getArgument(\"player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "const player = gdjs._extensionController.getPlayer(playerId)\r", "eventsFunctionContext.returnValue = player.isAnyButtonReleased();" ], @@ -2143,7 +2143,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -2159,7 +2159,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).rumble.weakMagnitude;" ], "parameterObjects": "", @@ -2173,7 +2173,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" } ], @@ -2189,7 +2189,7 @@ { "type": "BuiltinCommonInstructions::JsCode", "inlineCode": [ - "const playerId = eventsFunctionContext.getArgument(\"Player_ID\") - 1;\r", + "const playerId = eventsFunctionContext.getArgument(\"PlayerId\") - 1;\r", "eventsFunctionContext.returnValue = gdjs._extensionController.getPlayer(playerId).rumble.strongMagnitude;" ], "parameterObjects": "", @@ -2203,7 +2203,7 @@ "parameters": [ { "description": "The gamepad identifier: 1, 2, 3 or 4", - "name": "Player_ID", + "name": "PlayerId", "type": "expression" } ], From 83d59c32eaa0c92029fd51141862479db47297ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Davy=20H=C3=A9lard?= Date: Fri, 19 Sep 2025 13:14:05 +0200 Subject: [PATCH 14/14] Declare JS usage --- scripts/lib/ExtensionsValidatorExceptions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/lib/ExtensionsValidatorExceptions.js b/scripts/lib/ExtensionsValidatorExceptions.js index 436139d2a..e38847f92 100644 --- a/scripts/lib/ExtensionsValidatorExceptions.js +++ b/scripts/lib/ExtensionsValidatorExceptions.js @@ -261,9 +261,9 @@ const extensionsAllowedProperties = { javaScriptObjectAllowedProperties: [], }, Gamepads: { - gdjsAllowedProperties: ['_extensionController'], + gdjsAllowedProperties: ['_extensionController', 'AsyncTask'], gdjsEvtToolsAllowedProperties: [], - runtimeSceneAllowedProperties: ['getElapsedTime'], + runtimeSceneAllowedProperties: ['getElapsedTime', 'getAsyncTasksManager'], javaScriptObjectAllowedProperties: [], }, Geolocation: {