<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Input on Godot 4 レシピ</title><link>https://kamera25.github.io/godot_recipes/4.x/input/index.html</link><description>Recent content in Input on Godot 4 レシピ</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="https://kamera25.github.io/godot_recipes/4.x/input/index.xml" rel="self" type="application/rss+xml"/><item><title>Input Actions</title><link>https://kamera25.github.io/godot_recipes/4.x/input/input_actions/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/input/input_actions/index.html</guid><description>&lt;div class="box notices cstyle tip"&gt;
&lt;div class="box-label"&gt;&lt;i class="fa-fw fas fa-lightbulb"&gt;&lt;/i&gt; Tip&lt;/div&gt;
&lt;div class="box-content"&gt;
&lt;p&gt;This article is being updated from Godot 3 to Godot 4.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want to understand Godot&amp;rsquo;s &amp;ldquo;input action&amp;rdquo; system.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s say you&amp;rsquo;re making a top-down character and you write code using &lt;code&gt;InputActionKey&lt;/code&gt; that uses the arrow keys for movement. You&amp;rsquo;ll quickly find that many players prefer to use &amp;ldquo;WASD&amp;rdquo; style controls. You can go back into your code and add the additional key checks, but this would result in duplicated/redundant code.&lt;/p&gt;</description></item><item><title>Mouse Input</title><link>https://kamera25.github.io/godot_recipes/4.x/input/mouse_input/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/input/mouse_input/index.html</guid><description>&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want to detect mouse input.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;InputEventMouse&lt;/code&gt; is the base class for mouse events. It contains &lt;code&gt;position&lt;/code&gt; and &lt;code&gt;global_position&lt;/code&gt; properties. Inheriting from it are two classes: &lt;code&gt;InputEventMouseButton&lt;/code&gt; and &lt;code&gt;InputEventMouseMotion&lt;/code&gt;.&lt;/p&gt;
&lt;div class="box notices cstyle note"&gt;
&lt;div class="box-label"&gt;&lt;i class="fa-fw fas fa-exclamation-circle"&gt;&lt;/i&gt; Note&lt;/div&gt;
&lt;div class="box-content"&gt;
&lt;p&gt;You can assign mouse button events in the InputMap, so you can use them with &lt;code&gt;is_action_pressed()&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h3 id="inputeventmousebutton"&gt;&lt;code&gt;InputEventMouseButton&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;@GlobalScope.ButtonList&lt;/code&gt; contains a list of &lt;code&gt;BUTTON_*&lt;/code&gt; constants for each possible button, which will be reported in the event’s &lt;code&gt;button_index&lt;/code&gt; property. Note that the scrollwheel also counts as a button - two buttons, to be precise, with both &lt;code&gt;BUTTON_WHEEL_UP&lt;/code&gt; and &lt;code&gt;BUTTON_WHEEL_DOWN&lt;/code&gt; being separate events.&lt;/p&gt;</description></item><item><title>Adding Input Actions in code</title><link>https://kamera25.github.io/godot_recipes/4.x/input/custom_actions/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/input/custom_actions/index.html</guid><description>&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You need to add actions to the InputMap at runtime.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;Typically, you&amp;rsquo;ll add actions to the InputMap via &lt;em&gt;Project Settings&lt;/em&gt;, as shown in &lt;a href="https://kamera25.github.io/godot_recipes/4.x/input/input_actions/"&gt;Recipe: Input Actions&lt;/a&gt;. However, you may find yourself needing to add one or more actions directly in a script. The &lt;a href="https://docs.godotengine.org/en/latest/classes/class_inputmap.html" target="_blank"&gt;InputMap singleton&lt;/a&gt; has methods to help you do this.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example that would add a new action called &amp;ldquo;attack&amp;rdquo; using the space key:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-gdscript" data-lang="gdscript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#81a1c1;font-weight:bold"&gt;func&lt;/span&gt; &lt;span style="color:#88c0d0"&gt;_ready&lt;/span&gt;&lt;span style="color:#eceff4"&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#8fbcbb"&gt;InputMap&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;.&lt;/span&gt;&lt;span style="color:#88c0d0"&gt;add_action&lt;/span&gt;&lt;span style="color:#eceff4"&gt;(&lt;/span&gt;&lt;span style="color:#a3be8c"&gt;&amp;#34;attack&amp;#34;&lt;/span&gt;&lt;span style="color:#eceff4"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#81a1c1;font-weight:bold"&gt;var&lt;/span&gt; ev &lt;span style="color:#81a1c1"&gt;=&lt;/span&gt; &lt;span style="color:#8fbcbb"&gt;InputEventKey&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;.&lt;/span&gt;&lt;span style="color:#88c0d0"&gt;new&lt;/span&gt;&lt;span style="color:#eceff4"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ev&lt;span style="color:#81a1c1"&gt;.&lt;/span&gt;keycode &lt;span style="color:#81a1c1"&gt;=&lt;/span&gt; KEY_SPACE
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#8fbcbb"&gt;InputMap&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;.&lt;/span&gt;&lt;span style="color:#88c0d0"&gt;action_add_event&lt;/span&gt;&lt;span style="color:#eceff4"&gt;(&lt;/span&gt;&lt;span style="color:#a3be8c"&gt;&amp;#34;attack&amp;#34;&lt;/span&gt;&lt;span style="color:#eceff4"&gt;,&lt;/span&gt; ev&lt;span style="color:#eceff4"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you also wanted to add the left mouse button to the same action:&lt;/p&gt;</description></item><item><title>Capturing the Mouse</title><link>https://kamera25.github.io/godot_recipes/4.x/input/mouse_capture/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/input/mouse_capture/index.html</guid><description>&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want to hide the mouse cursor and keep the mouse from leaving the game window. This is common in many 3D games (and some 2D ones).&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;You can set the mouse state using &lt;code&gt;Input.mouse_mode&lt;/code&gt;. There are four possible mouse modes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MOUSE_MODE_VISIBLE&lt;/strong&gt;: The mouse is visible and can move freely into and out of the window. This is the default state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MOUSE_MODE_HIDDEN&lt;/strong&gt;: The mouse cursor is invisible, but the mouse can still move outside the window.&lt;/p&gt;</description></item><item><title>Customizing the Mouse Cursor</title><link>https://kamera25.github.io/godot_recipes/4.x/input/custom_mouse_cursor/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/input/custom_mouse_cursor/index.html</guid><description>&lt;div class="box notices cstyle tip"&gt;
&lt;div class="box-label"&gt;&lt;i class="fa-fw fas fa-lightbulb"&gt;&lt;/i&gt; Tip&lt;/div&gt;
&lt;div class="box-content"&gt;
&lt;p&gt;This article is being updated from Godot 3 to Godot 4.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want to use a custom mouse cursor.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;Setting the mouse cursor is done with &lt;code&gt;Input.set_custom_mouse_cursor()&lt;/code&gt;. All you need is a texture to use. The texture must be no larger than &lt;code&gt;256x256&lt;/code&gt; pixels in size.&lt;/p&gt;
&lt;p&gt;For example, to use the following image:&lt;/p&gt;
&lt;p&gt;
&lt;a href="#image-59fd3349521d8f88721df1523c28aed2" class="lightbox-link"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/crosshair137.png" alt="alt" style="height: auto; width: auto;" loading="lazy"&gt;
&lt;/a&gt;
&lt;a href="javascript:history.back();" class="lightbox" id="image-59fd3349521d8f88721df1523c28aed2"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/godot_recipes/3.x/img/crosshair137.png" alt="alt" class="lightbox-image" loading="lazy"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And set its hotspot to the center:&lt;/p&gt;</description></item><item><title>Mouse: Drag-select multiple units</title><link>https://kamera25.github.io/godot_recipes/4.x/input/multi_unit_select/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://kamera25.github.io/godot_recipes/4.x/input/multi_unit_select/index.html</guid><description>&lt;h2 id="problem"&gt;Problem&lt;/h2&gt;
&lt;p&gt;You want to click-and-drag to select multiple units, RTS style.&lt;/p&gt;
&lt;h2 id="solution"&gt;Solution&lt;/h2&gt;
&lt;p&gt;Realtime strategy (RTS) games often require giving orders to many units at once. A typical style of selecting multiple units is to click-and-drag a box around them. Once the units are selected, clicking on the map commands them to move.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example of what we&amp;rsquo;re going for:&lt;/p&gt;
&lt;p&gt;
&lt;a href="#image-224de033c135a72ed51d7c3be0bb3310" class="lightbox-link"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/img/multi_unit_01.gif" alt="alt" style="height: auto; width: auto;" loading="lazy"&gt;
&lt;/a&gt;
&lt;a href="javascript:history.back();" class="lightbox" id="image-224de033c135a72ed51d7c3be0bb3310"&gt;
&lt;img src="https://kamera25.github.io/godot_recipes/4.x/img/multi_unit_01.gif" alt="alt" class="lightbox-image" loading="lazy"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="unit-setup"&gt;Unit setup&lt;/h3&gt;
&lt;p&gt;To test this out, we&amp;rsquo;ll need some basic RTS-style units. They are set up to move towards a target and to avoid running into each other. We won&amp;rsquo;t go into too much detail on them in this tutorial. The unit script is commented if you&amp;rsquo;d like to use it as a base for creating your own RTS units. See below for a link to download the project.&lt;/p&gt;</description></item></channel></rss>