Energy #

Read more about the Energy system in Hiro here.

Functions #

Get #

Get all energies defined and the values a user currently owns by ID.

1
2
3
4
5
6
userId := "userId"

energies, err := systems.GetEnergySystem().Get(ctx, logger, nk, userId)
if err != nil {
  return err
}

Spend #

Deduct the given amounts from each energy for a user by ID.

1
2
3
4
5
6
7
userId := "userId"
amounts := map[string]int32{"hearts": 1, "stamina": 3}

energies, reward, err := systems.GetEnergySystem().Spend(ctx, logger, nk, userId, amounts)
if err != nil {
  return err
}

Grant #

Add the given amounts to each energy (while applying any energy modifiers) for a user by ID.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
userId := "userId"
amounts := map[string]int32{"hearts": 1, "stamina": 3}
modifiers := []*hiro.RewardEnergyModifier{{
  Id:          "myModifier",
  Operator:    "multiplier",
  Value:       2,
  DurationSec: 86400,
  Weight:      -1,
}}

energies, err := systems.GetEnergySystem().Grant(ctx, logger, nk, userId, amounts, modifiers)
if err != nil {
  return err
}

Hooks #

SetOnSpendReward #

Set a custom reward function which will run after an energy reward’s value has been rolled.

1
2
3
4
5
6
systems.GetEnergySystem().SetOnSpendReward(OnSpendReward)

func OnSpendReward(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID, sourceID string, source *hiro.EnergyConfigEnergy, rewardConfig *hiro.EconomyConfigReward, reward *hiro.Reward) (*hiro.Reward, error) {
	// Modify reward or take additional actions.
	return reward, nil
}