江湖救急——不用现成的对象池代码,如何动态生成方块?

[复制链接]
1325 |4
脑的研发记录 发表于 2023-11-8 23:41:15 | 显示全部楼层 |阅读模式
本帖最后由 脑的研发记录 于 2023-11-8 23:41 编辑

问题描述及使用流程:
需求背景:想用栈写一个动态生成的方块堆,做一点动态变化的柱子,能够被打碎的墙啥的这样的预制体。为后面尝试用方块做章鱼触手准备,以此基础尝试写个触手攻击脚本,实验实时交互的章鱼大战。


代码需求具体说明:
0.编辑器版本:0.25.0.5
代码功能内容:
1.用脚本在世界坐标(0,0,0)以角度(0,45,0)动态生成1个方块(资源ID:197386
2.用脚本在世界坐标(200,0,0)动态生成1个球(资源ID:197388


3.物体生成完,5秒后隐藏方块和球。
PS.物体不使用现成的对象池(代码用于数据结构帖子实战emm)
完毕。

这是报错的代码

class ooh {
    n: any;
    id: string;
}
@Core.Class
export default class adds extends Core.Script {

    protected build: Array<ooh> = new Array(6);
    protected i: number = 1;


    /** 当脚本被实例后,会在第一帧更新前调用此函数 */
    protected onStart(): void {

        for (this.i; this.i < this.build.length; this.i++) {
            this.build[this.i] = new ooh();
        }
        for (this.i = 0; this.i < this.build.length; this.i++) {

            // this.build[this.i].n = Gameplay.GameObject.spawn<GameObject>({
            //     guid: "197386",
            //     replicates: true,
            // });

            // this.build[this.i].n.setCollision = true;
            // this.build[this.i].n.setvisibility = true;
            // this.build[this.i].id = "ooh" + this.i;

            let NewRoad = Gameplay.GameObject.spawn("197386", {
                replicates: true,
                transform: new Transform(new Vector(0, 0, 0),
                    new Rotation(0, 0, 0),
                    new Vector(15, 8, 0.2))
            })


        }
    }
}




API动态生成物体的代码报错,好像编辑器改版,代码格式、参数也变化了,样例代码复制粘贴也跑不起来,样例代码来源如下
ObjPool<T> | API (ark.online)

360截图20231108230148260.jpg

谢谢好心人!




回复

使用道具 举报

复读机读复读机 发表于 2023-11-8 23:51:34 | 显示全部楼层
来啦!因为现在官网上面默认都是027版本的文档了(027本次API有较大改动),所以如果版本是027以下的话可以在右上角调整文档的版本 img_v3_0250_d912591a-f2cf-4113-83c9-a5c2a4b61ceg.jpg
回复

使用道具 举报

复读机读复读机 发表于 2023-11-8 23:57:38 | 显示全部楼层
所以里面生成的代码应该是这个,记得spawn之前需要先加载资源


let obj = Gameplay.GameObject.spawn({ guid: "197386",
replicates: true,

transform: new Transform()//写需要设置的Transform
});



回复

使用道具 举报

脑的研发记录楼主 发表于 2023-11-9 09:17:13 | 显示全部楼层
复读机读复读机 发表于 2023-11-8 23:57
所以里面生成的代码应该是这个,记得spawn之前需要先加载资源

谢谢你!确实用这个跑起来了
后续再改改出新帖
  1. class ooh {
  2.     n: any;
  3.     id: string;
  4. }

  5. @Core.Class
  6. export default class adds extends Core.Script {

  7.     protected build: Array<ooh> = new Array(6);
  8.     protected i: number = 1;


  9.     /** 当脚本被实例后,会在第一帧更新前调用此函数 */
  10.     protected onStart(): void {

  11.         for (this.i=0; this.i < this.build.length; this.i++) {
  12.             this.build[this.i] = new ooh();
  13.             // this.build[this.i].n = new Core.GameObject();
  14.         }

  15.         for (this.i = 0; this.i < this.build.length; this.i++) {

  16.             let obj = Gameplay.GameObject.spawn({
  17.                 guid: "197386",
  18.                 replicates: true,
  19.                 transform: new Transform(new Vector(0, this.i*200, this.i*200),
  20.                     new Rotation(0, 0, 0),
  21.                     new Vector(1, 1, 1))
  22.                 //写需要设置的Transform
  23.             });

  24.             this.build[this.i].n =obj
  25.             this.build[this.i].n.setCollision(Type.PropertyStatus.On, true);
  26.             this.build[this.i].n.setVisibility(Type.PropertyStatus.On,true);
  27.             this.build[this.i].id = "ooh" + this.i;
  28.         }

  29.         let obj2 = Gameplay.GameObject.spawn({
  30.             guid: "197386",
  31.             replicates: true,
  32.             transform: new Transform(new Vector(0, 400, 600),
  33.                 new Rotation(0, 0, 0),
  34.                 new Vector(1, 1, 1))
  35.             //写需要设置的Transform
  36.         });

  37.         let obj3 = Gameplay.GameObject.spawn({
  38.             guid: "197386",
  39.             replicates: true,
  40.             transform: new Transform(new Vector(0, 400, 800),
  41.                 new Rotation(0, 0, 0),
  42.                 new Vector(1, 1, 1))
  43.             //写需要设置的Transform
  44.         });
  45.     }
  46. }



  47. // @Component
  48. // export default class Test extends Script {
  49. //     /** 当脚本被实例后,会在第一帧更新前调用此函数 */
  50. //     protected onStart() {
  51. //         if (SystemUtil.isServer()) {
  52. //             // 通过立方体的 guid 找到立方体
  53. //             const object = GameObject.findGameObjectById("1EB0BD86D97D7EAD");
  54. //             // 获取立方体有几个子物体(当前只有一个圆锥体)
  55. //             console.log("子物体个数:" + object.getChildren().length);
  56. //         }
  57. //     }
  58. // }
复制代码
回复

使用道具 举报

复读机读复读机 发表于 2023-11-9 10:31:21 | 显示全部楼层
脑的研发记录 发表于 2023-11-9 09:17
谢谢你!确实用这个跑起来了
后续再改改出新帖

期待一波新帖
回复

使用道具 举报

热门版块
快速回复 返回顶部 返回列表