#Global functions

18 messages · Page 1 of 1 (latest)

dusty forge
#

Is there some way of registering global / util functions or allowing certain functions to be accessed via static calls? Creating a class with static functions so I can call Class.function() doesnt work here.

An alternative I tried was something like the following

const FakeClass = {
   doSomething(param) {
      // some stuff here
   }
}

global.FakeClass = FakeClass

or something like

function FakeClass() { }

// example 1
FakeClass.doSomething = function(param) {
   // some stuff here
}

// example 2
FakeClass.prototype.doSomething = function(param) {
   // some stuff here
}

global.FakeClass = FakeClass

and then calling FakeClass.doSomething(). But both of these give me a java.lang.UnsupportedOperationException

Is there some way I can get a Class.function() style of calling functions across scripts?

digital shaleBOT
#

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

short ginkgo
#

As of 1.21.1 you can only define global in startup, but you can define a js object first and define other js objects in the server.

dusty forge
short ginkgo
#

for example:

startup_scripts

global.Classes = {}

server_scripts

function FakeClass() {}
FakeClass.prototype.run = function () { return 1; }

global.Classes.FakeClass = () => new FakeClass();

const { Classes } = global;

How to use

server_scripts

const Fake = Classes.FakeClass();
console.log(Fake.run()); // 1
dusty forge
#

that seems simple enough, thank you :D

dusty forge
# short ginkgo for example: ### `startup_scripts` ```js global.Classes = {} ``` ### `server_scr...

startup_scripts/globals.js

global.Classes = {}

server_scripts/script1.js

function Fake1() {}

Fake1.prototype.something() { //something }

global.Classes.Fake1 = () => new Fake1()

server_scripts/script1.js

function Fake2() {}

Fake1.prototype.something() { //something }

global.Classes.Fake2 = () => new Fake1()

server_scripts/globals.js

const { Classes } = global

const FakeClass1 = Classes.Fake1()
const FakeClass2 = Classes.Fake2()

server_scripts/example.js

ServerEvents.someEvent(event => {
   event.do(FakeClass1.something())
})

ServerEvents.someOtherEvent(event => {
   event.do(FakeClass1.something())
})
dev.latvian.mods.rhino.EcmaError: TypeError: Cannot find function FakeClass1 in object [object Object]
dev.latvian.mods.rhino.EcmaError: TypeError: TypeError: redeclaration of const FakeClass1
dev.latvian.mods.rhino.EcmaError: TypeError: TypeError: redeclaration of const FakeClass1

Am I doing something wrong here?

short ginkgo
#

Perhaps you could try changing the priority of script loading.

vapid sailBOT
#

In 1.19.2 and all versions above you can use various headers at the top of a script to change its load conditions.
For example:

//ignored: true
console.log("I am never printed")
//packmode: default
console.log('I will only print when packmode in kubejs/config/common.properties is set to default')
//requires: minecraft
//requires: create
console.log('I will only print when mods with ids of minecraft AND create are loaded')

You can stack these too, like so

//priority: 10
//packmode: hard
//requires: create
//requires: tconstruct
console.log('I am complicated!')
short ginkgo
#

add //priority: 100 to top of file

dusty forge
# short ginkgo add `//priority: 100` to top of file

I added that to the top of server_scripts/globals.js and the original three errors are gone, but now it says this Thonk

dev.latvian.mods.rhino.EcmaError: TypeError: Cannot find function Fake1 in object [object Object]
short ginkgo
#

also script1.js and script2.js

It is important to make sure that everything is defined at the time of invocation.

short ginkgo
#

Lower the priority of globals.js and try it.

//priority: 99
dusty forge
short ginkgo
#

or just ```js
const FakeClass1 = global.Classes.Fake1()

dusty forge
dusty forge
#

startup_scripts

global.class = {
   something: function(perms) {
      ...
   }
}

server_scripts

ServerEvents.someEvent(event => {
   event.do(global.class.something())
})

this works :)