Another quick question,
Is there anything that can pause a function for a few seconds (like WaitForSeconds() in Unity’s c#).
I’ve tried putting sleep() inside a function and it didn’t do anything…
Another quick question,
Is there anything that can pause a function for a few seconds (like WaitForSeconds() in Unity’s c#).
I’ve tried putting sleep() inside a function and it didn’t do anything…
Well, some user need to achieve a similar effect, so someone told him to try this:
In the Tick() function add a float to count it down for each frame, i think it was float. Why don’t you try this way?
Hmm that would also require something that produce coroutine effect like “yield”. In addition, I don’t want to return from the functions.
I know there’s Delay in Blueprint, but I couldn’t find it in UT4’s c++.
Anyone has any other clues for us?
ps. Really nice Bruce Lee qoute. rofl
If you don’t return from the function, you basically freeze the entire game. Probably not what you want to do, right? For some things there is no 1:1 conversion between BP and C++.
You have to split the function in several parts.
Inside the Tick Method you will have a counter, that counts down and then calls the parts of the function one after another.
How does the blueprint delay work?
I guess the same as I wrote: It waits an amount of seconds till the next Node is triggered. That has to be done inside such a Tick method.
Since Delay is an own Node and function, it is easy to implement.
This might be what you’re looking for
GetWorldTimerManager().SetTimer(this, &MyClass::MyFunc, 3.f, false)
Will call MyClass::MyFunc once, in 3 seconds. Although its not quite exactly WaitForSeconds() in C#, it should be good enough?
@ - I was actually hoping to see something like that. I can’t imagine wanting the whole game to stop for three seconds…
Well, is this possible?
Your also able to allow the timer to run that functions every x seconds by making the final argument true. It may be worth you examining the timer functions here.
Sigh… sorry for digging this up but SetTimer() is such a nuisance since it can’t pass any variables along with it.
Can I kinda request that we have something similar to Blueprint’s delay node in c++?
Otherwise, this is way harder than it should be.
Hey KillerPenguin!
If it helps there’s an alternative method.
You could create your own UObject that gets used like an if statement.
So you could do something like:
bool CustomClass::CustomTimer(float & Time)
{
if(Time > 0)
{
Time -= 0.01f; //Run down in seconds.
return false;
}
else
{
Time = 0;
return true;
}
}
Then because you are using an outer parameter, you can quickly set up a UPROPERTY float.
And you’ll still be able to handle your function with normal parameters like so:
if(CustomTimer(TimeIWantToUse))
{
MyFunction(MyParam1, MyParam2, etc);
}
I haven’t tested the above, since it’s just off the top of my head, but you shouldn’t have any issues.
I know it’s not the most elegant of solutions, but at least it gets the job down.
You could objectify it, so you don’t need to hold a float property, and make it so the object self deletes at the end.
The other option is to place the timer function in a custom static library.
Your final alternative is to look into Threads, which is covered in detail by Rama **HERE
**Considering Coroutines where developed to be Memory-safe alternatives to threads,
it should have most of the functionality you are after! :3
Hope this helps!
-
I’m no cpp expert, but you should be able to pass a ref to lambda in settimer, which in turn calls your function with params.
Sorry I forgot to mention that this is now just a random rant. I actually came up with my own method of calling timers with variables.
It’s just plain annoying having to break everything into many different functions though.
If you’re used to c#, you could just keep everything in one function. Now I have to break them into like 10-20 for any delicate actions.
It’s just very hard to keep track of. I’m sure many people would rather spend this time doing something else with their games.
I also put this up on Epic feedback in case anyone want to help bumping it…-> Coroutine function (Delay node) for c++ - Feedback for Unreal Engine team - Unreal Engine Forums
The yield keyword is part of the C# language and there is nothing like it in C++. I don’t see how complaining will change this.
SetTimer() does allow parameters to be passed through using delegates. You can refer to this AnswerHub ticket Using SetTimer() on a Function with Parameters - Character & Animation - Epic Developer Community Forums