(SOLVED)Drawing text to an object

Discussion in 'Programming' started by Adjud, Jan 13, 2020 at 6:20 PM.

  1. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    59
    Hi, I want to create a random x/y range for text displayed from an object. specifically damage dealt displayed randomly over the enemies head. my current code:
    Code:
    if atime=1 { 
     draw_text(objslime.x -20,objslime.y -40 ,string(damage))
    }
    
    if miss=1 {
     draw_text_color(objslime.x -20,objslime.y -40 , "Miss",c_red,c_red,c_red,c_red,1)
    }
    what I want to achieve is something like:

    Code:
    if atime=1 { 
     draw_text(objslime.x + random(-40) to random(40) for x ,,objslime.y + random(-60) to random(-40) ,string(damage))
    }
    
    I know the above code is not a real code, its the idea, I'm trying to get the damage to display randomly above the enemies head but within a range of x -40 to +40 and y -40 to -60 if that makes sense?
    if I need an array or something more advanced to create this effect, please refer me to documentation on how to achieve my goal.

    thank you and sorry for the newbie questions...
     
  2. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    741
    irandom_range or random_range is what you're looking for

    Code:
    draw_text(objslime.x + irandom_range(-40, 40), objslime.y + irandom_range(-40, 60) ,string(damage))
    This would make the coordinates change every step. If you want it to be random but also stay in the same place, use 2 variables to store the random results and make sure you only set the variables once.

    E.g.

    Code:
    //Run ONE time per hit
    string_x = irandom_range(-40, 40);
    string_y = irandom_range(-40, 60);
    
    //draw event - this runs every step
    draw_text(objslime.x + string_x , objslime.y + string_y ,string(damage))
    
     
    Last edited: Jan 13, 2020 at 6:37 PM
  3. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    59
    so I tried what you said, it works but as you stated above the random x/y constantly moves im not clear on how to set a single non changing variable here is what I came up with that does not work:
    Code:
    if atime=1 {
     rangex = objslime.x + random_range(-40, 40)
     rangey = objslime.y + random_range(-40, -60)
     draw_text(rangex,rangey, string(damage))
    }
    so rangex and rangey keep randomly changing for the duration of the texts life..

    EDIT: I got it, I just had to remove the variable assigning code from DRAW event and change it to the pressed left mouse event, so when I click on the monster it sets
    Code:
    rangex = objslime.x + random_range(-40, 40)
     rangey = objslime.y + random_range(-40, -60)
    then the draw event draws from that range.

    Thank you!
     
  4. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    741
    There are plenty of different ways to do it and the "best/easiest" way would probably depend upon your code. How are you making your attacks at the moment? If there were some boolean that were true/false depending on whether the player just hit a slime or not, then you could set the variables there.
     
  5. Adjud

    Adjud Member

    Joined:
    Sep 11, 2017
    Posts:
    59
    got it, thank you
     
    Rob likes this.
  6. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    741
    Mark as [SOVLED] in title pls ;)
     

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