What I'm trying to achieve here is for the thread to await at FishData fishData = await Fishing().WaitOrCancel(cancelToken);
until a not null fishData is returned, else just keep on waiting and checking TryHook() again every 1 second.
I have also tried replacing the await Task.Delay(1000);
with Thread.Sleep() but they just doesn't seems to work and instead kept on causing crashes. also tried InvokeRepeating and coroutines but everything got too messy and I'm not even sure I implemented them correctly.
public async Task<bool> Fish(int ID)
{
reelingSuccess = false;
FishingRodData fishingRodData = inventory.fishingRods[ID];
if (currentCell.fill == CellFill.Water)
{
CancellationToken cancelToken = new CancellationToken();
try
{
FishData fishData = await Fishing().WaitOrCancel(cancelToken);
bool reelSuccess = await ReelingFish(fishingRodData.cycleTime, fishingRodData.tolerance, fishData.cycles, fishData.hits).WaitOrCancel(cancelToken);//, fishData.speed
if (reelSuccess) print("successfully reeled in fish");
else print("failed reeling in fish! better luck next time!");
inventory.interactBtn.onClick.RemoveAllListeners();
inventory.interactBtn.onClick.AddListener(delegate { inventory.itrct(); });
}
catch (OperationCanceledException) { print("fishing has been cancelled!"); }
return true;
} return false;
}
async Task<FishData> Fishing()
{
**await Task.Delay(1000);**
print("try hook");
FishData fish = TryHook();
if (fish == null) fish = await Fishing();
return fish;
}
ps: forgive my english
EDIT: here's the TryHook Code
its basically trying to catch a fish based on probability which will mostly return a null, but once every few calls from FishData fish = TryHook();
in Fishing(), it might return a fish.
FishData TryHook()
{
FishData fish = null;
for (int i = 0; i < fishLs.Length; i )
{
fishLs[i].minValue = inventory.fishes[i].minValue;
fishLs[i].maxValue = inventory.fishes[i].maxValue;
fishLs[i].probability = inventory.fishes[i].probability;
}
int index = GetRandomValue(fishLs);//get random index in fishLs but based on probability instead of Random.Range()
if (index != -1) { print("caught fish ind: " index); fish = inventory.fishes[index]; }
return fish;
}
EDIT2: not sure if this is important, but this is how the Fish() is called.
void Start()
{
itrct = new interact(Interact);
fs = new Fish(activities.Fish);
rf = new removeFront(RemoveFront);
tryActivities.Add(new Func<bool>(() => activities.DoNothing()));
//tryActivities.Add(new Func<bool>(() => activities.Fish(currentSlot.basicData.typeID).Result));
tryActivities.Add(new Func<bool>(() => fs(currentSlot.basicData.typeID).Result));
tryActivities.Add(new Func<bool>(() => activities.UseStove(currentSlot.basicData.typeID)));
}
public void Interact()//id of currently hold item
{
Debug.Log("interact");
if (currentSlot != null && currentSlot.basicData.allID != -1 && TryPlaceOrUse())//check for hand activity
{
Debug.Log("successfully do in-hand activity");
}
else if (currentCell.objects.Count > 1 && currentCell.objects[^1].GetComponent<SpawnedObjectData>().allID != -1 &&
tryActivities[(int)allBasicData[currentCell.objects[^1].GetComponent<SpawnedObjectData>().allID].myActivity]())//check activity infront
{
//await tryActivities[2];
Debug.Log("successfully do in-front activity");
}
else Debug.Log("show emotes~ there's not hing to do ~~");
}
CodePudding user response:
async Task<FishData> Fishing()
{
while(true)
{
await Task.Delay(1000);
print("try hook");
FishData fish = TryHook();
if (fish != null)
{
return fish;
}
}
}
and then:
var data = await Fishing();
// do what you want with your fish here