• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code gamepad_is_connected() issues in obj creation event

R

RonnieX08

Guest
Hello,

Looking for a little help here.

I am trying to detect and index all plugged in controllers. I would like to run the below code inside a create event, either a room or an object. Unfortunately this does not appear to work. Though, the code below does work fine when I run it inside an objects step event.

To summarize, "gamepad_is_connected(i)" does not return true when executed inside of a obj/room create event. Any ideas?

Additional diagnostic steps taken:
* Encapsulated this loop inside another loop, ran it 30 times to see if after a certain point the GamePad is detected. No luck

Code:
var MaxGamePads = gamepad_get_device_count();
global.GamePadIndex[0] = 0;
var GamePadConnectedIncrement = 0;
for (var i = 0; i < MaxGamePads; i++)
    {
       show_debug_message("Inside loop: " + string(i));
       if (gamepad_is_connected(i)){
           show_debug_message("Game pad description: " + gamepad_get_description(i));
           global.GamePadIndex[GamePadConnectedIncrement] = i;
           GamePadConnectedIncrement++;
       }
    }
Output when executed inside an object or room create event:
Inside loop: 0
Inside loop: 1
Inside loop: 2
Inside loop: 3
Inside loop: 4
Inside loop: 5
Inside loop: 6
Inside loop: 7
Inside loop: 8
Inside loop: 9
Inside loop: 10
Inside loop: 11

Output when executed inside an objects step event:
Inside loop: 0
Inside loop: 1
Inside loop: 2
Inside loop: 3
Inside loop: 4
Game pad description: Wireless Gamepad
Inside loop: 5
Game pad description: Logitech RumblePad 2 USB
Inside loop: 6
Inside loop: 7
Inside loop: 8
Inside loop: 9
Inside loop: 10
Inside loop: 11
 

Greenwitch

Member
From the documentation.

gamepad_is_connected
Note that there may be a slight delay between the user connecting the gamepad and GameMaker: Studio detecting it as being connected (this is especially the case when dealing with bluetooth connected controllers).
..which means at the moment of creation a.k.a create event, GameMaker hasn't been able to detect the gamepad. Anything could cause this, whether it's OS level limitation or it's just GameMaker's nature nobody knows for sure, thus requiring you to keep checking for it every step. Shouldn't be much of a problem if you ask me. What are you trying to do that specifically needs you to check it only on create event?
 
R

RonnieX08

Guest
Thank you for the reply.

There is no reason I cannot have this live inside a step event, it just felt unnecessary to keep checking which gamepads are connected to where so frequently. If doing this check every step is the best method, I can create a counter and check the gamepad_is_connected every so many steps. Though, with that being said, it looks like "System Event" is where I want to be?
This event now permits you to move all your gamepad checking logic from the Step Event or an Alarm event into the System Event and only run it when it's actually required.
Thanks!
 
Top