I am having an issue where I have a bunch of objects each with a script attached, which creates a tween inside of an async task. It seems each object refers back to this same task as I get errors about being unable to create more tweens since the first one was already made. Any help would be greatly appreciated!

Error:
ERROR: Tween can't be created directly. Use create_tween() method.

Script in question which is attached to a bunch of shard objects as part of a destruction plugin:

private async Task Initialize()
	{
		await Task.Run(async() =>
		{
			if (true == false) //(shrinkDelay == -1f && fadeDelay == -1f)
			{
				Thread.Sleep(2000);
			}
			else
			{
				GD.Print(fadeDelay);
				await ToSignal(GetTree(), "physics_frame");
				await ToSignal(GetTree(), "physics_frame");

				MeshInstance3D  _meshInstance = GetNode<MeshInstance3D>("MeshInstance");
				Material material = _meshInstance.Mesh.SurfaceGetMaterial(0);
				if (material == null)
				{
					return;
				}

				material = material.Duplicate() as Material;
				_meshInstance.MaterialOverride = material;
				material.Call("set_flag", "flags_transparent", true);

				Tween tween = new Tween();
				ApplyImpulse(RandomDirection() * explosionPower, -Position.Normalized());

				if (fadeDelay > 0)
				{
					tween.TweenProperty(material, "albedo_color", new Color(1, 1, 1, 0), 2).
						SetDelay(fadeDelay).
						SetTrans(Tween.TransitionType.Expo).
						SetEase(Tween.EaseType.Out);
				}
				

				if (shrinkDelay > 0)
				{
					tween.Parallel().TweenProperty(_meshInstance, "scale", Vector3.Zero, 2).SetDelay(shrinkDelay);
				}

				await ToSignal(tween, "Finished");
			}
			QueueFree();
		});
	}
  • As the error says, you can't do new Tween() directly. You need to use SceneTree::CreateTween().

    Btw why do you need an asynchronous task just to create and wait for a tween?

As the error says, you can't do new Tween() directly. You need to use SceneTree::CreateTween().

Btw why do you need an asynchronous task just to create and wait for a tween?

    xyz Thanks, I figured out the rest. Is there a way to await without async? I did end up moving it to the ready with async enabled, if thats what you mean.

    • xyz replied to this.

      ZachAR3 Yes, you can await from any piece of script code. Engine scripts work like coroutines by design.

        xyz Ok, I have it in my _ready. Thanks for the note!