-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use global variables? #111
Comments
Try declare some var in Nevermind, tested here they don't share same 16:18:59 [INFO] ["script2"] "set script2 var"
16:19:01 [INFO] ["script1"] "get script2 var: undefined" script1.ts wait(1500);
log('get script2 var: ' + globalThis['script2_var']) script2.ts globalThis['script2_var'] = 1234;
log('set script2 var')
wait(1500)
Also theres script2: addEventListener("Script2:OnLoad", () => {
log("script1: received script2 load callback!")
}); |
Thank you, but the variable scope of eventListener.ts interface OnPedCreateEvent {
name: string;
data: { address: int };
}
const peds: number[] = [];
addEventListener('OnPedCreate', ({ data }: OnPedCreateEvent) => peds.push(data.address));
addEventListener('logPeds', () => {
log(`logPeds: ${peds.join()}`);
});
export const getPeds = () => {
log(`getPeds: ${peds.join()}`);
return peds;
}; script1.ts import { getPeds} from 'eventListener.ts';
getPeds();
dispatchEvent('logPeds');
In the This means that when using I tried putting the tool.ts export class PedSingle {
static instance: PedSingle;
private peds: Char[] = [];
constructor() {
if (!PedSingle.instance) {
PedSingle.instance = this;
}
return PedSingle.instance;
}
getPeds() {
try {
return this.peds.filter((item) => !Char.IsDead(item as any));
} catch (error) {
log(`PedSingle getPeds error: ${error}`);
return [];
}
}
addPed(char: Char) {
try {
this.peds = this.getPeds();
this.peds.push(char);
} catch (error) {
log(`PedSingle addPed error: ${error}`);
}
}
}
export const PedSingleInstance = new PedSingle(); eventListener.ts import { PedSingle } from './.config/tools.ts';
addEventListener('OnPedCreate', (event: OnPedCreateEvent) =>
PedSingle.instance.addPed(Memory.GetPedRef(event.data.address))
);
addEventListener('logPeds', () => {
log(
`eventListener-logPeds: ${PedSingle.instance
.getPeds()
.map((item) => item.valueOf())
.join()}`
);
}); script1.ts import { PedSingle } from './.config/tools.ts';
dispatchEvent('logPeds');
log(
`getPeds: ${PedSingle.instance
.getPeds()
.map((item) => item.valueOf())
.join()}`
); logPeds.ts import { PedSingle } from './.config/tools.ts';
addEventListener('logPeds', () => {
log(
`logPeds: ${PedSingle.instance
.getPeds()
.map((item) => item.valueOf())
.join()}`
);
});
Only in the callback function of |
in this example
|
Add log to |
script1.ts import { KeyCode } from './.config/vc.enums.ts';
while (true) {
wait(0);
if (Pad.IsKeyDown(KeyCode.Num2)) {
log('dispatchEvent:logPeds');
dispatchEvent('logPeds');
}
} eventListener.ts addEventListener('OnPedCreate', (event: OnPedCreateEvent) => {
const char = Memory.GetPedRef(event.data.address);
log(`new PedCreate: ${char.valueOf()}`);
PedSingle.instance.addPed(char);
});
addEventListener('logPeds', () => {
log(
`logPeds: ${PedSingle.instance
.getPeds()
.map((item) => item.valueOf())
.join()}`
);
}); logPeds.ts import { PedSingle } from './.config/tools.ts';
addEventListener('logPeds', () => {
log(
`logPeds: ${PedSingle.instance
.getPeds()
.map((item) => item.valueOf())
.join()}`
);
});
|
eventListener.ts and logPeds.ts are two separate scripts, they don't share memory. Each of them has its own copy of |
in other words, each script owns it's own copy of runtime. you can share memory by allocating a memory chunk in one script and passing a pointer to it using CLEO.runScript But I can hardly imagine when this can be needed unless you're cooking something really complicated. You can just combine all files in one big script. You can run different subscripts using this technique |
Oh, thank you. I'll try again. Can you support simpler sharing of global variables? At first, I used So I am considering using the OnPedCreate event to implement it, and it can be read normally in any script, when needed. for (let i = -xyRadius; i < xyRadius; i += radius) {
for (let j = -xyRadius; j < xyRadius; j += radius) {
for (let k = -zRadius; k < zRadius; k++) {
chars.push(World.GetRandomCharInSphereNoSaveRecursive(x + i, y + j, z + k, radius, false, true));
}
}
} |
There are multiple ways of sharing data across scripts already.
I don't plan on adding more ways, especially for global state because it's always more trouble than help. If you need a shared data, put it all in one script and think how you can cache some costly operations. |
Okay, thank you. I don't know much about CLEO, I am familiar with JS syntax, so I am trying to write some simple functions myself. I think this phenomenon is quite strange, so I brought up this issue. I will try your suggestion. |
There's another (more complex and weird) way to do this, its implementing basic RPC-like structure around addEventListener and dispatchEvent like multiplayer games does. But its simple just use memory allocation or |
How to define global variables in Redux, similar to the syntax of SannyBuilder, so that any script can share this global variable
The text was updated successfully, but these errors were encountered: