(SOLVED)Drawing text to an object

Adjud

Member
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...
 

Rob

Member
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:

Adjud

Member
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!
 

Rob

Member
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.
 
Top