Skip to content

ExchangeRateMultiplier

IExchangeRateMultiplier

IExchangeRateMultiplier can be used to calculate a rate between two different IExchangeable objects. It can be thought of as a ratio used when exchanging or trading IExchangeable objects such as ICurrency or IUpgradeableCurrency.

To generate a rate, all IExchangeable objects have an exchange value which can be returned via the GetExchangeValue method. The rate between two IExchangeable objects is then based on these exchange values. The exchange values are independent of the amount of IExchangeable that the player currently has.

The rate the IExchangeRateMultiplier generates can be tweaked via the Multiplier property. This should be used to make trades or exchanges more or less favourable to the player.

Note

The exchange values of different Exchangeables should be set based on factors such as their rarity or game balance. Having a Multiplier value allows designers to alter the favorability of specific exchanges between particular Exchangeables and in different contents.

Tip

The rate generated for an exchange from Hard Currency with an exchange value of 20 to Soft Currency with an exchange value 2 would be 10 (i.e. Hard Currency is 10 times more valuable than Soft Currency, and 1 Hard Currency could be traded for 10 Soft Currency). A favourable exchange rate Multiplier of 2 would then result in a rate of 20 being returned (i.e. 1 of the Hard Currency could be traded for 20 of the Soft Currency).

ExchangeRateMultiplier

A concrete implementation of IExchangeRateMultiplier. The exchange rate is generated by passing the exchange value of two IExchangeable objects into the GetRate(IExchangeable,IExchangeable) method, which is implemented as follows:

public virtual double GetRate(IExchangeable inputExchangeable, IExchangeable outputExchangeable)
{
    return GetRate(inputExchangeable.GetExchangeValue(), outputExchangeable.GetExchangeValue());
}
protected virtual double GetRate(double inputExchangeValue, double outputExchangeValue)
{
    if (outputExchangeValue == 0)
    {
        Logs.HandleException(new ArgumentException($"Invalid output currency exchange value of 0 passed to ExchangeRate.GetRate"));
        return 0;
    }

    return (inputExchangeValue / outputExchangeValue) * ExchangeRateMultiplierData.Multiplier;
}

Relationships

ExchangeRate

Missing Resources Exchange Rate Example

The IdleKit Showcase project uses IExchangeRateMultiplier to implement the missing resource exchange functionality.

When the player does not have enough resources to upgrade an IUpgradeableCurrency, this functionality allows for an exchange of hard ICurrency to the missing resource. The Missing Resources Exchange Rate between the hard currency and the desired ICurrency is then used to calculate the input hard CurrencyAmount required to perform the exchange. The input and output CurrencyAmount are then passed to the MakeExchangeStateAction, which performs the trade by adding and removing the relevant currency amounts.

Tip

To calculate the amount of Hard Currency required to acquire 50 Soft Currency, multiply the amount of Soft Currency by GetRate(Soft Currency, Hard Currency). If Soft Currency has a value of 1 and Hard Currency has a value of 5, GetRate(IExchangeable,IExchangeable) would return a value of 0.2 (assuming a multiplier of 1 is applied). Hence, it would cost 50 x 0.2 = 10 Hard Currency to gain 50 Soft Currency.

Sample code for missing resources exchange rate can be found in ShowcaseUpgradeableCurrencyView. The diagram below outlines the exchange process.

MissingResourcesExchangeRate