Scripting 继续上一讲 https://github.com/okamstudio/godot/wiki/tutorial_scripting_2

Processing 处理

Godot的各个动作事件会通过回调或者虚拟函数被激发,所以没有必要在运行时写检测触发的代码。此外,更多可以做的是动画编辑器。

但是,使用脚本在每一帧中进行处理仍然是一种很常见的情况。通常有两种情况的处理:空闲处理和固定处理。

当使用Node.set_process() 设置后,空闲处理会被激活。一旦被激活,Node._process()就会在每一帧回调。

例子:

func _ready():set_process(true)func _process(delta):[dosomething..]

delta参数描述了从上一次调用Node._process()到这一次所用的时间(浮点数float类型,以秒为单位)。固定处理也是相似的,但是仅仅需要和物理引擎同步。

一个简单的测试方法是:在一个场景中创建一个标签节点,和以下的脚本:

extends Labelvar accum=0func _ready():set_process(true)func _process(delta):accum+=deltaset_text(str(accum))
这个标签就会显示每秒钟增加的计数。
Groups 组
节点可以添加到组(按照每个节点的需要添加)。在管理大的场景的时候,这是一个简单但是有用的功能。有两个方法可以做这件事情,一是通过UI界面的组按钮Groups Button,可以看下面的图:

一个有用的例子就是绑定敌人的场景scene,如下:

func _ready():add_to_group("enemies")
这样,如果玩家潜入了秘密基地,被发现了,所有的敌人就会被警报通知到,通过使用SceneMainLoop.call_group():
func _on_discovered():get_scene().call_group(0,"guards","player_was_discovered")

上面的函数在组“guards”的所有成员上回调了方法player_was_discovered。很明显,通过调用SceneMainLoop.get_nodes_in_group()获取“guards”的节点列表是可能的:

var guards = get_scene().get_nodes_in_group("guards")

后面会有更多和SceneMainLoop相关的东西。

Notifications
Godot有一个通知的系统,但是它通常不需要在脚本中使用。因为它们很多都是低级的虚拟函数提供的接口。了解它们什么时候出现就可以了,简单的在你的脚本代码中添加Object._notification()函数就可以了:
func _notification(what):if (what==NOTIFICATION_READY):print("This is the same as overriding _ready()...")elif (what==NOTIFICATION_PROCESS):     var delta = get_process_time()print("This is the same as overriding _process()...")
但是,大多数的脚本都提供了简单的可继承的函数
Overrideable Function
正如上面提到的,最好使用这些函数。节点可以提供很多有用的可继承的函数,如下面所描述的:
func _enter_scene():pass # When the node enters the active scene, this function is called. Children nodes have not entered the active scene yet. In general, it's better to use _ready() for most cases.func _ready():pass # This function is called after _enter_scene, but it ensures that all children nodes have also entered the active scene, and they are all functional.func _exit_scene():pass # When the node exists the active scene, this function is called. Children nodes have all exited the active scene at this point.func _process(delta):pass # When set_process() is enabled, this is called every framefunc _fixed_process(delta):pass # When set_fixed_process() is enabled, this is called every physics framefunc _paused():pass #Called when game is paused, after this call, the node will not receive any more process callbacksfunc _unpaused():pass #Called when game is unpaused   

Godot教程中文版-脚本2(Scripting2)相关推荐

  1. GraPhlAn教程中文版——超炫物种树进化树绘制

    文章目录 GraPhlAn教程中文版 概述Overview 介绍Introduction 安装Installation 方法1. Bioconda快速安装 方法2. Mercurial下载 方法3. ...

  2. pytorch官方教程中文版(二)学习PyTorch

    pytorch编程环境是1.9.1+cu10.2 建议有能力的直接看官方网站英文版! 下面所示是本次教程的主要目录: pytorch官方教程中文版: PyTorch介绍 学习PyTorch 图像和视频 ...

  3. 撒花!PyTorch 官方教程中文版正式上线,激动人心的大好事!

    点击上方"AI有道",选择"星标"公众号 重磅干货,第一时间送达 什么是 PyTorch?其实 PyTorch 可以拆成两部分:Py+Torch.Py 就是 P ...

  4. 简明python教程-简明Python教程-中文版.pdf

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp计算机&nbsp>&nbspPython 简明Python教程-中文版.pdf152页 本文 ...

  5. OpenCV官方教程中文版

    OpenCV官方教程中文版(For Python) OpenCV2-Python-Tutorials 段力辉 译 说明:搬运自linux公社pdf文件,粗略搬运,仅作个人笔记参考,有时间再美化 部分文 ...

  6. Kaldi入门教程 aishell脚本

    Kaldi入门教程 aishell脚本 文章目录 Kaldi入门教程 aishell脚本 @[toc] 准备词典 准备数据,分为test.dev.train集 词典.语言文件准备,生成对应的数据关系 ...

  7. pytorch官方教程中文版(一)PyTorch介绍

    pytorch编程环境是1.9.1+cu10.2 建议有能力的直接看官方网站英文版! 下面所示是本次教程的主要目录: pytorch官方教程中文版: PyTorch介绍 学习PyTorch 图像和视频 ...

  8. python官网 中文版-python .. 官方教程中文版.pdf

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp计算机&nbsp>&nbspPython python .. 官方教程中文版.pdf105页 ...

  9. 面向开发人员的 ChatGPT 提示词教程中文版 - ChatGPT 版

    面向开发人员的 ChatGPT 提示词教程中文版 - ChatGPT 版 1. 指南 1-1. 提示的指南 1-2. 配置 1-3. 提示语原则 原则 1: 写出清晰而具体的指示 技巧 1: 使用分隔 ...

最新文章

  1. JavaScript 工作原理之五-深入理解 WebSockets 和带有 SSE 机制的HTTP/2 以及正确的使用姿势(译)...
  2. python工程师待遇-Python工程师待遇如何 玩转Python看这里
  3. 轮距和轴距有什么区别_迈巴赫S级推“婚庆版”?轴距超3.3米 612马力配全时四驱...
  4. python核心编程第三版_《Python核心编程(第3版)》
  5. 解决Windows下运行出现ModuleNotFoundError: No module named xxx问题
  6. linux find 命令详解
  7. ZooKeeper官方文档学习笔记03-程序员指南
  8. C++小游戏——24点
  9. 大数据、Hadoop、Hbase介绍
  10. 2005/2006/2007/2008/2009/2010/2012/2013/2014/2015/2016/2017/2018/2019/2020高德poi 一级类别 二级类别
  11. 知识图谱05:知识图谱构建涉及的技术
  12. socks5认证过程
  13. springboot word excel ppt 图片aspose 转换PDF 在线预览
  14. flappy bird用java实现_java实现Flappy Bird游戏源代码
  15. 尚品宅配、欧派激战整装市场
  16. disallow php,在robots.txt中Disallow: /abc和Disallow: /abc/的区别
  17. pip install pyinstaller安装报错
  18. 无线网服务器断开怎么回事,无线网自动断开怎么回事
  19. 图像处理领域顶级期刊及会议
  20. 【建议收藏】数据结构和算法面试题

热门文章

  1. 【安全狐】BURP代理及安装证书
  2. java计算机毕业设计线上甜品店售卖系统源码+系统+数据库+lw文档
  3. 【每日一词】buzzword
  4. NFS网络文件共享服务
  5. JavaScript获取变量数据类型
  6. java-php-python-ssm养老院管理系统计算机毕业设计
  7. dicom胶片展示,使用基石插件cornerstoneTools完成
  8. 批量添加用户 php,hmailserver批量添加用户
  9. PCB厂甩铜常见的原因分析
  10. 宝箱十连抽保底设计思路