Jump to content

SpellManager.CastSpellByNameOn - Async ? Await ?


Evaexe

Recommended Posts

Hello Wrobot Community,

I am currently working on a script for WoW 3.3.5a private server, and I need some assistance in making my function asynchronous to ensure it is executed properly. Here's the code snippet I am using:

 
public static bool WildGrowthHeal(WowPlayerExtended player)
{
    if (CanCast(Main.WildGrowth, player.OriginalPlayer))
    {
        SpellManager.CastSpellByNameOn("Wild Growth", player.OriginalPlayer.Name);
        Logging.WriteDebug($"{player.OriginalPlayer.Name}Wild Growth");
        return true;
    }
    return false;
}

&&

internal static bool CanCast(Spell spell, WoWUnit target, bool canMove = true, bool tank = false)
		{
			// Check if the caster is not stunned, not dead, not casting, the target is alive, the spell is usable,
			// the distance to the target is good, and there is no line of sight issue
			if (!ObjectManager.Me.IsStunned && !ObjectManager.Me.IsDead && !ObjectManager.Me.IsCast && !target.IsDead && spell.IsSpellUsable && spell.IsDistanceGood && !TraceLine.TraceLineGo(target.Position))
			{
				if (target.MaxHealth - target.Health != 0 || tank)
				{
					// Log the spell cast and the target's health percentage
					Logging.WriteDebug($"Cast: {spell.NameInGame} on {target.Name} because life had less than {target.MaxHealth - target.Health} PV and he is a {target.WowClass}");

					// Return true, indicating the spell can be cast
					return true;
				}
			}

			// Return false, indicating the spell cannot be cast
			return false;
		}

 

This function checks if "Wild Growth" can be cast on a player and casts the spell if possible. However, I want to make sure that the function waits for the spell to be cast before proceeding further. I am aware that using Thread.Sleep is not the best solution, as it can block the thread and cause other issues.

Instead, I would like to make this function asynchronous using async and await, but I'm not sure how to do this with the current SpellManager.CastSpellByNameOn method, which is not asynchronous.

Could anyone please provide guidance on how to make this function asynchronous or suggest an alternative way to ensure that the spell is cast before the function proceeds further? Any help would be greatly appreciated

 

Thank you in advance for your assistance!

Link to comment
Share on other sites

Hello,

You can use "Task.Run" to call "SpellManager.CastSpellByNameOn" (and others synchronous methods) and make your asynchronous async.

But, CastSpellByNameOn cast the spell and return (it doesn't wait that the spell was cast, for that, you should use "Usefuls.WaitIsCasting()")

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...