AspireMint

  • Joined Dec 9, 2022
  • 5 best answers
  • mek02

    from https://docs.godotengine.org/en/stable/tutorials/physics/using_kinematic_body_2d.html :

    try moving into the obstacle at an angle and you'll find that the obstacle acts like glue - it feels like the body gets stuck
    This happens because there is no collision response. move_and_collide() stops the body's movement when a collision occurs. We need to code whatever response we want from the collision.

    Use move_and_slide . Let me fix your code:

    extends KinematicBody2D
    
    var speed = Vector2.ZERO
    var maxspeed = 5000
    var grav=200
    var desped: float = 0.1 #from value 0.0 to 1.0 (max)
    var acsped=50
    
    func _ready():
    	pass # Replace with function body.
    
    func _input(event):
    	if Input.is_action_pressed("ui_right"):
    		speed.x += acsped
    	if Input.is_action_pressed("ui_left"):
    		speed.x -= acsped
    
    func _physics_process(delta):
    	clamp(speed.x, -maxspeed, maxspeed)
    	
    	speed.x = lerp(speed.x, 0, desped)
    	speed.y = grav
    	
    	speed = move_and_slide(speed)
    	print(speed)

    EDIT: note that i did not use delta, use it here speed = move_and_slide(speed * delta) but then increase variable values (grav and acsped, I guess)

    • sahildoesstuff
      Probably BossHealth isn't "ready" - in your node tree, put breakpoint to your line BossBar.show() and do what cybereality suggest you to do. See what is at that moment in node tree.

      • sahildoesstuff
        Can you call BossBar.show() inside of _ready function and see if same error appears?
        You can get nil value if you try to call BossBar.show() inside of _init function.

        • ChromiumOS-Guy
          Some questions, some tips:
          1) How is question in title related to context?
          2) Why do you remove and add back children, if they are in node tree? They are already loaded. How is it going to speed up your app?
          3) Naming conventions - apply snake case to childofchilod variable name. Why resource is lowercase but GROUPS is uppercase.
          4) Nested loops - opportunity to split it into smaller functions.
          5) What if there is childofchildofchildofchild node? Node tree is tree. You can make small function to traverse it. Your code will be small and easy to fix.
          6) Lines of code are glued together - use empty lines. Everything is in one function - apply single responsibility principle.
          7) Shorten your line size (see 2 vertical lines in editor at character 80 and 100), for example use additional variables.
          8) Post code instead of image - higher chance someone is willing to test it.

          • Serhy
            Yes, sorry, my bad. I edited it. I was testing it with Timer.new() thats why I had to manually append it to node tree. But you already have instance of timer in node tree, so no need to add_child.

          • Serhy
            Don't forget 2 important lines:

            $AccTimer.one_shot = true
            add_child($AccTimer)

            then start your timer$AccTimer.start()

            EDIT:
            oh, timer is already in node tree, so just add this line before you start timer:
            $AccTimer.one_shot = true

            • Musicgun47
              You can move (hide) calculation of movement to other class, which will extends "abstract" class. Example:

              file movement_type.gd (autoload asMovementType):

              extends Node
              
              const CLIMBING = "movement-climbing"
              const WALKING = "movement-walking"

              file abstract_movement.gd:

              extends Node
              
              class_name AbstractMovement  # let's pretend it is abstract class :)
              
              var movement_type: String
              
              func _init(movement_type: String):
              	self.movement_type = movement_type
              
              func calc_movement(delta: float, velocity: Vector3):
              	assert(false, "Could not call abstract function")

              file climbing.gd (autoload as Climbing):

              extends AbstractMovement
              
              # autoload
              
              func _init().(MovementType.CLIMBING):
              	pass
              
              func calc_movement(delta: float, velocity: Vector3):
              	var new_velocity := Vector3.ZERO
              	# do some magic and return new velocity
              	return new_velocity

              file walking.gd (autoload as Walking):

              extends AbstractMovement
              
              # autoload
              
              func _init().(MovementType.WALKING):
              	pass
              
              func calc_movement(delta: float, velocity: Vector3):
              	var new_velocity := Vector3.ZERO
              	# do some magic and return new velocity
              	return new_velocity

              file Player.gd (see logic behind movement calculation is hidden, you just call movement.calc_movement function):

              extends KinematicBody
              
              var movement: AbstractMovement = Walking
              
              func _ready():
              	pass
              
              func _process(delta):
              	if Input.is_action_just_pressed("ui_home"):
              		print("switching movement to climbing")
              		movement = Climbing
              	elif Input.is_action_just_pressed("ui_end"):
              		print("switching movement to walking")
              		movement = Walking
              	# or if player bump into ladder, etc...
              	
              	# ...
              	
              	if movement.movement_type == MovementType.CLIMBING:
              		print("climbing mode active")
              	if movement.movement_type == MovementType.CLIMBING:
              		print("walking mode active")
              
              	# ...
              
              	var velocity = Vector3.ZERO
              	# ...
              	var new_velocity = movement.calc_movement(delta, velocity)
              	
              	move_and_slide(new_velocity)

              🙂

            • jooscher
              Yay 🙂 but you are right, editor doesn't suggest get_root (in my 3.5.stable version), but root is property of SceneTree, and root getter is get_root. Weird.

            • jooscher
              Correct me if I am wrong, autoload files are always in root node.
              To get scMain node, use one of these:

              • var instance = get_tree().get_root().get_node("scMain")
              • var instance = get_tree().get_root().get_child(1) #if you know index(position)
              • var instance = get_tree().get_root().find_node("scMain", true, false)
              • In Sign

                sign function needs float param type
                float sign(s: float)
                Your velocity type is Vector3, not float.
                It returns -1 or 0 or 1.

                • jooscher
                  Because "randomize()" is called AFTER you initiate choice1/2/3.
                  So seed for random function is same everytime you run app.
                  Fix:

                  • initiate your variables in "_ready" function after "randomize()" line
                    or
                  • change "var" to "onready var" and put "randomize()" into _init() function
                    or
                  • do "randomize()" inside of global script - singleton
                • Lethn
                  Yes, you are right, it was distance issue. Thank you!
                  If I set $Camera.far to 10000000 (That number was much bigger. Do not ask me, that number Ill keep for myself 😃) objects starts to disappearing (randomly). If I set it to 1000, everything is fine.
                  Isn't it bug? What number is MAX for camera viewing distance?

                  EDIT: specs:
                  Intel(R) Core(TM) i5-7200U CPU @ 2.5
                  HD Graphics 620
                  GeForce 940MX
                  16GiB System memory

                  • Im trying to spawn some low poly trees, but they keep disappearing in front of me.

                    Here is video:

                    Camera viewing distance is set to maximum, max Renderable elements to maximum (65k).
                    I can generate huge terrain, so memory should not be problem.
                    Tested in Windows and Linux (godot 3.5), same problem.

                    Could you help me, please? Thanks for any help.

                    • AspireMint If I set $Camera.far to 10000000 (That number was much bigger. Do not ask me, that number Ill keep for myself 😃) objects starts to disappearing (randomly). If I set it to 1000, everything is fine.

                      Yes, the near/far clipping relies on floating point (i.e. single) precision. If you up the far value you should also up the near value so as not to decimate the incremental precision.

                  • Top. Middle row looks foggy to me.
                    I can imagine parallax bg/fog - it would look like middle row, but when you fly up atmosphere is cleaner, and you will see it in nice colors (top row) 🙂