Changing Text with DnD

Discussion in 'Programming' started by PixelPluto, Sep 1, 2018.

  1. PixelPluto

    PixelPluto Member

    Joined:
    Sep 1, 2018
    Posts:
    15
    Hello there,

    I am new to GameMaker 2 and I'm working with Drag and Drop, and I have a problem with text in my game project.

    I would like to do the following: I click with the mouse (event "Left released") on my text object, and the text changes with every click, like this:

    click 1 = "Hello,"
    click 2 = "my name is PixelPluto,"
    click 3 = "how do you do?"
    etc.

    Is it even possible to do this with Drag and Drop? I looked into every Drag and Drop tutorial, into the forum here, and I tried it with string_replace in the "draw value" action, but so far nothing has worked for me.

    I should also mention that I created a counter for this that increases by 1 with every click, and that I put a transparent sprite into the text object, so that the player has some kind of "button" to click on.

    I hope I managed to explain my problem here as good as possible, I'm not a native English speaker.

    Many thanks in advance for any help.
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,885
    You will need, or rather you will want, to make an array for each text. I'm not sure if you can do the shorthand method in GML, but the long method is

    Set Variable text[0] to "Hello,"
    Set Variable text[1] to "my name is PixelPluto"

    And so on. Or if you don't use alarms, you can use Set Alarm since that's a free 12-index array for ya to use. lol

    When reading text[ ], put your counter variable between the brackets.
     
  3. PixelPluto

    PixelPluto Member

    Joined:
    Sep 1, 2018
    Posts:
    15
    2
    Thank you for your reply. I'm not sure into which event I should set the variables/the array. I've tried the "Draw" event, the "Create" event, and the "Step" event.
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,885
    You SET them in the create event. You'd show the text in the Draw event. I think you have to use the Draw Variable action and the variable would be text[counter], where text[ ] is the array and counter is your counter.
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,538
    So you've been doing Drag and Drop tutorials, yet you haven't learned what the Create, Step and Draw events do? That is completely unacceptable, yet also too commonplace for its own good.

    Here is an overview of the 6 fundamental events in GMS 2, what kinds of code typically go in each and what mistakes rookies typically make with them:
    • Create event: This runs once when an instance of the object is created, either via the room editor, the "Create Instance" action, or instance_create_*() functions. Setup code goes here, such as instance variable declarations and allocations for an instance's dynamic resources (e.g. data structures, surfaces, buffers, etc.). Notice that if you use a with statement to apply actions or code to a created instance, that code is run AFTER the Create event. If you want the code to run before, see Yal's instance_change() constructor pattern.
    • Step event: This repeats once per step for as long as the instance of the object is active. Code that runs on an ongoing basis belong here, such as continuous condition checks, movement, keyboard/mouse checks, etc.
    • Draw event: This repeats at the end of each step, once for each view active (including the default view if you didn't specify additional cameras). Code for non-default drawing behaviour belongs here, but it must be code that is tied to positions within the room and OK to be shifted/scaled by camera positioning, NOT tied to static positions on display (e.g. HUDs are an example of something that typically does NOT belong in this event). The main drawing surface clears before this event, so DO NOT think about erasing old drawn content from earlier steps. Note that if any content is placed here, the default sprite will disappear. It can be brought back with the "Draw self" action or draw_self().
    • Draw GUI event: This repeats at the end of each step, but only once and after all instances have finished their Draw events. Any drawing code run during this event will cover graphics drawn during the Draw event (this includes automatically drawn sprites), and stay at the same place on the screen regardless of camera positions. HUDs are an example of drawn content that usually belongs in this event. Again, DO NOT think about erasing old content from earlier steps. Typically, code containing terms of instance positions (e.g. x, y, phy_position_x, phy_position_y) or camera coordinates (e.g. camera_get_view_x(), camera_get_view_y()) is INADMISSIBLE in this event unless there is additional code converting that position to a strictly on-screen coordinate. Also, draw_self() should NOT be used in this event, and there is no need to because including content in this event does NOT remove the default sprite.
    • Destroy event: This runs once when an instance of the object is explicitly removed using instance_destroy() or the "Destroy Instance" action. Code for special effects generated upon explicit destruction belong here. Deallocations for dynamic resources always belong in the Cleanup event, NEVER this event.
    • Cleanup event: This runs once when an instance of the object is removed for any reason, including instance_destroy(), the "Destroy Instance" action or when leaving a room while persistence is off. Code for deallocating an instance's dynamic resources belong here.
    Given the descriptions above, it is clear that your code can't all be in one place, and pieces of it belong in the following events:
    • The group of "Set text[...] to "..."" actions and the declaration of the counter variable belong in the Create event. That's where instance variable declarations belong.
    • The click-checking condition and its associated counter increment belong in the Step event. That's where code repeated on an ongoing basis belong.
    • The part drawing text[counter] belongs in the Draw event. That's where custom camera-dependent drawing code belongs. Forget about the string_replace() nonsense you thought of earlier on because there is no need to erase old content manually (and even then that's NOT what the function does, look it up in the Manual).
    Nobody ever gets anywhere with GMS 2 without a thorough understanding of at least the 6 events above, optionally plus the alarm and collision events. It's high time for mainstream GM education to make event ignorance wrong again.
     
    Bentley likes this.
  6. PixelPluto

    PixelPluto Member

    Joined:
    Sep 1, 2018
    Posts:
    15
    Oh my, thanks again for all the help! I knew that my problem was too "commonplace", and I'm really sorry to bother the commmunity with it, but I was just so stuck.

    I managed to change the text by rearranging my "Step" event for the object, and began using "Draw GUI" to display the text, and now it works :) .
     

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