GM:S 1.4 Pause my character while text is active?

Discussion in 'Programming' started by Frisk2401, Jul 11, 2019.

  1. Frisk2401

    Frisk2401 Member

    Joined:
    Jul 11, 2019
    Posts:
    39
    I've been trying to get my text system to pause my character while active, and then when you type a character it'll close it and you can move again, I have the main base down for the code but I need help with those two parts, can anybody help? Much appreciated.
     
  2. pipebkOT

    pipebkOT Member

    Joined:
    Dec 13, 2018
    Posts:
    210
    @Frisk2401
    Just create a variable that controls if the player can move or not

    Create event
    Global.canmove=true

    Step event or keyboards events
    If global.canmove=true
    {
    All the player movement code



    }



    And when the text box appears set global.canmove=false so the player movement code is not executed

    And when the text box is closed make the variable true again
     
    FacesOfMu likes this.
  3. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    I agree with the above point except there is no need for it to be a global variable, as you don't want to pollute the global context with a variable that only interests the player instance. It should instead be an instance variable.
     
  4. Frisk2401

    Frisk2401 Member

    Joined:
    Jul 11, 2019
    Posts:
    39
    Okay, thanks! ^-^
     
  5. Frisk2401

    Frisk2401 Member

    Joined:
    Jul 11, 2019
    Posts:
    39
    @pipektOT
    I'm still having problems with this.. Here is my code.

    Character

    Create Event:
    friction=0.9
    global.canmove=true

    Step Event:
    image_speed = speed/40
    //this one works, but could be slower.

    if speed=0 image_index = 0

    Left Key Pressed:
    global.canmove=true
    if global.canmove=true
    {
    direction=180
    sprite_index=spr_chara_left
    speed=5
    if(keyboard_check_pressed(vk_left)){
    x=x-3
    }
    }

    Text Box

    Create Event:
    ///Some Variables
    message[0] = "A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A "

    message_current = 0 //0 is the first number in our list of messages
    message_end = 0 //how many messages you want
    message_draw = ""; //this is what we 'write' out.
    increase = 0.5 //the speed at which new characters are added
    characters = 0 //how many characters have been drawn
    hold = 0 //if we hold 'Z', the text will render faster

    message_length = string_length(message[message_current]) //get the number of characters

    if obj_text1.visible = true{
    global.canmove=false
    }

    Step Event:
    if(place_meeting(x,y,Chara)){
    if (characters < message_length) {
    hold = keyboard_check(ord('z'))
    characters += increase * (1 + hold)
    message_draw = string_copy(message[message_current], 0, characters)
    }
    else{
    if(keyboard_check_pressed(ord('z'))){
    if(message_current < message_end){
    message_current += 1
    message_length = string_length(message[message_current])
    characters = 0
    message_draw = "";
    }
    }
    }
    }

    Draw Event:
    ///Draws the sentence
    draw_set_color(c_white)
    draw_set_font(fnt)

    if(place_meeting(x,y,Chara)){
    global.speed = 0
    draw_set_color(c_black);
    draw_rectangle(32,512,992,736,0);
    draw_set_color(c_white);
    draw_rectangle(37,516,988,732,0);
    draw_set_color(c_black);
    draw_rectangle(43,522,982,726,0);
    draw_set_color(c_white);
    draw_text_ext(54,528,message_draw, 60,950)
    }
     
  6. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    I'm assuming your problem is that you can still move around, is that correct? You should always specify what is your exact issue along posting your code, in this case, the problem can be noticed at a glance but it's not always that easy.
    Anyway, the issue here is that, on keypress, you set global.canmove to true before checking if the character can move, which means they can always move. The way it should work is that the textbox disables movement when it starts (and it seems you do this in the create event) and it should then re-enable it when it stops... which is probably in the destroy event if the textbox destroys itself once it has shown all its messages.

    EDIT:
    Also, remember to use code tags to make your code more legible, thanks!
     
  7. Frisk2401

    Frisk2401 Member

    Joined:
    Jul 11, 2019
    Posts:
    39
    Yes, I can still move around. Sorry, I should've been more specific. Oh my god! Thank you so much! I've been trying to do this for months! But how can I make it so it closes and opens with a key press? (The textbox.) It opens when you walk on it.. oh! and I can still move diagonally when in a text box, apart from that it's great. (EDIT) I fixed the diagonal moving bug.

    I would like to know how to make the text box appear when interacting and pressing z, and disappear after pressing z again.
     
    Last edited: Jul 12, 2019
  8. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    Just as a general overview in regards to the global.canmove thing, it's really important to remember to step through your code in your own head. Each code block will execute in order, so all the 'ingredients' are there, it's just up to you to visualise the 'recipe'. As you are writing code (or reviewing it when bug fixing), remember to think about what each step implies for the rest of the execution. If you are setting something to true, it will be true for any checks later in the code block. Remembering to do this and paying close attention to it can solve 90% of bugs. I often spend an afternoon just sitting and thinking about a few code blocks before coming to the realisation of where a bug lies, which I think is a step that a lot of people skip. Spend some time really deeply going over your code again and again and asking yourself what each step is doing.

    In regards to your question about making the textbox open and close on a keypress, I assume you're following a tutorial/script provided by someone else? The keypress is simple, just use GMS' inbuilt Key Pressed events to create a new instance of the Text Box object. However, you might have to provide some information to the instance when you create it, in which case the code would look like this (please note, this is pseudocode, it won't necessarily work out of the box, you'll have to adapt it to your specific situation):
    Code:
    var inst = instance_create_layer(x,y,"Layer",obj_text_box); // Here we create our textbox instance in the layer named "Layer" and store it's instance ID in the variable inst which we will use to give it some info
    with (inst) { // Here we are using the previously stored instance ID (the ID of the textbox) to tell the textbox what we want it to show
       message[0] = "Hello, how are you.";
       message[1] = "You're looking good today!";
       message_end = 1;
       /* Overall, this code block is giving the textbox two messages to display. From the code you've provided it seems like messages are stored in an array and the final message position is also stored, so I've added two messages (they probably get triggered one after the other) with the textbox possibly being destroyed after the message_current variable exceeds the message_end variable. */
    }
    
     
  9. Frisk2401

    Frisk2401 Member

    Joined:
    Jul 11, 2019
    Posts:
    39
    Before I got help, I was doing the same thing, but I didn't make it a correct variable. And yes, I used pre-written code as I enjoy it being a typewriter style. I'm new to gamemaker, but I hope I'll learn.
     
    RefresherTowel likes this.
  10. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    Keep at it and it'll all make sense eventually =D
     
  11. Frisk2401

    Frisk2401 Member

    Joined:
    Jul 11, 2019
    Posts:
    39
    Alright, thank you. ^-^
     

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