MZ My first plugin (Solved)

I am learning so please be patient with me. I am familiar but but by no means an expert with javascript. I need my variable to have an initial value in my plugin. I don't want it to keep the value but its returning a null value error and I can't play the game.

Code:
/** /*:
 * @target MZ
 *
 * @author LadyLegend
 * @plugindesc First Plugin
 *
 *
 * @help
 * Help text
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
*/


;

(() => {

    
    
    class Window_GoldPop extends Window_Base {
        constructor(rect) {
            super(rect);
        }

        update() {
            super.update();
            this.aupdate();
        }

        async aupdate() {
            return new Promise((s,f) => {
                try {
                    
                    this.contents.clear();
                    this.drawIcon($dataItems[$gameVariables.value(1)].iconIndex, 0, 0);
                    this.drawText($gameParty._gold, 0, 0, this.contents.width, 'center');
                    
                    s();
                } catch (e) {
                    f(e);
                }
            }).then().catch(e => {
                throw e;
            });
        }
    }

    const sms = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        sms.call(this);
        this.goldwinp = new Window_GoldPop(new Rectangle(0, 0, 240, Window_Base.prototype.lineHeight() * 2));
        this.addChild(this.goldwinp);
    }
})();
What exactly are you trying to do with this and how is it intended to differ from the gold window that's already on the map? This seems overly-complicated for what it appears to be.
What exactly are you trying to do with this and how is it intended to differ from the gold window that's already on the map? This seems overly-complicated for what it appears to be.
Oh I am drawing icons to the window. The window stays on screen. I eventually will be adding more to the script like backgrounds etc.

It works when I use this:
this.drawIcon($dataItems[1].iconIndex, 0, 0);

but this:
this.drawIcon($dataItems[$gameVariables.value(1)].iconIndex, 0, 0);
makes a null error

from what I understand is that the variable is set to 0 initially and there's no index icon at 0 it return null;
Oh I am drawing icons to the window. The window stays on screen. I eventually will be adding more to the script like backgrounds etc.

It works when I use this:
this.drawIcon($dataItems[1].iconIndex, 0, 0);

but this:
this.drawIcon($dataItems[$gameVariables.value(1)].iconIndex, 0, 0);
makes a null error

from what I understand is that the variable is set to 0 initially and there's no index icon at 0 it return null;
The issue is that all the $data arrays have null as their 0-index item; it's done like this so that the database can list items from ID 1 and they will map directly to the array, rather than the index always being off by 1. So when the variable value is 0, $dataItems[$gameVariables.value(1)] is null, and null items don't have an iconIndex. You'll have to put in a check for the value being above 0 first.

I'm curious though as to why you're using async and a promise for this. You should just be able to modify the drawing of the window itself, the flow of it is already handled by the engine.
The issue is that all the $data arrays have null as their 0-index item; it's done like this so that the database can list items from ID 1 and they will map directly to the array, rather than the index always being off by 1. So when the variable value is 0, $dataItems[$gameVariables.value(1)] is null, and null items don't have an iconIndex. You'll have to put in a check for the value being above 0 first.
So like an if else statement?

I'm curious though as to why you're using async and a promise for this. You should just be able to modify the drawing of the window itself, the flow of it is already handled by the engine.
I'm following a tutorial so I don't know. I'm follow to the point of drawing the gold info in the window and decided to try adding icons on my own.
So like an if else statement?


I'm following a tutorial so I don't know. I'm follow to the point of drawing the gold info in the window and decided to try adding icons on my own.
Yeah, you'd wrap it in an if statement for $gameVariables.value(1) being > 0
Yeah, you'd wrap it in an if statement for $gameVariables.value(1) being > 0
the if else worked! Of course it would that should've came to my mind instantly. I know I won't forget now. Thanks!
Glad to be of assistance! Sometimes we can't see the wood for the trees, I get it.
Glad to be of assistance! Sometimes we can't see the wood for the trees, I get it.
About the async and promise: Is it a problem to use those? I don't know what the alternative looks like.
About the async and promise: Is it a problem to use those? I don't know what the alternative looks like.
Just...not using them.

There's no harm in this case, but there's also no need for an asynchronous operation. What you're doing in the promise doesn't depend on any other data being available at the time. You're just adding extra complexity and work for no return.
Just...not using them.

There's no harm in this case, but there's also no need for an asynchronous operation. What you're doing in the promise doesn't depend on any other data being available at the time. You're just adding extra complexity and work for no return.
So I put all that inside the update function and voila!
Screenshot 2023-01-03 at 12.55.30 PM.png


I use to write code for Unity. 10+ years ago and my brains dumped alot of that info lol. from what I remember is that the update function runs all the time and to put things in there sparingly, but there were others that ran on load once and after execution. I don't remember the syntax or even if it was unity or some other engine. I'll remember one day since I'm doing this again.
So I put all that inside the update function and voila!View attachment 249653

I use to write code for Unity. 10+ years ago and my brains dumped alot of that info lol. from what I remember is that the update function runs all the time and to put things in there sparingly, but there were others that ran on load once and after execution. I don't remember the syntax or even if it was unity or some other engine. I'll remember one day since I'm doing this again.
Yeah. Update functions are generally run every frame, so 60 times per second. You don't want to do too many intensive operations in there or you'll slow your game down.
Yeah. Update functions are generally run every frame, so 60 times per second. You don't want to do too many intensive operations in there or you'll slow your game down.
What are the other functions besides update? I need a serious memory jog.
What are the other functions besides update? I need a serious memory jog.
Standard ones for windows? Initiialize (the constructor), refresh, update. Window_Selectable also has drawAllItems, drawItem, select etc.
Standard ones for windows? Initiialize (the constructor), refresh, update. Window_Selectable also has drawAllItems, drawItem, select etc.
Yessssss I remember some of these thanks.

ok so I want to know for streamlining purposes how do I use the conditional ternary operator in my code instead of the if else statements?
Yessssss I remember some of these thanks.

ok so I want to know for streamlining purposes how do I use the conditional ternary operator in my code instead of the if else statements?
You'd do something like

JavaScript:
let icon = $gameVariables.value(1) > 0 ? $gameVariables.value(1) : 1;
You'd do something like

JavaScript:
let icon = $gameVariables.value(1) > 0 ? $gameVariables.value(1) : 1;
hmm it didn't work go the null error again
hmm it didn't work go the null error again
Show me your code now.
Screenshot 2023-01-03 at 2.33.52 PM.png
I deleted the other icon and it worked but it was both working with the Original if else. I'll tinker now that I see it works. Once again thank you!

edited: I got them to work your fantastic.

Original thread: https://forums.rpgmakerweb.com/threads/my-first-plugin-solved.153729/