“getActivePinia()” was called but there was no active Pinia. Not sure how I’m using my store wrong

I encountered this error when trying to implement a game loop for my game. I’m using Pinia to store the global state of my game which I’ve created like so:

baseCamp.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { defineStore } from "pinia";
import { ref } from "vue";
export const useBaseCampStore = defineStore("baseCamp", () => {
const baseCamp = ref({
artifactSeed: 1,
population: 0,
workingPop: 0,
resources: {},
prestigeMaterial: {},
availableJobs: {},
industry: {
buildings: {},
equipment: {},
},
research: {},
stats: {},
});
const increaseResource = (resource, quantity) => {
if (!baseCamp.value.resources[resource]) {
baseCamp.value.resources[resource] = 0;
}
baseCamp.value.resources[resource] += quantity;
};
const formatNumber = (number) => {
return parseFloat(number).toFixed(0);
};
return { baseCamp, increaseResource, formatNumber };
});
</code>
<code>import { defineStore } from "pinia"; import { ref } from "vue"; export const useBaseCampStore = defineStore("baseCamp", () => { const baseCamp = ref({ artifactSeed: 1, population: 0, workingPop: 0, resources: {}, prestigeMaterial: {}, availableJobs: {}, industry: { buildings: {}, equipment: {}, }, research: {}, stats: {}, }); const increaseResource = (resource, quantity) => { if (!baseCamp.value.resources[resource]) { baseCamp.value.resources[resource] = 0; } baseCamp.value.resources[resource] += quantity; }; const formatNumber = (number) => { return parseFloat(number).toFixed(0); }; return { baseCamp, increaseResource, formatNumber }; }); </code>
import { defineStore } from "pinia";
import { ref } from "vue";

export const useBaseCampStore = defineStore("baseCamp", () => {
  const baseCamp = ref({
    artifactSeed: 1,
    population: 0,
    workingPop: 0,
    resources: {},
    prestigeMaterial: {},
    availableJobs: {},
    industry: {
      buildings: {},
      equipment: {},
    },
    research: {},
    stats: {},
  });

  const increaseResource = (resource, quantity) => {
    if (!baseCamp.value.resources[resource]) {
      baseCamp.value.resources[resource] = 0;
    }

    baseCamp.value.resources[resource] += quantity;
  };

  const formatNumber = (number) => {
    return parseFloat(number).toFixed(0);
  };

  return { baseCamp, increaseResource, formatNumber };
});

I have created my pinia instance in main.js along with my vue app.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";
const pinia = createPinia();
const app = createApp(App);
app.use(pinia);
app.mount("#app");
</code>
<code>import { createApp } from "vue"; import { createPinia } from "pinia"; import App from "./App.vue"; const pinia = createPinia(); const app = createApp(App); app.use(pinia); app.mount("#app"); </code>
import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";

const pinia = createPinia();
const app = createApp(App);

app.use(pinia);

app.mount("#app");

And brought all of that into my App.vue file where I try to initiate the loop.

App.vue

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><template>
<StateTest />
</template>
<script>
import { onMounted } from "vue";
import { mainLoop } from "./gameLoop";
import StateTest from "./components/StateTest.vue";
export default {
setup() {
onMounted(() => {
requestAnimationFrame(mainLoop);
});
},
components: {
StateTest,
},
};
</script>
</code>
<code><template> <StateTest /> </template> <script> import { onMounted } from "vue"; import { mainLoop } from "./gameLoop"; import StateTest from "./components/StateTest.vue"; export default { setup() { onMounted(() => { requestAnimationFrame(mainLoop); }); }, components: { StateTest, }, }; </script> </code>
<template>
  <StateTest />
</template>

<script>
import { onMounted } from "vue";
import { mainLoop } from "./gameLoop";
import StateTest from "./components/StateTest.vue";

export default {
  setup() {
    onMounted(() => {
      requestAnimationFrame(mainLoop);
    });
  },
  components: {
    StateTest,
  },
};
</script>

gameLoop.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { useBaseCampStore } from "@/store/baseCamp";
const store = useBaseCampStore();
const { increaseResource } = store;
var lastTime = null;
var totalTime = 0;
export function mainLoop() {
const currentTime = performance.now();
if (lastTime === null) {
lastTime = currentTime;
}
const deltaTime = currentTime - lastTime;
updateGame(deltaTime, totalTime);
totalTime += deltaTime;
lastTime = currentTime;
//console.log("it ran");
requestAnimationFrame(mainLoop);
}
// eslint-disable-next-line no-unused-vars
function updateGame(deltaTime, totalTime) {
increaseResource("wood", (1 / 1000) * deltaTime);
}
</code>
<code>import { useBaseCampStore } from "@/store/baseCamp"; const store = useBaseCampStore(); const { increaseResource } = store; var lastTime = null; var totalTime = 0; export function mainLoop() { const currentTime = performance.now(); if (lastTime === null) { lastTime = currentTime; } const deltaTime = currentTime - lastTime; updateGame(deltaTime, totalTime); totalTime += deltaTime; lastTime = currentTime; //console.log("it ran"); requestAnimationFrame(mainLoop); } // eslint-disable-next-line no-unused-vars function updateGame(deltaTime, totalTime) { increaseResource("wood", (1 / 1000) * deltaTime); } </code>
import { useBaseCampStore } from "@/store/baseCamp";

const store = useBaseCampStore();
const { increaseResource } = store;

var lastTime = null;
var totalTime = 0;

export function mainLoop() {
  const currentTime = performance.now();
  if (lastTime === null) {
    lastTime = currentTime;
  }
  const deltaTime = currentTime - lastTime;

  updateGame(deltaTime, totalTime);

  totalTime += deltaTime;
  lastTime = currentTime;

  //console.log("it ran");
  requestAnimationFrame(mainLoop);
}

// eslint-disable-next-line no-unused-vars
function updateGame(deltaTime, totalTime) {
  increaseResource("wood", (1 / 1000) * deltaTime);
}

The code compiles and will run if I leave out the onMounted() function in App.vue. It will even run if I first open it with the onMounted portion commented out and then uncomment it and it works fine. I’ve gathered from searching online and reading several different posts that I’m somehow trying to access my store before the Pinia instance has been created but I don’t quite understand why that’s the case. Does main.js not need to run first before App.vue can be interpreted?

These are the posts I’ve read but don’t really understand their answers and haven’t been able to implement either answer into my code successfully.

getActivePinia was called with no active Pinia. Vue

“getActivePinia() was called but there was no active Pinia. Did you forget to install pinia?” error in console, but the app compiles successfully

I have also tried using <script setup()> in App.vue as I read that composables are not compatible with the options API, but then I get an error when trying to import my StateTest component that won’t compile.

Ultimately I’d like to fix my code, but I think I’m having a fundamental misunderstanding of what is happening behind the scenes.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật