修复async content中事件不使用await会报错的问题2

This commit is contained in:
shijian 2023-12-18 02:12:03 +08:00
parent d6941b3774
commit feaac1735e
2 changed files with 55 additions and 13 deletions

View File

@ -2211,6 +2211,8 @@ game.import('character',function(lib,game,ui,get,ai,_status){
frequent:true,
preHidden:true,
async content(event,trigger,player){
player.damage();
//player.recover();
await player.draw();
},
},

View File

@ -32148,24 +32148,56 @@ new Promise(resolve=>{
// 如果父级事件也是一个异步的话,那应该立即执行这个事件的
// 如果在AsyncFunction执行过程中在别的位置新建了一个异步事件那也直接等会set配置完执行
if (eventPromise&&_status.event.content instanceof AsyncFunction){
// 异步执行game.loop
// 不直接game.loop(event)是因为需要让别人可以手动set()和setContent()
// 再执行game.loop是因为原有的game.loop被await卡住了
// 得新执行一个只执行这个异步事件的game.loop
if(!game.executingAsyncEventMap.has(_status.event.toEvent())){
game.executingAsyncEventMap.set(_status.event.toEvent(),Promise.resolve().then(()=>{
console.log(event.name, '将要执行');
if(_status.event!=eventPromise){
eventPromise.parent=_status.event;
_status.event=eventPromise;
game.getGlobalHistory('everything').push(eventPromise);
}
// 异步执行game.loop
// 不直接game.loop(event)是因为需要让别人可以手动set()和setContent()
// 再执行game.loop是因为原有的game.loop被await卡住了
// 得新执行一个只执行这个异步事件的game.loop
game.executingAsyncEvent=Promise.resolve().then(()=>game.loop(eventPromise))
.then(()=>{
delete game.executingAsyncEvent;
});
return game.loop(eventPromise);
}).then(()=>{
console.log(event.name, '执行完毕');
return event.name;
}));
}
else{
game.executingAsyncEventMap.set(_status.event.toEvent(),game.executingAsyncEventMap.get(_status.event.toEvent()).then(()=>{
console.log(event.name,'将要执行');
if (_status.event != eventPromise) {
eventPromise.parent = _status.event;
_status.event = eventPromise;
game.getGlobalHistory('everything').push(eventPromise);
}
return game.loop(eventPromise);
}).then(()=>{
console.log(event.name, '执行完毕');
return event.name;
}));
}
}else{
console.error(event.name,'没有执行');
console.error(_status.event.toEvent());
return event.name;
}
};
if(!game.executingAsyncEventMap){
game.executingAsyncEventMap=new Map();
}
// 没有await上个事件也就是_status.event
if(game.executingAsyncEvent) game.executingAsyncEvent.then(callback);
else callback();
if(game.executingAsyncEventMap.has(_status.event.toEvent())){
console.log(event.name,'正在等待',_status.event.name,'执行');
game.executingAsyncEventMap.set(_status.event.toEvent(),game.executingAsyncEventMap.get(_status.event.toEvent()).then(callback));
}
else{
console.log(event.name,'立即执行');
callback();
}
});
});
this.#event=event;
@ -41642,9 +41674,17 @@ new Promise(resolve=>{
else if (event.content instanceof AsyncFunction) {
// _status,lib,game,ui,get,ai六个变量由game.import提供
event.content(event, trigger, player).then(() => {
if (game.executingAsyncEventMap && game.executingAsyncEventMap.has(event.toEvent())) {
game.executingAsyncEventMap.get(event.toEvent()).then(() => {
game.executingAsyncEventMap.delete(event.toEvent());
event.finish();
resolve();
});
} else {
event.finish();
resolve();
}
});
}
else {
event.content(event, step, source, player, target, targets,