An Important Disclaimer
Normally, I wouldn’t put something like this at the top of a post - let alone before the actual focus of the post itself - but in this case, I have decided to make an exception due to the importance of this information.
PlatformProvider should [color=#ff5f5f]NEVER[/color] be used to perform platform-conditional actions that can alter how a user interacts with the game. Actions performed in the game should always match the context of whatever it is they’re doing and how they are doing it (e.g. what input device they are using should define how the game is controlled, not this module).
Misusing this module may severely damage your game’s reception and cause a whole slew of problems that could have been trivially avoided simply by doing things the right way. You have been warned.
PlatformProvider - What is it?
PlatformProvider is a simple module that will use various context clues to attempt to discern the overall platform type a user might be running. It was designed with edge cases in mind (e.g. if someone plugs in a keyboard and mouse into their Android device) and should ideally provide reasonable output reflective of the user’s current platform. Right now, it can return three values representing computers, xbox, and all mobile devices.
This module was made in light of the great number of requests for a method of acquiring the user’s platform. The use cases are vast, and so long as the precautions mentioned in the start of this thread are taken, this module should remedy most of the desires you all might have.
You can get the module here.
How do I use it?
The API is incredibly simple. Here’s an example script:
local PlatformProvider = require(path.to.PlatformProvider)
local CurrentPlatform = PlatformProvider.GetPlatform()
function DoSomethingWithThisInformation()
if CurrentPlatform == PlatformProvider.Platforms.Mobile then
-- Whatever it may be.
end
end
The method this module uses to detect looks at the available hardware and uses a points system to gauge the best fit. Gyroscope and accelerometer both add 2 points to the mobile result, and keyboard, mouse, and gamepad add 1 point to the computer result.
If both end up being equal, there is a boolean (true/false) property of the module PlatformProvider.GuessFromTouch
which will use the presence of a touchscreen as a last-resort means of discerning between mobile and computer. Its default value is true
. Of course, a computer can be touch screen, so that is why this method is used as a last resort.
Of course, if you figure out anything that should be tweaked or run into bugs, just let me know and I’ll do what I can to fix it. Enjoy!