I'm trying to extract a lot of code in my program and I encountered a problem.
This is my function to extract a smelting process:
public IEnumerator Smelting(Button button, Image bar, BigDouble material, BigDouble output)
{
button.interactable = false;
float t = 0f;
while (t <= 1f)
{
bar.fillAmount = t;
t += Time.deltaTime / 4;
yield return null;
}
bar.fillAmount = 0;
//saveLoadHandler.data.copperBar += smeltingHandler.smeltCopperOreOutput;
material += output;
button.interactable = true;
}
My Problem is now that I want to add the output to the material with: material += output
. The Command above shows what these parameter look like.
So it looks like in this case the material
doesn't get assigned correctly.
I don't want to go the way if(type = copper){copper += output } else if(type = iron)...
Is there a workaround for that?
Thanks in advance!
if BigDouble
is just like a normal double
, it is a value type , and is therefore being passed by value to your method. You can add the ref
keyword to pass it by reference:
public IEnumerator Smelting(Button button, Image bar, ref BigDouble material, BigDouble output)
However, iterators cannot have ref arguments, so another workaround might be to pass a delegate to the method who's job it is to increment something
public IEnumerator Smelting(Button button, Image bar, Action<BigDouble> incrementMaterial, BigDouble output) {
....
incrementMaterial(output); // instead of material += output
....
}
and then the caller:
StartCoroutine(smeltingCore.Smelting(
smeltCopperOreButton,
smeltCopperOreBar,
amount => saveLoadHandler.data.copperBar += amount,
smeltingHandler.smeltCopperOreOutput));
Of course, at this point you're only passing in output
to use it in the delegate so you can simplify both:
public IEnumerator Smelting(Button button, Image bar, Action incrementMaterial) {
....
incrementMaterial();
....
}
and
StartCoroutine(smeltingCore.Smelting(
smeltCopperOreButton,
smeltCopperOreBar,
() => saveLoadHandler.data.copperBar += smeltingHandler.smeltCopperOreOutput
));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.