Git Product home page Git Product logo

godotcallthrottled's Introduction

Godot replace call_deferred with call_throttled

func _ready() -> void:
	# Wait 1 second for the game engine to settle down
	await self.get_tree().create_timer(1).timeout

	# Setup frame budget and threshold
	var frame_budget_usec := floori(1000000 / float(Engine.get_physics_ticks_per_second()))
	var frame_budget_threshold_usec := 5000
	CallThrottled.start(frame_budget_usec, frame_budget_threshold_usec)

	# Setup callbacks
	CallThrottled.connect("waiting_count_change", Callable(self, "_on_waiting_count_change"))
	CallThrottled.connect("engine_not_busy", Callable(self, "_on_engine_not_busy"))
	CallThrottled.connect("engine_too_busy", Callable(self, "_on_engine_too_busy"))
	CallThrottled.connect("over_frame_budget", Callable(self, "_on_over_frame_budget"))

func _on_waiting_count_change(waiting_count : int) -> void:
	print("There are %s calls waiting" % [waiting_count])

func _on_engine_not_busy(waiting_count : int) -> void:
	print("Started running calls again")

func _on_engine_too_busy(waiting_count : int) -> void:
	print("Too busy to run any calls!")

func _on_over_frame_budget(used_usec : int, budget_usec : int) -> void:
	print("The current frame took %s, but the budget was %s" % [used_usec, budget_usec])

func _on_button_pressed() -> void:
	var cb := func():
		var ball := _ball_scene.instantiate()
		self.add_child(ball)

	# Replace this
	for n in 500:
		cb.call_deferred()

	# With this
	for n in 500:
		CallThrottled.call_throttled(cb)

Video

Part 1: https://www.youtube.com/watch?v=WLDM0tQ-XqE

Part 2: https://www.youtube.com/watch?v=5M8bWNO2d_0

godotcallthrottled's People

Contributors

workhorsy avatar

Stargazers

Leo Esc avatar Marek Kotewicz avatar  avatar  avatar  avatar  avatar  avatar Geoffroy Warin avatar

Watchers

 avatar  avatar

godotcallthrottled's Issues

call_throttled() can't return values.

call_throttled() can't return values, which hinders it's usage.

I am attempting to use this for a physics check in a heavy function, and that can obviously get ahead of the target time
ex:
while counter >= 0 and collision_exceeds_threshold == false: var volume = AABBCollision(RoomList[RoomList.size() - 1], RoomList[counter])
this would not work with

while counter >= 0 and collision_exceeds_threshold == false: var volume = CallThrottled.call_throttled(AABBCollision, RoomList[RoomList.size() - 1], RoomList[counter])

This is a great addon, but it could be greater with return values.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.