DRincs-Productions / pixi-vn

Create visual novels with a modern 2D rendering engine and your favorite JavaScript framework.
https://pixi-vn.web.app/
GNU General Public License v3.0
10 stars 0 forks source link

Storage should not use RAM #236

Open BlackRam-oss opened 1 week ago

BlackRam-oss commented 1 week ago

``Currently the game storage uses ram. Meaning it uses a static variable inside a class. https://github.com/DRincs-Productions/pixi-vn/blob/main/src/managers/StorageManagerStatic.ts#L5

This creates problems when multiple processes write to it. (This bug is especially noticeable when using '<>' of ink.) The problem I encountered can be explained as follows (it coincides with the narration.goNext of the library):

export default class StorageManagerStatic {
    static index = 0
    static storage: { [key: string]: any } = {}
    static setVariable(key: string, value: any) {
        key = key.toLowerCase()
        StorageManagerStatic.storage[key] = value
    }
    static getVariable<T>(key: string): T | undefined {
        key = key.toLowerCase()
        if (StorageManagerStatic.storage.hasOwnProperty(key)) {
            return StorageManagerStatic.storage[key] as T
        }
        return undefined
    }
}

const steps = [
    async () => { StorageManagerStatic.setVariable("dialogue", "A"); goNext() },
    async () => { StorageManagerStatic.setVariable("dialogue", StorageManagerStatic.getVariable("dialogue") + "B"); goNext() },
    async () => { StorageManagerStatic.setVariable("dialogue", StorageManagerStatic.getVariable("dialogue") + "C") }
]

async function goNext() {
    StorageManagerStatic.index = StorageManagerStatic.index + 1
    console.log(StorageManagerStatic.getVariable("dialogue"))
    if (steps.length > StorageManagerStatic.index) {
        let step = steps[StorageManagerStatic.index]
        await step()
    }
}

async function start() {
    let step = steps[0]
    await step()
    console.log(StorageManagerStatic.getVariable("dialogue"))
}

start()

the result I expect is that the dialogue is equal to '"ABC"', but it is not so because it is equal to '"C"'.

The reason is not clear to me, but putting a log getVariableStorage("dialogue") in goNext, and the values ​​the logs are as follows: "A" "AB" "ABC".

I don't understand why if I put a log at the end of start() the result is "C".


the simplest solution that comes to mind is to use localStorage. but I would avoid it because it has a maximum space limit and is a bit slow.

I was wondering if there were better solutions. The constraints are as follows:

BlackRam-oss commented 1 week ago

hello @blurymind sorry if I ask so many questions.

do you know of any libraries that I could use to manage the game's storage or do you know the solution to this problem?

blurymind commented 6 days ago

I found a bunch of options out there, but decided to write my own in order to avoid adding yet another dependency to yarn https://github.com/blurymind/YarnClassic/blob/master/src/js/classes/storage.js#L3 funnily enough it ends up being dependent on idb :rofl:

I took inspiration from a codepen. The browser's db api is much more confusing than localStorage imo. I found it much harder to set up than it, so the intention of the class I wrote in yarn was to allow me to replace usages of localstorage https://codepen.io/blurymind/pen/mdgpYBj

BlackRam-oss commented 6 days ago

Ok thanks I'll take a look to "take inspiration" (copy).

Anyway I found out the error had nothing to do with the processes it was my stupid mistake.

I was looking especially for a library that also has a chrome extension to view and modify the values. like the one from pixijs https://chromewebstore.google.com/detail/pixijs-devtools/aamddddknhcagpehecnhphigffljadon?pli=1 , but instead of modifying a canvas it modifies the storage