ThePix / QuestJS

A major re-write of Quest that is written in JavaScript and will run in the browser.
MIT License
66 stars 13 forks source link

preSave running on game startup? (I probably have something coded improperly.) #42

Closed KVonGit closed 3 years ago

KVonGit commented 3 years ago

Hello.

I don't know if this is a bug, or if I have something messed up.

This also involves this. So, I was afraid to post it on the forum. (Any misinformation I may inadvertently post should be less apt to confuse people here.) (Also note that I've replaced this with w.TV, and it works fine.)


The preSave and postLoad on my TV object:

    preSave:()=>{
        if (ytPlayer) {
            this.ytPlayerInfo = ytPlayer.playerInfo
            this.ytDisplayed = $("#youtube").attr("display")
            console.info("Saving ytPlayer.playerInfo to this.ytPlayerInfo.")
            log(this)
        }
        console.info("preSave running.")
    },
    postLoad:()=>{
        console.info("Running postLoad on TV . . .")
        log("this:")
        log(this)
        if (this.ytPlayerInfo){
            log(this)
            console.info("Found ytPlayerInfo")
            if (!ytPlayer) {
                createYouTube()
                console.info("Youtube created")
            }
             ytPlayer.playerInfo = this.ytPlayerInfo
             $("#youtube").attr("display", this.ytDisplayed)
        }
        console.info("Postloaded.")
    }

The following is from the console on game startup. I do not save during this process, but preSave runs when the game loads.

Also, this seems to be targeting the window object rather than the item in this scenario. (I promise I didn't find this bit out purposefully.)


image


In fact, I just messed with it some more, and preSave seems to run every time I interact with the TV.

I am probably misunderstanding this.

EDIT

Now I get it. It preSaves every time the item is messed with during play to make sure all the attributes are up to date (I think).


The entire TV object:

createItem("TV", SWITCHABLE(false), {
    loc:"cellar",
    examine:"An ordinary TV, currently{if:TV:switchedon: tuned to Quest TV: switched off}.<br><br>The remote is not here.\
      So, all you can do is switch it on or off.",
    onSwitchOn:function(){
        onlineCheck()
        w.TV.switchedOnTimes++;
        msg("XM has it on Quest TV, and he has hidden the remote.");
        if (settings.noConnection){
            return
        }
        let vid = getYouTubeIdByTitle("It is pitch dark")
        if (!w.TV.switchedOnTimes){
            w.TV.switchedOnTimes = 0;
            createYouTube(vid,390,640,{ 'playlist': [vid, '7vIi0U4rSX4'],
                                    'autoplay': 1,
                                    'loop':1,
                                    'rel':0 });
        }else{
            var cvid;
            cvid  = getYouTubeCurrentId();
            if (cvid !== '7vIi0U4rSX4' ){
                loadYouTubeVideoById('7vIi0U4rSX4');
                //playYouTube()
            }else{
                playYouTube();
                showYouTube();
            }
        }

        showYouTube();

        if (w.TV.switchedOffTimes===1){
            //msg ("The video starts over every time you turn it off and back on.");
        }
    },
    onSwitchOff:function(){
        if (!w.TV.switchedOffTimes){
            w.TV.switchedOffTimes = 0;
            msg ("{b:{i:[SYSTEM MESSAGE:  THANK YOU!]}}");
        }
        w.TV.switchedOffTimes++;
        if (!settings.noConnection){
            pauseYouTube();
            hideYouTube();
        }
    },
    preSave:()=>{
        if (ytPlayer) {
            this.ytPlayerInfo = ytPlayer.playerInfo
            this.ytDisplayed = $("#youtube").attr("display")
            console.info("Saving ytPlayer.playerInfo to this.ytPlayerInfo.")
            log(this)
        }
        console.info("preSave running.")
    },
    postLoad:()=>{
        console.info("Running postLoad on TV . . .")
        log("this:")
        log(this)
        if (this.ytPlayerInfo){
            log(this)
            console.info("Found ytPlayerInfo")
            if (!ytPlayer) {
                createYouTube()
                console.info("Youtube created")
            }
             ytPlayer.playerInfo = this.ytPlayerInfo
             $("#youtube").attr("display", this.ytDisplayed)
        }
        console.info("Postloaded.")
    }
});
KVonGit commented 3 years ago

Never mind.

I'm silly. It just preSaves everything every time something changes. That's how it should be, I believe.

(See? I had a feeling I shouldn't post this one on the forum. This way, I can just close the issue.)