Skip to content

RewardedMiniGame

Gates an in-app reward after playing a game with a given character. The user plays for minPlayThreshold seconds, plays a brief ad, then taps Claim Reward.

Rewarded mini-game ad with claim reward flow on mobile

Basic Usage

jsx
import { useEffect, useRef, useState } from 'react';
import { SimulaRewardedMiniGame, SimulaEventType } from '@simula/ads-react-native';

function MessageLimitGate() {
  const [loaded, setLoaded] = useState(false);
  const retryAttempt = useRef(0);
  const rewardedGame = useRef(null);

  useEffect(() => {
    rewardedGame.current = SimulaRewardedMiniGame.init({
      adUnitId: 'simula-xxxx-rewarded', // Optional: provided by Simula for tracking and measurement
    });

    const subs = [
      rewardedGame.current.addAdEventListener(SimulaEventType.LOADED, () => {
        retryAttempt.current = 0;
        setLoaded(true);
      }),
      rewardedGame.current.addAdEventListener(SimulaEventType.LOAD_FAILED, () => {
        retryAttempt.current += 1;
        if (retryAttempt.current > 6) return;
        const delay = Math.pow(2, retryAttempt.current);
        setTimeout(() => rewardedGame.current.load(), delay * 1000);
      }),
      rewardedGame.current.addAdEventListener(SimulaEventType.EARNED_REWARD, () => {
        grantMessageCredits(10); // Only grant the reward here
      }),
      rewardedGame.current.addAdEventListener(SimulaEventType.REWARD_VERIFICATION_FAILED, () => {
        showError("Reward verification failed. Please try again.");
      }),
      rewardedGame.current.addAdEventListener(SimulaEventType.CLOSED, () => {
        setLoaded(false);
        rewardedGame.current.load();
      }),
    ];

    rewardedGame.current.load();

    return () => subs.forEach(sub => sub.remove());
  }, []);

  return (
    <button disabled={!loaded} onClick={() => rewardedGame.current.show({
      charID: 'luna-123', // Optional: current character, CharacterSelector pick, or omit for Simula default
      charName: 'Luna',
      charImage: 'https://cdn.example.com/avatars/luna.png',
    })}>
      Watch an ad to earn 10 message credits
    </button>
  );
}

Init Config

ParameterTypeRequiredDefaultDescription
adUnitIdstringNoAd unit ID provided by Simula. Used for measurement and improved targeting.
minPlayThresholdnumberNo30Minimum play time in seconds before Claim Reward appears. Clamped to [10, 30].

Show Config

The character info personalizes the game experience — the AI companion reacts to gameplay, sends messages, and is present throughout. Pass in the character the user is currently talking to, a character from CharacterSelector, or omit to use a Simula default.

ParameterTypeRequiredDescription
charIDstringNoCharacter identifier
charNamestringNoCharacter name displayed in header
charImagestringNoCharacter avatar URL
charDescstringNoCharacter description

Event Types

EventPayloadDescription
SimulaEventType.LOADEDAd loaded and ready to show
SimulaEventType.LOAD_FAILEDerrorInfoAd failed to load. Retry with exponential backoff.
SimulaEventType.DISPLAYEDadInfoAd displayed on screen
SimulaEventType.DISPLAY_FAILEDerrorInfoAd failed to display. Load next ad.
SimulaEventType.CLICKEDadInfoUser tapped the ad
SimulaEventType.EARNED_REWARDSSV succeeded. Grant the reward here — only trusted signal.
SimulaEventType.REWARD_VERIFICATION_FAILEDSSV failed. Show error, do not grant reward.
SimulaEventType.CLOSEDadInfoUser closed the ad. Load next ad.

Grant rewards only on EARNED_REWARD

Always grant the reward inside EARNED_REWARD — it is the only trusted signal. Re-call .load() after CLOSED or DISPLAY_FAILED.