Help instantiating timers at the right part in the script

Godot Version

Godot 4.4

Question

Hello, I have characters set up to stick on walls when they collide with them. They are able to stick and unstick, however, when I go to move them they often(not always, if I move well) restick immediately.

I was thinking a way to fix this would be to make a new variable, can_touch, setting to true on ready, and when the character touches a wall, set it to false, then making a timer set it to true again, if it’s false. However, I have been hammering away at this and I feel like I’m stalling. I cannot figure out where to place this timer. Or really if I’m heading in the right direction at all.

I have more than one character, with separate scenes and scripts. I thought I had something nailed down but it keeps starting when I don’t want it to. And before this mess that I know doesn’t work, I also tried putting it into an on func _on_input_event but… and maybe I’m just a moron but it’s a drag and drop system, so I gotta click to move and when I click on a different character, this timer starts…so I tried moving on from that option and now, for where my timer is, I have this (not sure if I worded this question decently at all, my apologies):

if touch_wall == true:
		can_touch = false
		if can_touch == false :
			timer.start(4.5)

if prev_pos != self.global_position:
		var collision_count = get_slide_collision_count()
		if collision_count > 0:
			for i in range(collision_count):
				var collision = get_slide_collision(i)
				if collision:
					if can_touch:
                        just_touched = true
						touch_wall = true



func _on_timer_timeout() -> void:
	can_touch = true

Thank you for reading. I’ve posted a few other times this week and this community has been completely wonderful. I appreciate it.

Might need more context on the code, like what function is this sample part of? I could see this condition firing every frame would prevent the timer from depleating. If touch_wall is true, then can_touch will always be false, and the timer will always restart.

Is there any place where you set touch_wall to false?

Thank you for your reply yes so you read it spot on. This is in the character script’s physics process running so long as it is false. And I’m new and likely just the sloppiest person. So in my original comment I left out a variable, because I wasn’t sure if it would be relevant, it’s

var just_touched

in my character script and i’ll edit the original comment to include it’s placement

I have a script for my main level and I have it alternating between characters… so in my game level 1 physics process I have

	if charactera.just_touched == true:
		charactera.just_touched= false
		characterb.touch_wall = false
	elif characterb.just_touched == true:
		characterb.just_touched = false
		charactera.touch_wall = false

What’s odd is that it will work for a bit, and then fail. I think you’re right and it has something to do with everything running in the physics process

And looking at it, I definitely feel like I should be connecting things on ready and making signals but I just can’t figure when is right to do that or even like how to do it for my intended response…I am missing the signals. I tried helping someone else here like 3 mins ago and totally failed.

Think I finally saw what you were saying thank you. Moved that logic from the process function to the body area entered function and it seems to be doing what I want. Thank you for your help! You hit it on the head

1 Like