Giter Club home page Giter Club logo

Comments (5)

popcar2 avatar popcar2 commented on May 10, 2024 1

Thanks for looking into this. Most windows do separate keyboard input, but some games don't because they directly use Input in physics process instead of the _input functions. I'm not 100% sure if there's a way to stop a node from receiving input via the Input singleton, even with that script.

from godotos.

HVukman avatar HVukman commented on May 10, 2024

Couldn't you send the signal focus_exited() when the focus is exited. Then some boolean variable could block input.

from godotos.

popcar2 avatar popcar2 commented on May 10, 2024

Then some boolean variable could block input.

I tried to do this in game_window.gd using node.set_process_input(false), but it turns out that only disables the _input() function and doesn't stop it from using the Input singleton, which means games will probably use input anyways in process functions. As far as I know there isn't really a catch-all way to disable input.

from godotos.

PJ-568 avatar PJ-568 commented on May 10, 2024

Then some boolean variable could block input.

I tried to do this in game_window.gd using node.set_process_input(false), but it turns out that only disables the _input() function and doesn't stop it from using the Input singleton, which means games will probably use input anyways in process functions. As far as I know there isn't really a catch-all way to disable input.


My solution

My solution is to split _process(delta:float) (or _physics_process(delta:float)) into two parts:

  1. The functions which uses Input.
  2. The functions which does not use Input.

The 2nd part will run only if it is focused. And everything works just fine.

Example

Something like this:

extends StateMachine
class_name CompatibleStateMachine

func _ready():
	for child_node in get_children():
		if child_node is State:
			all_states[child_node.name] = child_node
			child_node.state_changed.connect(on_child_state_changed)
	
	if initial_state:
		initial_state.enter()
		current_state = initial_state

### 1st part ###

func _input(event: InputEvent):
	if current_state:
		current_state.handle_input_event(event)

func _unhandled_input(event: InputEvent):
	if current_state:
		current_state.handle_remaining_input_event(event)

func _process(delta: float):
	if current_state:
		current_state.update_frame(delta)

func _physics_process(delta: float):
	if current_state:
		current_state.update_physics(delta)

### 2nd part ###

func handle_input_event(event: InputEvent):
	if current_state:
		current_state.handle_input_event(event)

func handle_remaining_input_event(event: InputEvent):
	if current_state:
		current_state.handle_remaining_input_event(event)

func update_per_frame(delta: float):
	if current_state:
		current_state.update_per_frame(delta)

func update_per_physics(delta: float):
	if current_state:
		current_state.update_per_physics(delta)

Upon is a state machine which is designed to be compatible with InputManager I metioned.

Sorry that I can not share all the code since the project is confidential for now.

from godotos.

PJ-568 avatar PJ-568 commented on May 10, 2024

State example of walk:

extends CompatibleState
class_name PlayerState

@onready var machine = $".."
@onready var player = $"../.."

func enter():
	pass

func exit():
	pass

func handle_input_event(event: InputEvent):
	pass

func handle_remaining_input_event(event: InputEvent):
	pass

func update_per_frame(delta: float):
	pass

func update_per_physics(delta: float):
	if player.is_on_floor():
		elif player.direction:
			if Input.is_action_pressed("Sprint"):
				machine.change_state.emit(self, "Run")
			else:
				machine.change_state.emit(self, "Walk")
		else:
			machine.change_state.emit(self, "Idle")
	else:
		machine.change_state.emit(self, "Air")

func handle_input_event(event: InputEvent):
	pass

func handle_remaining_input_event(event: InputEvent):
	pass

func update_per_frame(delta: float):
	pass

func update_per_physics(delta: float):
	if player.is_on_floor():
		if Input.is_action_pressed("Down"):
			machine.change_state.emit(self, "Down")

extends PlayerState

func enter():
	player.appearance.walk_run(player.velocity.length())

func exit():
	player.acceleration = player.walk_acceleration

func update_per_frame(delta: float):
	origin.handle_camera_shake(delta)

func update_per_physics(delta: float):
	player.acceleration = lerp(player.acceleration, player.walk_acceleration, delta * player.acceleration_change_rate)
	if player.direction:
		player.velocity += (player.direction * player.acceleration * delta)
		player.appearance.walk_run(player.velocity.length())
	
	if player.is_on_floor():
		if Input.is_action_pressed("Down"):
			if player.velocity.length() > player.slide_start_threshold:
				machine.change_state.emit(self, "Slide")
			else:
				machine.change_state.emit(self, "Crouch")
		elif Input.is_action_just_released("Up"):
			machine.change_state.emit(self, "Jump")

Hope these help. :-)

from godotos.

Related Issues (13)

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.