Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Core/GDCore/Extensions/Builtin/SceneExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,41 +222,44 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsSceneExtension(
"LoadObjectAssets",
_("Preload object"),
_("Preload an object resources in background."),
_("Preload object _PARAM1_ in background"),
_("Preload object _PARAM1_ in background (scene: _PARAM2_)"),
"",
"res/actions/hourglass_black.svg",
"res/actions/hourglass_black.svg")
.SetHelpPath("/all-features/resources-loading")
.AddCodeOnlyParameter("currentScene", "")
.AddParameter("string", _("Object name"))
.AddParameter("sceneName", _("Object scene"), "", true)
.MarkAsAdvanced();

extension
.AddAction(
"UnloadObjectAssets",
_("Unload object"),
_("Unload an object resources. The \"resource preloading\" property must be set to \"preload with an action\" for this action to actually unload resources."),
_("Unload object _PARAM1_"),
_("Unload object _PARAM1_ (scene: _PARAM2_)"),
"",
"res/actions/hourglass_black.svg",
"res/actions/hourglass_black.svg")
.SetHelpPath("/all-features/resources-loading")
.AddCodeOnlyParameter("currentScene", "")
.AddParameter("string", _("Object name"))
.AddParameter("sceneName", _("Object scene"), "", true)
.MarkAsAdvanced();

extension
.AddCondition(
"AreObjectAssetsLoaded",
_("Object preloaded"),
_("Check if object resources have finished to load in background."),
_("Object _PARAM1_ was preloaded in background"),
_("Object _PARAM1_ was preloaded in background (scene: _PARAM2_)"),
"",
"res/actions/hourglass_black.svg",
"res/actions/hourglass_black.svg")
.SetHelpPath("/all-features/resources-loading")
.AddCodeOnlyParameter("currentScene", "")
.AddParameter("string", _("Object name"))
.AddParameter("sceneName", _("Object scene"), "", true)
.MarkAsAdvanced();
}

Expand Down
6 changes: 3 additions & 3 deletions GDJS/Runtime/ResourceLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace gdjs {
const logger = new gdjs.Logger('ResourceLoader');
// TODO add a condition before each log to avoid building the message for nothing.
const debugLogger = new gdjs.Logger('ResourceLoader - debug').enable(true);
const debugLogger = new gdjs.Logger('ResourceLoader - debug').enable(false);

const addSearchParameterToUrl = (
url: string,
Expand Down Expand Up @@ -773,8 +773,8 @@ namespace gdjs {
);
// Also add the resources manually loaded for objects during the current scene.
// TODO Abort loading task to avoid to leave resources from an object that is currently loading.
const unloadedSceneObjectResourceLoadingQueue = newSceneName
? this.getObjectResourceLoadingQueue(newSceneName)
const unloadedSceneObjectResourceLoadingQueue = unloadedSceneName
? this.getObjectResourceLoadingQueue(unloadedSceneName)
: null;
if (unloadedSceneObjectResourceLoadingQueue) {
for (const objectLoadingState of unloadedSceneObjectResourceLoadingQueue.loadingStates.values()) {
Expand Down
19 changes: 13 additions & 6 deletions GDJS/Runtime/events-tools/runtimescenetools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,31 +369,38 @@ namespace gdjs {
*/
export const loadObjectOrGroupAssets = (
runtimeScene: gdjs.RuntimeScene,
objectOrGroupName: string
objectOrGroupName: string,
sceneName: string
): void => {
runtimeScene.getGame().loadObjectOrGroupAssets(objectOrGroupName);
runtimeScene
.getGame()
.loadObjectOrGroupAssets(objectOrGroupName, sceneName);
};

/**
* Unload an object assets.
*/
export const unloadObjectOrGroupAssets = (
runtimeScene: gdjs.RuntimeScene,
objectOrGroupName: string
objectOrGroupName: string,
sceneName: string
): void => {
runtimeScene.getGame().unloadObjectOrGroupAssets(objectOrGroupName);
runtimeScene
.getGame()
.unloadObjectOrGroupAssets(objectOrGroupName, sceneName);
};

/**
* Check if object assets have finished to load in background.
*/
export const areObjectOrGroupAssetsLoaded = (
runtimeScene: gdjs.RuntimeScene,
objectOrGroupName: string
objectOrGroupName: string,
sceneName: string
): boolean => {
return runtimeScene
.getGame()
.areObjectOrGroupAssetsLoaded(objectOrGroupName);
.areObjectOrGroupAssetsLoaded(objectOrGroupName, sceneName);
};
}
}
Expand Down
75 changes: 51 additions & 24 deletions GDJS/Runtime/runtimegame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,26 +917,32 @@ namespace gdjs {
/**
* Preload an object assets in background.
*/
loadObjectOrGroupAssets(objectOrGroupName: string): void {
loadObjectOrGroupAssets(
objectOrGroupName: string,
sceneName?: string
): void {
const currentScene = this._sceneStack.getCurrentScene();
if (!currentScene) {
return;
}
if (!sceneName) {
sceneName = currentScene.getName();
}
const objectGroupData = this.getObjectGroupData(
currentScene.getName(),
sceneName,
objectOrGroupName
);
if (objectGroupData) {
for (const object of objectGroupData.objects) {
this._loadObjectAssets(currentScene, object.name);
this._loadObjectAssets(sceneName, object.name);
}
} else {
this._loadObjectAssets(currentScene, objectOrGroupName);
this._loadObjectAssets(sceneName, objectOrGroupName);
}
}

private _loadObjectAssets(currentScene: RuntimeScene, objectName: string) {
const objectData = currentScene._objects.get(objectName);
private _loadObjectAssets(sceneName: string, objectName: string) {
const objectData = this.getObjectData(sceneName, objectName);
if (!objectData) {
return;
}
Expand All @@ -945,7 +951,7 @@ namespace gdjs {
return;
}
this._resourcesLoader.loadObjectResources(
currentScene.getName(),
sceneName,
objectName,
usedResources
);
Expand All @@ -954,70 +960,91 @@ namespace gdjs {
/**
* @returns true when all the resources of the given object are loaded.
*/
areObjectOrGroupAssetsLoaded(objectOrGroupName: string): boolean {
areObjectOrGroupAssetsLoaded(
objectOrGroupName: string,
sceneName?: string
): boolean {
const currentScene = this._sceneStack.getCurrentScene();
if (!currentScene) {
return false;
}
if (!sceneName) {
sceneName = currentScene.getName();
}
const objectGroupData = this.getObjectGroupData(
currentScene.getName(),
sceneName,
objectOrGroupName
);
if (objectGroupData) {
for (const object of objectGroupData.objects) {
if (
!this._resourcesLoader.areObjectAssetsReady(
currentScene.getName(),
object.name
)
!this._resourcesLoader.areObjectAssetsReady(sceneName, object.name)
) {
return false;
}
}
return true;
}
return this._resourcesLoader.areObjectAssetsReady(
currentScene.getName(),
sceneName,
objectOrGroupName
);
}

/**
* Unload an object assets.
*/
unloadObjectOrGroupAssets(objectOrGroupName: string): void {
unloadObjectOrGroupAssets(
objectOrGroupName: string,
sceneName?: string
): void {
const currentScene = this._sceneStack.getCurrentScene();
if (!currentScene) {
return;
}
if (!sceneName) {
sceneName = currentScene.getName();
}
const objectGroupData = this.getObjectGroupData(
currentScene.getName(),
sceneName,
objectOrGroupName
);
if (objectGroupData) {
for (const object of objectGroupData.objects) {
this._resourcesLoader.unloadObjectResources(
currentScene.getName(),
object.name
);
this._resourcesLoader.unloadObjectResources(sceneName, object.name);
}
} else {
this._resourcesLoader.unloadObjectResources(
currentScene.getName(),
sceneName,
objectOrGroupName
);
}
}

private getObjectData(
sceneName: string,
objectName: string
): ObjectData | null {
const sceneData = this.getSceneData(sceneName);
if (sceneData) {
for (const objectData of sceneData.objects) {
if (objectData.name === objectName) {
return objectData;
}
}
}
return null;
}

private getObjectGroupData(
sceneName: string,
objectGroupName: string
): ObjectGroupData | null {
const sceneData = this.getSceneData(sceneName);
if (sceneData) {
for (const objectGroup of sceneData.objectsGroups) {
if (objectGroup.name === objectGroupName) {
return objectGroup;
for (const objectGroupData of sceneData.objectsGroups) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if (objectGroupData.name === objectGroupName) {
return objectGroupData;
}
}
}
Expand Down
Loading
Loading