how would u await something, if you cant use promises and await 👀

I have a function in a class that cannot be async, however in that function i need to run another function that will do a playthrough of a game (basically just run a set interval, every 50 ms) and when that returns, then execute the rest of the function i dont think that can work with a callback as i specifically need to wait for the function that does the playthrough to complete as it returns values that the outer function needs. which would be a perfect usecase for async await, but the function isnt async, and cant be made async without modifying the lib
26 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
sure
protected seed(): Promise<Entity> {
return new Promise<Entity>(async (resolve) => {
const wRunner: IRunner = window.Runner;
var runner = wRunner.instance_;

runner.startGame();
runner.playIntro();
runner.tRex.startJump(runner.currentSpeed);

console.log(runner.config.MAX_SPEED);

let Run = async () => {
if (!runner.tRex.jumping) {
var inputs = getObstacles(runner);

// console.log(Math.ceil(runner.distanceRan) * DIST_COEFFICIENT);

var shouldJump = ShouldJump(inputs);
if (shouldJump) {
runner.tRex.startJump(runner.currentSpeed);
}
}
};

await Run();

resolve({
shouldJump: Math.random(),
runner: runner,
});

// setInterval(() => {
// if (!runner.playing) {
// runner.restart();
// } else if (!runner.tRex.jumping) {
// var inputs = getObstacles(runner);

// // console.log(Math.ceil(runner.distanceRan) * DIST_COEFFICIENT);

// var shouldJump = ShouldJump(inputs);
// if (shouldJump) {
// runner.tRex.startJump(runner.currentSpeed);
// }
// }
// }, 50);
});
}
protected seed(): Promise<Entity> {
return new Promise<Entity>(async (resolve) => {
const wRunner: IRunner = window.Runner;
var runner = wRunner.instance_;

runner.startGame();
runner.playIntro();
runner.tRex.startJump(runner.currentSpeed);

console.log(runner.config.MAX_SPEED);

let Run = async () => {
if (!runner.tRex.jumping) {
var inputs = getObstacles(runner);

// console.log(Math.ceil(runner.distanceRan) * DIST_COEFFICIENT);

var shouldJump = ShouldJump(inputs);
if (shouldJump) {
runner.tRex.startJump(runner.currentSpeed);
}
}
};

await Run();

resolve({
shouldJump: Math.random(),
runner: runner,
});

// setInterval(() => {
// if (!runner.playing) {
// runner.restart();
// } else if (!runner.tRex.jumping) {
// var inputs = getObstacles(runner);

// // console.log(Math.ceil(runner.distanceRan) * DIST_COEFFICIENT);

// var shouldJump = ShouldJump(inputs);
// if (shouldJump) {
// runner.tRex.startJump(runner.currentSpeed);
// }
// }
// }, 50);
});
}
but seed() should only return Entity not a promise. (also this code doesnt work)
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
because i need the value from the game, in the return value so currently i have
protected seed(): Promise<Entity> {
return new Promise<Entity>(async (resolve) => {
const wRunner: IRunner = window.Runner;
var runner = wRunner.instance_;

runner.startGame();
runner.playIntro();
runner.tRex.startJump(runner.currentSpeed);

console.log(runner.config.MAX_SPEED);

let Run = async () => {
return await new Promise((res) => {
const interval = setInterval(() => {
if (!runner.playing) {
res("");
clearInterval(interval);
} else if (!runner.tRex.jumping) {
var inputs = getObstacles(runner);

// console.log(Math.ceil(runner.distanceRan) * DIST_COEFFICIENT);

var shouldJump = ShouldJump(inputs);
if (shouldJump) {
runner.tRex.startJump(runner.currentSpeed);
}
}
}, 50);
});
};

await Run();
runner.restart();

resolve({
runner: runner,
});
});
}
protected seed(): Promise<Entity> {
return new Promise<Entity>(async (resolve) => {
const wRunner: IRunner = window.Runner;
var runner = wRunner.instance_;

runner.startGame();
runner.playIntro();
runner.tRex.startJump(runner.currentSpeed);

console.log(runner.config.MAX_SPEED);

let Run = async () => {
return await new Promise((res) => {
const interval = setInterval(() => {
if (!runner.playing) {
res("");
clearInterval(interval);
} else if (!runner.tRex.jumping) {
var inputs = getObstacles(runner);

// console.log(Math.ceil(runner.distanceRan) * DIST_COEFFICIENT);

var shouldJump = ShouldJump(inputs);
if (shouldJump) {
runner.tRex.startJump(runner.currentSpeed);
}
}
}, 50);
});
};

await Run();
runner.restart();

resolve({
runner: runner,
});
});
}
however seed should be returning an entity, not a promise i need the values to be changed for the seed
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
ye the issue is i need it in the setInterval
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
because I am basicallly having to poll the chrome dino game every 50ms to check if i should jump. and only way to do that is to have it in a setInterval the other solution was a while loop, but theres no way to sleep a while loop in JS
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
GitHub
GitHub - Glavin001/genetic-js: Advanced genetic and evolutionary al...
Advanced genetic and evolutionary algorithm library written in Javascript - GitHub - Glavin001/genetic-js: Advanced genetic and evolutionary algorithm library written in Javascript
WOLFLEADER
WOLFLEADEROP3y ago
the thing is i should be able to make it a promise
WOLFLEADER
WOLFLEADEROP3y ago
its an async function but basically to generate my seeds each entity should do a playthrough of the game
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
yea
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
👍 should i await in the clone? or
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
ah true
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
well time to fork it and edit i guess haha
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
do u think maybe i need to reconsider my approach too?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
coz i just cant think of a way to generate the seed without a playthrough of the game first
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
WOLFLEADER
WOLFLEADEROP3y ago
ye fair enough. haha honestly still new to doing AI shit lmao
Want results from more Discord servers?
Add your server