Detect and write platform specific code!

Yea yea yea… “omg you should write universal code not platform specific1!1!1!!” Well, you know what? I have quite a few use cases for platform specific code! So, I decided to spend 3 hours hacking around the ROBLOX API, testing on 4 different devices, and creating an API which will tell you precisely what platform ROBLOX is running on, and what inputs are supported at the moment. You can view it in the GitHub below

https://github.com/Reinitialized/Project-Mora/blob/master/Source/MoraCore.lua#L44

This code has been tested on Desktop, Tablet, Phone, and XBOX One! Each and every device reported the correct platform, and it will even update the inputs which users will most likely add/remove! (Why would you remove a touch screen mid game? Hell if I know) Anywho, the code is rather straight forward.

local Platform, Inputs = Device.GetPlatform()

if (Platform == "Computer") then
  if (Inputs.Touch) then
    -- give user choice to use touch interface or keyboard/mouse
  else
    -- give user keyboard/mouse interface
  end
elseif (Platform == "XboxOne") then
  -- give user controller friendly interface
elseif (Inputs.Touch) then
  if (Platform == "Tablet") then
    -- give user tablet optimized touch interface
  elseif (Platform == "Phone") then
    -- give user phone optimized touch interface
  end
end

This code does not account for screen size! You’ll still need to adjust your User Interface according to resolution. However, this should allow you to have a ballpark interface for each platform. Of course, there are many variant phone/tablet resolutions, and lets not forget the different monitor resolutions (like my 2560 by 1080 ultrawide :smile:)

Hope its useful

EDIT:
I should also mention that I did this thanks to UserInputService:GetPlatform() being locked. I would much rather have the ROBLOX Engine tell me what I’m running, cause it should know what it’s running how. However…

3 Likes

Yeah, except in my case, not so much for running platform-specific code, but for analytic purposes.

Also: _G.Device = Device
Ew. Use ModuleScripts nob

1 Like

Some of the logic is pretty weird to me:

if (UserInputService.TouchEnabled) then
          -- If we support a touch screen, it's safe to assume we're a mobile device. But are we a tablet? Or a phone? Hmm...

          local ViewportSize = Workspace.CurrentCamera.ViewportSize

          if (ViewportSize.X < 700) then
            PlatformType = "Phone"
          elseif (ViewportSize.X >= 700) then
            PlatformType = "Tablet"
          end

These kind of statements seem very arbitrary and not future-proof

I don’t really think you need to know the platform, you just need to know those other properties that UserInputService already exposes. (TouchEnabled, VREnabled, GamepadEnabled, etc)

Well the iPhone 6+ is 750 x 1334 pixels, so I would assume this script thinks it is a tablet. But if this changed to be 1335 as the change between phone and tablet, the iPad mini 1 is 1024 x 768 pixels, so no number would be perfect in theory.

Yeah but do you really need to know whether a device is a tablet or a phone like that? Probably you would rather do something with the size instead. (i.e. you have a “small”, “medium” and “large” version for your UI which changes as the screen size changes)

2 Likes

Tablet interfaces, I find, have more gesture controls than on phones. IE Swipe with 3 fingers to change app on tablet, but cannot on phone because it is so small. So user expectations are changed between platforms. I admit that it is an edge case and wouldn’t personally use it, but someone might.

@EchoReaper I was testing in ScriptBuilder! Leave me alone qq

As for the phone/tablet detection, I was going off Android standards verses iOS. I made sure it worked with quad HD screens thanks to my Nexus 6P, but forgot about iOS a bit (kinda hate it). I think there’s a way to determine if you’re using Android or iOS, so I’ll implement screen checks based off device OS

That makes it even ewier. Test in Studio instead of those archaic things nob.