1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 How to Assign a Mouse Click or Touch to a Variable [SOLVED]

Discussion in 'Programming' started by KyleRansford, Jul 26, 2019.

  1. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    81
    Hello, I'll try to explain what I mean, but I have a Input Object (o_input) that has all the controls for the character like:
    Code:
    action_one_pressed_   = gamepad_button_check_pressed(xbox_controller, gp_face1) or keyboard_check_pressed(ord("D"));
        action_two_pressed_   = gamepad_button_check_pressed(xbox_controller, gp_face3) or keyboard_check_pressed(ord("A"));
        action_three_pressed_ = gamepad_button_check_pressed(xbox_controller, gp_face4) or keyboard_check_pressed(ord("W"));
        action_four_pressed_  = gamepad_button_check_pressed(xbox_controller, gp_face2) or keyboard_check_pressed(ord("S"));
       
        action_one_   = gamepad_button_check(xbox_controller, gp_face3) or keyboard_check_pressed(ord("D"));
        action_two_   = gamepad_button_check(xbox_controller, gp_face2) or keyboard_check_pressed(ord("A"));
        action_three_ = gamepad_button_check(xbox_controller, gp_face4) or keyboard_check_pressed(ord("W"));
        action_four_  = gamepad_button_check(xbox_controller, gp_face1) or keyboard_check_pressed(ord("S"));
    
    xbox_controller = 0;
       
        right_ = keyboard_check(vk_right) or gamepad_axis_value(xbox_controller, gp_axislh) > 0.50 or gamepad_button_value(xbox_controller, gp_padr);
        left_  = keyboard_check(vk_left)  or gamepad_axis_value(xbox_controller, gp_axislh) < -0.50 or gamepad_button_value(xbox_controller, gp_padl);
        up_    = keyboard_check(vk_up)    or gamepad_axis_value(xbox_controller, gp_axislv) < -0.40 or gamepad_button_value(xbox_controller, gp_padu);
        down_  = keyboard_check(vk_down)  or gamepad_axis_value(xbox_controller, gp_axislv) > 0.40 or gamepad_button_value(xbox_controller, gp_padd);
    
    Where in the part
    "action_one_pressed = gamepad_button_check(xbox_controller, gp_face3) or keyboard_check_pressed(ord("D"));"

    action_one_pressed is a Button or D Key press.

    While looking at the other functions I can't find anything related to, for example, "action_one_pressed = tap on o_A_Button".

    o_A_Button is an Object with a Tap event. So, instead of having to reinvent the wheel, can I assign the Object's Tap event to the action_one_pressed variable?

    Asking because with the action_one_pressed variable I use it for weapons and choices, etc. like:
    Code:
    if o_input.action_one_pressed_ {
        audio_play_sound(a_laser, 1, false)
        event_user(3);
    }
    event_user(3) being an event to fire a bullet every time the player presses the A Button or the D Key. So instead of having to have the o_A_Button have a Tap event. Can I add the Tapping on the o_A_Button as an additional input in "code/variable" form?
     
    Last edited: Jul 26, 2019
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,025
    Here's a little something I learned from reading NES code, which may help you with this.

    You will need a minimum of 2 variables. I typically call these io_hold and io_prev. io_hold tracks all the inputs currently active, with each input occupying one bit. If you don't feel comfortable with bits, you can still do the whole thing with multiple variables, but it would require 2 variables per input, which can be quite a lot. To keep things kind of simple, I'll just presume you'll use the less efficient method and have 2 variables per input.

    So for this explanation, I'll just go along with what you have so far. Let's work with action_one_. You'll then need another variable called action_one_pre. In the Begin Step Event, set action_one_pre to action_one_ and then set action_one_ as you normally would.
    Code:
    action_one_pre = action_one_;
    action_one_ = gamepad_button_check(xbox_controller, gp_face3) or keyboard_check(ord("D"));
    Notice it's just the simple checks, not a pressed check.

    Now open up o_A_Button and go to its Tap event. In that event, you'll OR o_input's action_one_ with 1.
    Code:
    o_input.action_one_ |= 1;
    Since this only uses the Tap event to set action_one_, it will be treating Tap and Drag as the same thing, which shouldn't be an issue, but I felt I should just mention that.

    The purpose of the action_one_pre variable is so we can distinguish between a tap/press, a hole/drag, and a release. If you combine action_one_ and actione_one_pre, then exclude one of them, you'll have either the pressed or the released status.
    Code:
    var temp = action_one_ | action_one_pre;
    var pressed = temp ^ action_one_pre;
    var released = temp ^ action_one_;
    To put the code in layman's terms: We get the pressed value by looking at what was held in the previous step and what is currently held now; then if we ignore all the inputs that were held in the previous step and only look at the inputs which are held now, clearly the inputs held now that were not held previously were pressed on this step. Conversely, we get the released value by looking at what was held previously and what is currently held, ignore what is currently held and see what was held last time that isn't held now, and that clearly is the input that was released on this step.

    There's probably a way to do it using event_map, but this would be simpler, I think.
     
    Toque likes this.
  3. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    81
    Thank you for the reply!

    As I as was reading through the code, I saw this part "o_input.action_one_ |= 1;" and I just put only that code in the o_A_Button and everything is working just like I wanted it to without using the other variables and code.

    For clarification, what is the "|= 1" for? I know " | " means OR, but what is the 1 for?
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,025
    "true"

    Events don't return values like functions do, so you need to OR action_one with something.

    If you used multiple bits instead of multiple variables, you would use 1, 2, 4, 8, 16, or whatever power of 2 you needed. But since you are using multiple variables, you just use "1".
     
    Last edited: Jul 28, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice