Script Displaying All Text Instantaneously, Delay Not Working

GarbageHaus

Member
I have a script that seems to do everything I want but when testing it displays everything all at once rather than one at a time. See the photo of the problem:
trashtalk.jpg

The goal is to have the text appear, slowly fade in and rise up at the same time. (Later I'll add a "fade out"). I left in a few of my comments so you can see my thought process. All methods attempted basically did the same thing.

GML:
///Param text What you want to enter
///Param length how long text should last
///Param height how high the text should float up
///Param color of text.
function Risingtext(text, length, height, color){
    alf = 0 //alpha value start.
    rise = self.y + 0 //for rising
    //prevtime = (delta_time / 1000000)
    //if prevtime < (delta_time / 1000000) + 3
    for (var i = 0; i < length; i++) {
        alf = (i / length) //Alpha becomes more opaque as the percentage reaches 1.
        draw_text_color(self.x,rise,text,color,color,c_aqua,c_blue,alf)
        if (self.y - height) != self.y {
            rise = rise - 1 //If the distance left to travel is NOT the current position then continue rising.
        }
    }
}
Ignoring the fact that the colors and position are wrong, I can fix that later. The entirety of the loop is processed instantaneously regardless of how long my "length" is. The function is being called within a Draw Event via:
GML:
if keyboard_check_pressed(global.CON_ATTACK1) {
    Risingtext("I'm just trash! Please don't punch!",22,13,c_red)
}
I've changed the loop types "do" "while" "repeat" but all behave the same way. I tried using an Alarm but the help guide's description of how that works is... lacking.

How am I supposed to delay this loop so that one frame = one frame?
 

GarbageHaus

Member
So, I read your post but I'm still a bit lost since I need to create a loop otherwise it will only iterate through 1 instance of the function, which in this case means for 1 frame the invisible text appears and then disappears since it can't loop. I thought the only way to get it to loop properly was to call the function again but this seems to soft-lock the game until it crashes.

I tried adding a "While" to iterate though the function but this did nothing that my initial code didn't already do.

GML:
function Risingtext(text, length, height, color){
    alf = 0 //alpha value start.
    rise = self.y + 0 //for rising
    loop = 0
    while (loop < length) {
        loop = loop + 1
        alf = (loop / length)
        draw_text_color(self.x,rise,text,color,color,c_aqua,c_blue,alf)
        if (self.y - height) != self.y {
            rise = rise - 1
        }
    }
}
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Anything in an event or a function is iterated over in a single game step, so using a for loop all you are doing is iterating over something XX number of times in a single step. If you need to iterate over something every step, then you need to use a counter variable or an alarm to control it. For example, if you need to iterate over something 30 times, you need to set the counter variable to 0 and then each step check it, if it's under 30 perform an action, then add to it. Once it hits 30 that action will no longer be performed. EG:

GML:
/// CREATE EVENT
counter = 0;

/// STEP (or draw) EVENT
if counter < 30
{
// perform action here
counter += 1;
}
Did you read the tech blog I linked to? I would suggest you create a new test project and follow it through as it'll give you a better understanding of what's needed...
 

GarbageHaus

Member
Thank you. I was really hoping the entire thing could be encapsulated within just the Draw Event. That was the whole point in making a script do this instead.

Didn't realize the function() occurred all within a single step. That was the information I was missing.

Thanks again.
 
Top