1

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!

Timebreaker900
  • 312
  • 2
  • 7
  • 21
  • There are a few key-points missing. This is a function with a `yield return`. How do you call it? How exactly is `+=` implemented for `BigDouble`? – Chriz Feb 22 '21 at 11:32
  • I call it like this `StartCoroutine(smeltingCore.Smelting(smeltCopperOreButton, smeltCopperOreBar, saveLoadHandler.data.copperBar, smeltingHandler.smeltCopperOreOutput));` The BigDouble is this one "https://github.com/Razenpok/BreakInfinity.cs" it is basically the same like a normal double – Timebreaker900 Feb 22 '21 at 11:34
  • But the problem isn't about the yield return or something. I think it is a basic c# problem. I can't add the output value to the material value. – Timebreaker900 Feb 22 '21 at 11:37

1 Answers1

2

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 
     ));
Jamiec
  • 133,658
  • 13
  • 134
  • 193