#Rhino Error when trying to create global variables

53 messages · Page 1 of 1 (latest)

broken bone
#

I have the following script in my serverscripts:

// priority: 1000000
const itemHandler = {
    createItemStack: (itemId, amount) => {
        return {id: itemId, count: amount}
    },

    getItemOf: (itemStack) => {
        return Item.of(itemStack.id, itemStack.count);
    },

    getItemIngredient: (itemStack) => {
        return {
            "count": itemStack.count,
            "id": itemStack.id
        };
    }
};

const fluidHandler = {
    createFluidStack: (fluidId, amount) => {
        return {id: fluidId, amount: amount}
    },

    getFluidOf: (fluidStack) => {
        return Fluid.of(fluidStack.id, fluidStack.count);
    },

    getFluidIngredient: (fluidStack) => {
        return {
            "amount": fluidStack.amount,
            "id": fluidStack.id
        };
    }
};

global.itemHandler = itemHandler;
global.fluidHandler = fluidHandler;

It serves as a way for me to handle items and fluids easily, but that is besides the point.

When I run this code, I get the attached error

by commenting out the bottom 2 lines (globalizing the variables), this error disappears. I declared them at the bottom while debugging, normally I would just directly declare them but here we are.

I have done this exact same thing, creating a global object with functions, at multiple other scripts in the same instance already, yet here it breaks, and I can't figure out why.

glossy trailBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

royal oreBOT
#

Paste version of message.txt from @broken bone

broken bone
#

I did some further testing, every global.<variable> = {} in server scripts does this

they are completely fine in the start-up scripts tho

#

changing the type (e.g. using a string or array instead of an object) does not change anything

grand bone
#

In remaining script types, it's read-only

broken bone
#

🙁

grand bone
#

Why would you want to assign to global in server scripts anyway?

broken bone
#

simply put, I am generating data that gets used primarily on the server so I figured I would put it there

grand bone
#

If you want to use a variable between script types, put it in startup scripts.
If only in one script type, just put it as a variable in global scope

main hill
#

you only need global if what you put in it is intended to be used in more than one type of script

broken bone
#

wait you are saying I can access variables from all my server scripts in every server script?

grand bone
#

Yes

main hill
#

yes

broken bone
#

my life is a lie

grand bone
#

All scripts of a type share the same global scope

main hill
#

in the background, kubejs loads all of the same script types into one unified "script" so to say

midnight sand
#

is it also the case in 1.20.1?

main hill
#

yes

grand bone
#

Yes

main hill
#

it was never not the case

midnight sand
#

my life was also a lie

grand bone
#

That's why if you declare a const somewhere in global scope, even in a different file, it won't let you reassign it

midnight sand
#

that makes so much sense lol

broken bone
#

it sounds logical but also something I would have never realised

main hill
#

you separating your server scripts into different files is just shits and giggles
nothing more than for looks
functionally it does nothing

grand bone
#

So for example:

// file1.js
const ITEMS = ["minecraft:stone", "minecraft:cobblestone"]
// file2.js
const ITEMS = ["minecraft:deepslate", "minecraft:tuff"] // ERROR - reassigned const
midnight sand
#

I feel even dumber because I did run into an error where I reassigned a variable in different file and it wouldnt let me 😭

main hill
grand bone
#

If you don't want name clashes, put your scripts in IIFEs so you have control over what you assign to global scope:

// file1.js
(() => {
  const ITEMS = ["minecraft:stone", "minecraft:cobblestone"]
})()
// file2.js
(() => {
  const ITEMS = ["minecraft:deepslate", "minecraft:tuff"]
})()
// No name clashes!
main hill
#

i see a lot of people in support threads and in example scripts under the impression that global is needed for this
i dont know where this came from, it was never the case, nobody says it is the case, and its been explained countless times that its not how it works

broken bone
#

I mean in my mind the existance of global implied that non global variables were local

main hill
grand bone
#

Yeah, I also used to think like that

broken bone
#

and since we do most of the stuff inside event calls, the variables there are local to the call and you wouldn't notice this behaviour

grand bone
#

Until I finally understood what global is actually for

main hill
#

should be called superGlobal or something then xd

broken bone
#

that why when people go looking they see it

main hill
#

i miss the times when i could edit directly on the website bceTired3Dead4

#

now i gotta PR it, wait for approval and whatever other bs
just makes me not wanna do anything

#

xd

broken bone
#

I feel that in my bones

#

the greatest obstacle to documentation is not willingness but tediousness

main hill
#

i used to update the wiki when people reported typos, or issues, or additional missing info

#

too much paperwork now

grand bone
main hill
#

yeah... too many steps now, so i dont touch it

#

we just had approved accs before with edit access

#

directly on the website

broken bone
#

anyways thank you for the help and the lesson