(JavaScript) Here are two sections of code in one script attached to a cube (the ‘player’):
public function ReleaseGun()
{
print("Started release");
weaponry.WieldedWeapon.CurrentlyReleasing = true;
yield WaitForSeconds(weaponry.WieldedWeapon.ReleaseTime);
if (weaponry.WieldedWeapon.CurrentlyReleasing == true)
{
weaponry.WieldedWeapon.currentFiresLeft = weaponry.WieldedWeapon.ReleaseCapacity;
weaponry.WieldedWeapon.CurrentlyReleasing = false;
print("Release successful");
}
}
if (Input.GetKeyDown("r") && weaponry.WieldedWeapon.CurrentlyReleasing == false)
{
Invoke("ReleaseGun",0);
}
The if is in Update() (sorry for the sloppy code example, I don’t want to try to get the code format to work with two separate pieces…it hates me).
I tried Invoke with ReleaseGun just printing something. It worked and printed immediately after hitting R. I tried Invoke with everything but the “yield” in ReleaseGun. It worked, but instantly instead of WaitForSeconds working. When I add yield before WaitForSeconds, nothing happens at all. The first print isn’t printed and no variables are set.
By the way, weaponry is a variable based off of a class called Weaponry. WieldedWeapon is also set to a class called Weapon, which has various weapon-related stats, including ReleaseTime, a float. The ReleaseTime of the specific weapon I was using is 3, and I printed that to verify it.
When I just regularly call the function via “ReleaseGun()”, it works perfectly, but I want to invoke it so I can CancelInvoke in the middle if you change your weapon (this is like a reload system in an MOFPS).
Why does the ‘yield’ seem to destroy the function…?
I don't think Invoke is implemented as a coroutine.
– Eric5h5Thanks! I did what you said and it's working, and I made the weapon changing function cancel the invoke and that works fine, too. Also, thanks for fixing up the code blocks up there...I can never seem to get them right.
– SirMacJeffersonThis format button sometimes refuses to work - I use the following tags: (always let a blank line before the first tag) <pre> (code goes here) </pre> It has always worked fine for me in questions, answers and comments (much different from that damned format button...)
– aldonaletto