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!

3
  • 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? Commented Feb 22, 2021 at 11:32
  • I call it like this StartCoroutine(smeltingCore.Smelting(smeltCopperOreButton, smeltCopperOreBar, saveLoadHandler.data.copperBar, smeltingHandler.smeltCopperOreOutput)); The BigDouble is this one "github.com/Razenpok/BreakInfinity.cs" it is basically the same like a normal double Commented Feb 22, 2021 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. Commented Feb 22, 2021 at 11:37

1 Answer 1

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 
     ));
Sign up to request clarification or add additional context in comments.

2 Comments

If i use it it says "Iterators cannot have any ref parameters". Any Idea?
@Timebreaker900 of course not, silly me. See update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.