Cakestax it will work and print all the results

Where's the problem then?

DaveTheCoder Yes but it won't make a difference to the problem. I literally just need a way to change those two variables from within the function. If you don't know, I have another solution I may need. Which is just to instead await GetNextShake.call() and then return the result of X and Y.

However, for this I will need to figure out how to "wait" for the function to return as it's not going to return a number instantly due to the while loop.

If you know I can wait for a function to return a value before continuing the code, please let me know!

  • xyz replied to this.

    Cakestax You should redo the whole thing without using await. This implementation you have looks too convoluted. Can you describe in plain language what exact behavior you need to implement?

      The issue here is the lambda scope, which captures the local environment. Variables are captured by value so any update doesn't apply to scope outside of the lambda; the lambda doesn't belong to the class (docs).

      Why [not] just use functions defined in the class?

      • xyz replied to this.

        spaceyjase It's not lambdas per se but the way they are used as coroutines here. It's a very confusing solution on top of being buggy. And yeah, using regular class methods and properties would be preferable, and eliminating coroutines completely would be even better.

        xyz I need my screen to shake in random positions but I made the GetNextShake() function so I could get a new random position for the next shake (BUT IT CANNOT BE THE SAME AS THE PREVIOUS SHAKE) Hence why I made it a subroutine so that it wouldn't interfere with the main thread. I am getting the next shake while the current one is still moving to it's position, that way there wouldn't be any stutters or stops while the code tries to find the next shake position.

        • xyz replied to this.

          Cakestax I still don't really understand what effect are you trying to implement. Can you give an example described in plain language without abstractions and technicalities?

          Btw subroutine and coroutine are two quite different things.

            xyz Not sure if this helps but here basically my base system

            • xyz replied to this.

              Cakestax I didn't ask about your system. The system you have is obviously causing you problems, so let's put it aside for a moment. I'm asking what effect(s) you want to produce, the final visual result you're after, a typical example. That way I can suggest you a system that may do a better job with less complications.

                xyz I want to have an easy to use function that shakes the users screen repeatedly until stopped or you can set a time limit on how long it shakes for. Also, most importantly, I don't want repeating X and Y positions for the camera movement - So it won't cause stuttering and stopping

                • xyz replied to this.

                  Cakestax Ok. I'd prefer doing this directly in _process(), but since you started designing it with coroutines here's a coroutined version. I added attenuation as well as it's a common need for shakes.
                  This is a Shake class. It's a 2D version but you can easily extend it to 3D. Try it with a sprite first.
                  You instantiate it and call start() with all needed parameters. It'll run as a coroutine until time is up or you explicitly call stop()

                  class_name Shake extends RefCounted
                  
                  var run = false
                  
                  func start(node, pivot, amplitude, speed, duration, attenuation = 1.0):
                  	run = true
                  	var time = 0.0
                  	var direction = Vector2.from_angle(randf_range(0, PI*2.0))
                  	while(run):
                  		# calculate camera position to tween to
                  		direction = direction.rotated(randf_range(PI/2.0, PI*3.0/2.0))			# rotate direction vector by random angle in range 90-270 deg
                  		var shake_offset = direction * amplitude * (1.0 - attenuation*time/duration)	# multiply direction by (attenuated) amplitude
                  		var shaken_pos = pivot + shake_offset						# absolute offset position
                  		
                  		# tween it and wait for tween to finish
                  		var t = node.get_tree().create_tween()
                  		t.tween_property(node, "position", shaken_pos, node.position.distance_to(shaken_pos) / speed )
                  		await t.finished
                  		
                  		# did we reach time limit?
                  		time += t.get_total_elapsed_time()
                  		if time > duration:
                  			break
                  			
                  	# reset to pivot position after the shake is done
                  	node.position = pivot
                  
                  	
                  func stop():
                  	run = false

                  Test usage from a sprite node:

                  extends Sprite2D
                  
                  var s
                  		
                  func _input(e):
                  	if e is InputEventMouseButton and e.is_pressed():
                  		if e.button_index == MOUSE_BUTTON_LEFT:
                  			s = Shake.new()
                  			s.start(self, self.position, 10, 800, 2)
                  		if e.button_index == MOUSE_BUTTON_RIGHT:
                  			s.stop()