Fix objects created by custom actions not being picked (#8575)

This commit is contained in:
D8H
2026-05-07 17:48:22 +02:00
committed by GitHub
parent edc26bdcbf
commit 1480595463
3 changed files with 70 additions and 30 deletions
@@ -667,30 +667,29 @@ gd::String EventsCodeGenerator::GenerateEventsFunctionContext(
// TODO: we could speed this up by storing a map of object names, but
// the cost of creating/storing it for each events function might not
// be worth it.
" if (objectsList) {\n"
" if (parentEventsFunctionContext && !(scopeInstanceContainer &&\n"
" scopeInstanceContainer.isObjectRegistered(objectName))) {\n"
" if (objectsList) {\n" +
" const object = parentEventsFunctionContext && "
// The object is not a child object and is passed by parameter from a
// parent instance container.
" const object = parentEventsFunctionContext.createObject(objectsList.firstKey());\n"
" if (object) {\n"
"!(scopeInstanceContainer && "
"scopeInstanceContainer.isObjectRegistered(objectName)) ?\n" +
" "
"parentEventsFunctionContext.createObject(objectsList.firstKey()) "
":\n" +
" runtimeScene.createObject(objectsList.firstKey());\n" +
" if (object) {\n" +
// Add the new instance to object lists
" objectsList.get(objectsList.firstKey()).push(object);\n"
" eventsFunctionContext._objectArraysMap[objectName].push(object);\n"
" }\n"
" return object;\n"
" } else {\n"
// The object is not a child object
" const object = runtimeScene.createObject(objectsList.firstKey());\n"
" if (object) {\n"
// Add the new instance to object lists. `_objectsMap` and
// `_objectArraysMap` contains the same Array instance so we only add
// it once
" eventsFunctionContext._objectArraysMap[objectName].push(object);\n"
" }\n"
" return object;\n"
" }\n"
" }\n"
" objectsList.get(objectsList.firstKey()).push(object);\n" +
// For child-objects _objectsMap` and `_objectArraysMap` contains the
//same Array instance so we only add it once.
" if (!(scopeInstanceContainer && "
"scopeInstanceContainer.isObjectRegistered(objectName))) {\n" +
" "
"eventsFunctionContext._objectArraysMap[objectName].push(object);\n" +
" }\n" + //
" }\n" + //
" return object;\n" + //
" }\n" +
// Unknown object, don't create anything:
" return null;\n" +
" },\n"
+35
View File
@@ -580,6 +580,15 @@ class CustomRuntimeObject2D extends RuntimeObject {
constructor(parentInstanceContainer, objectData) {
super(parentInstanceContainer, objectData);
this._instanceContainer = parentInstanceContainer;
const variantData = this._instanceContainer
.getGame()
.getEventsBasedObjectData(objectData.type);
if (variantData) {
for (const childObjectData of variantData.objects) {
this._instanceContainer.registerObject(childObjectData);
}
}
}
onCreated() {}
}
@@ -776,6 +785,7 @@ class RuntimeGame {
gameData && gameData.variables
);
this._variablesByExtensionName = new Map();
this._eventsBasedObjectDatas = new Map();
if (gameData) {
for (const extensionData of gameData.eventsFunctionsExtensions) {
if (extensionData.globalVariables.length > 0) {
@@ -785,6 +795,16 @@ class RuntimeGame {
);
}
}
if (gameData.eventsFunctionsExtensions) {
for (const extension of gameData.eventsFunctionsExtensions) {
for (const eventsBasedObject of extension.eventsBasedObjects) {
this._eventsBasedObjectDatas.set(
extension.name + '::' + eventsBasedObject.name,
eventsBasedObject
);
}
}
}
}
}
@@ -795,6 +815,17 @@ class RuntimeGame {
getVariablesForExtension(extensionName) {
return this._variablesByExtensionName.get(extensionName) || null;
}
getEventsBasedObjectData(type) {
const eventsBasedObjectData = this._eventsBasedObjectDatas.get(type);
if (!eventsBasedObjectData) {
console.error(
'The game has no events-based object of the type "' + type + '"'
);
return null;
}
return eventsBasedObjectData;
}
}
/** A minimal implementation of gdjs.RuntimeScene for testing. */
@@ -843,6 +874,10 @@ class RuntimeScene {
this._instances[objectData.name] = [];
}
isObjectRegistered(objectName) {
return !!this._objects[objectName];
}
createObject(objectName) {
if (!this._instances[objectName]) this._instances[objectName] = [];
@@ -956,20 +956,26 @@ function generatedCustomObject(
gameData: JSON.parse(gd.Serializer.toJSON(serializedProjectElement)),
sceneData: JSON.parse(gd.Serializer.toJSON(serializedSceneElement)),
});
const objectType =
eventsFunctionsExtension.getName() + '::' + eventsBasedObject.getName();
const CompiledRuntimeCustomObject = generateCompiledEventsForEventsBasedObject(
gd,
project,
eventsFunctionsExtension,
eventsBasedObject,
gdjs,
options
);
const CompiledRuntimeCustomObject =
generateCompiledEventsForEventsBasedObject(
gd,
project,
eventsFunctionsExtension,
eventsBasedObject,
gdjs,
options
);
serializedProjectElement.delete();
serializedSceneElement.delete();
project.delete();
const object = new CompiledRuntimeCustomObject(runtimeScene, { content: {} });
const object = new CompiledRuntimeCustomObject(runtimeScene, {
type: objectType,
content: {},
});
return { gdjs, runtimeScene, object };
}