GMS2 一个xbox手柄被识别为2个手柄

时间:2024-02-21 13:39:09

参照gamemaker studio2的教程Adding Gamepad Support - Code进行学习时遇到了问题。
教程中的代码功能是当识别接入了手柄时,会创建一个player实例。而当我把自己的xbox one s手柄连接到电脑时,游戏中生成了两个player实例。也就是说,一个xbox手柄被识别为了两个手柄。经过debug,被识别出的两个虚拟手柄中有一个不能接收手柄的所有输入,比如十字键的输入。

阅读gms2的解释

When a gamepad is plugged in to the device running the game, it is assigned a "slot" value which is its pad_index. This index value is then used in all further gamepad functions to identify it, and on most platforms pads are indexed from 0, so the first pad connected will be in slot 0, and the second in slot 1 etc... Be aware that on Android you may find that the first gamepad connected is actually placed in slot 1, as the OS reserves slot 0 for general bluetooth connections. On Windows, gamepads which use DirectInput will be detected in slots 0 - 3 (Xbox 360/Xbox One pads, most PC pads) and Xinput devices would be detected in slots 4 - 11 (PS DualShock 4, many newer PC pads).

又经过代码测试,可知接入的xbox手柄被识别为接入了slot 0和slot 4,而slot 0是功能完善的。于是我修改代码,仅识别pad_index在0-3之间的手柄。

// Get the pad index value from the async_load map
	var pad = async_load[? "pad_index"];
	// Set the "deadzone" for the axis
	gamepad_set_axis_deadzone(pad, 0.5);
	// Set the "threshold" for the triggers
	gamepad_set_button_threshold(pad, 0.1);
	// Check to see if an instance is associated with this pad index
	// 仅当手柄索引为0-3时有效
	if !(instance_exists(player[pad])) && pad>=0 && pad<=3
	{
		// Create a player object and assign it a pad number
		var _xx = 64 + random(room_width - 128);
		var _yy = 64 + random(room_height - 128);
		player[pad] = instance_create_layer(_xx, _yy, "instances", obj_Player);
		with (player[pad])
		{
			image_index = instance_number(object_index) - 1;
			pad_num = pad;
		}
	}

之后,对于接入的xbox手柄便不会出现bug。但我想到,dualshock4手柄在slot 4- 11,这样不就不能使用ds4手柄了吗?
于是我借来了同学的ds4手柄进行测试,果然在上面的代码情况下ds4无法被识别。
解决方案是使用神器DS4Windows,它可以将ds4手柄伪装成xbox手柄,这一功能就解决了我们的问题。
参照科技:如何将DualShock 4控制器连接到PC对DS4Windows进行安装使用即可。
DS4Windows还可以让ds4的触控板变成可以操纵鼠标的触摸屏,而且可以用RGB值修改呼吸灯的颜色,实在是太赞了!