I need help making this feature for my 2D platformer

Godot Version

4.2.2`

Question

I’m making a 2d pixel platformer, I want to make an item that the player can equip and drop using a button, I want it to stay where the player dropped it visibly and be able to be equipped again, I want the player to be slower, have less jump power and be able to interact with other objects while the item is equipped, how should i go about this?

  1. Create an Item Scene
    Create a new scene for the item (e.g., Item.tscn).
    This scene can be a KinematicBody2D or Area2D, depending on whether the item has collision or just needs interaction.
    Add a Sprite2D for the item’s visual and a CollisionShape2D if needed.
  2. Equip/Drop Mechanism
    In your player script, create a variable to track whether the item is equipped and another variable to store the reference to the item.
    Use input actions to handle equipping and dropping the item, such as equip_item and drop_item.
    Example in GDScript:
    gdscript
    Copy code
    var equipped_item: Node = null
    var is_equipped: bool = false

func _process(delta: float):
if Input.is_action_just_pressed(“equip_item”):
if equipped_item:
unequip_item()
else:
equip_item()

if Input.is_action_just_pressed("drop_item") and is_equipped:
    drop_item()

func equip_item():
# Find nearby items using Area2D or proximity detection logic
var nearby_items = get_overlapping_items()
if nearby_items.size() > 0:
equipped_item = nearby_items[0] # Equip the first item found
is_equipped = true
apply_item_effects()

func unequip_item():
is_equipped = false
equipped_item = null
reset_player_effects()

func drop_item():
if equipped_item:
equipped_item.global_position = global_position
is_equipped = false
equipped_item = null
reset_player_effects()
3. Modify Player Stats When Equipped
When the item is equipped, modify the player’s speed, jump power, and interaction abilities.
Example:
gdscript
Copy code
var normal_speed = 200
var equipped_speed = 100
var normal_jump_power = 500
var equipped_jump_power = 300

func apply_item_effects():
speed = equipped_speed
jump_power = equipped_jump_power
# Additional effects or interaction restrictions

func reset_player_effects():
speed = normal_speed
jump_power = normal_jump_power
# Reset other effects
4. Dropping the Item
Ensure the item remains in the world when dropped by setting its position to the player’s global_position.
5. Re-equipping Items
When the player comes near a dropped item, use an Area2D to detect proximity and allow them to re-equip the item by pressing the equip button again.
Detection Example:
gdscript
Copy code
func get_overlapping_items():
var overlapping_items =
for body in get_overlapping_bodies():
if body.is_in_group(“items”): # Assuming items are grouped
overlapping_items.append(body)
return overlapping_items
6. Interaction with Other Objects
You can add logic to prevent or allow interaction with other objects based on whether the item is equipped, such as modifying interaction inputs.
With these steps, you can create a system that allows the player to equip and drop items, with the equipped item affecting the player’s speed, jump, and interactions.

2 Likes

I’m new, but I’d suggest creating a bool variable. Program your functions to check that variable via if statements which check that variable to create differences in movement logic. The bool only swaps states when the item is dropped or retrieved.

Something like that?

I will say this was helpful, however KinematicBody2D is no longer named such in my version of Godot, I think the equivalent is CharacterBody2D.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.