diff --git a/locals/Log.csv b/locals/Log.csv index 96c110d..76bc83f 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -27,6 +27,8 @@ log_loot_list_has_no_entries,ID为{0}的战利品表,没有指定条目。,"Lo log_not_within_the_loot_spawn_range,给定的数值{0}没有在战利品{1}的生成范围{2}内。,The given value {0} is not within the spawn range {2} of loot {1}.,与えられた数値{0}は戦利品{1}の生成範囲{2}内にありません。 log_loot_data_quantity,有{0}个战利品数据被返回。,{0} loot data was returned.,{0}個の戦利品データが返されます。 +log_warning_node_cannot_cast_to,创建的物品{0}无法被转型为类型{1},Created items {0} cannot be cast into type {1},作成されたアイテム {0} をタイプ {1} にキャストすることはできません。 + log_start_item_register_from_file,开始从文件注册物品信息,Start registering item information from files,アイテム情報をファイルから登録開始 log_found_files,找到{0}个文件,Found {0} files,{0}ファイルが見つかりました log_found_item_types,从文件中找到{0}个物品类型,Found {0} item types in files,ファイルから{0}個のアイテム・タイプが見つかった diff --git a/scripts/utils/NodeUtils.cs b/scripts/utils/NodeUtils.cs index 5ec5d65..270937d 100644 --- a/scripts/utils/NodeUtils.cs +++ b/scripts/utils/NodeUtils.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; + using ColdMint.scripts.debug; using ColdMint.scripts.item; using ColdMint.scripts.item.weapon; + using Godot; @@ -69,7 +71,7 @@ public static class NodeUtils /// /// public static Node2D? GetTheNearestNode(Node2D origin, Node[] array, HashSet? exclude = null, - bool excludeInvisibleNodes = true) + bool excludeInvisibleNodes = true) { var closestDistance = float.MaxValue; Node2D? closestNode = null; @@ -125,10 +127,11 @@ public static class NodeUtils return GameSceneNodeHolder.WeaponContainer; } - if (GameSceneNodeHolder.PacksackContainer!= null && childNode is Packsack) + if (GameSceneNodeHolder.PacksackContainer != null && childNode is Packsack) { return GameSceneNodeHolder.PacksackContainer; } + return defaultParentNode; } @@ -153,37 +156,68 @@ public static class NodeUtils /// 默认父节点,如果传入null,则不会将实例化后的节点放入父节点内。您需要手动放置,这对于延迟设置父节点相当有用。 /// /// - public static T? InstantiatePackedScene(PackedScene packedScene, Node? defaultParentNode = null, - bool assignedByRootNodeType = true) where T : Node + /// + public static Node InstantiatePackedScene(PackedScene packedScene, Node? defaultParentNode = null, + bool assignedByRootNodeType = true) { - try + var instantiateNode = packedScene.Instantiate(); + //An attempt is made to place an instantiated node under a parent node only after the default parent node is set. + //只有设定了默认父节点后才会尝试将实例化的节点放置到父节点下。 + if (defaultParentNode != null) { - var instantiateNode = packedScene.Instantiate(); - //An attempt is made to place an instantiated node under a husband node only after the default parent node is set. - //只有设定了默认父节点后才会尝试将实例化的节点放置到夫节点下。 - if (defaultParentNode != null) + if (assignedByRootNodeType) { - if (assignedByRootNodeType) - { - var containerNode = FindContainerNode(instantiateNode, defaultParentNode); - containerNode.AddChild(instantiateNode); - } - else - { - //If you do not need to assign by type, place it under the default parent node. - //如果不需要按照类型分配,那么将其放到默认父节点下。 - defaultParentNode.AddChild(instantiateNode); - } + var containerNode = FindContainerNode(instantiateNode, defaultParentNode); + containerNode.AddChild(instantiateNode); } + else + { + //If you do not need to assign by type, place it under the default parent node. + //如果不需要按照类型分配,那么将其放到默认父节点下。 + defaultParentNode.AddChild(instantiateNode); + } + } - return instantiateNode; - } - catch (InvalidCastException e) - { - //null is returned if the type conversion fails. - //在类型转换失败时,返回null。 - LogCat.WhenCaughtException(e); - return null; - } + return instantiateNode; + } + + /// + /// + /// Generic version of that transforms the created node to type, + /// if the created node can't be transformed to the specified type, return null and release it. + /// + /// 的泛型版本,转换创建的节点至类型,如果创建的节点无法转型至指定的类型,返回null并释放它 + /// + /// + ///This method is recommended in place of all packedScene.Instantiate<T>() calls within a project, using it to instantiate a scene, optionally assigned to a container that matches the type of the root node. + ///推荐使用此方法代替项目内所有的packedScene.Instantiate<T>()调用,使用此方法实例化场景,可选择将其分配到与根节点类型相匹配的容器内。 + /// + /// + ///packedScene + ///打包的场景 + /// + /// + ///Enabled by default, whether to place a node into a container node that matches the type of the root node after it is instantiated. If the assignment fails by type, it is placed under the default parent node. + ///默认启用,实例化节点后,是否将其放置到与根节点类型相匹配的容器节点内。如果按照类型分配失败,则放置在默认父节点下。 + /// + /// + /// Default parent. If null is passed, the instantiated node is not put into the parent. You need to place it manually, which is quite useful for delaying setting the parent node. + /// 默认父节点,如果传入null,则不会将实例化后的节点放入父节点内。您需要手动放置,这对于延迟设置父节点相当有用。 + /// + /// + /// If new node cannot cast to given type, return null + /// 如果创建的节点无法转型至指定的类型,返回null + /// + public static T? InstantiatePackedScene(PackedScene packedScene, Node? defaultParentNode = null, + bool assignedByRootNodeType = true) + where T : class + { + var node = InstantiatePackedScene(packedScene, defaultParentNode, assignedByRootNodeType); + // 检查类型转化,成功返回结果 + if (node is T result) return result; + //如果转型失败,释放所创建的节点 + LogCat.LogWarningWithFormat("warning_node_cannot_cast_to",node,nameof(T)); + node.QueueFree(); + return null; } } \ No newline at end of file