This repository has been archived on 2026-05-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
AgrarianGameArchive/Scripts/setup_interact_input.py
T
2026-05-14 00:08:45 +00:00

87 lines
3.0 KiB
Python

import unreal
def load(path):
asset = unreal.EditorAssetLibrary.load_asset(path)
if not asset:
raise RuntimeError(f"Could not load {path}")
return asset
def create_input_action(path):
existing = unreal.EditorAssetLibrary.load_asset(path)
if existing:
return existing
template_path = "/Game/Input/Actions/IA_Jump"
template = unreal.EditorAssetLibrary.load_asset(template_path)
if not template:
raise RuntimeError(f"Could not load input action template {template_path}")
action = unreal.EditorAssetLibrary.duplicate_asset(template_path, path)
if not action:
raise RuntimeError(f"Could not create {path}")
return action
def set_boolean_value_type(action):
# UE exposes this enum as BOOLEAN in Python for Enhanced Input actions.
action.set_editor_property("value_type", unreal.InputActionValueType.BOOLEAN)
try:
action.set_editor_property("triggers", [])
except Exception as exc:
unreal.log_warning(f"Could not clear IA_Interact triggers; keeping template defaults: {exc}")
unreal.EditorAssetLibrary.save_loaded_asset(action)
def mapping_exists(context, action, key_name):
mapping_data = context.get_editor_property("default_key_mappings")
mappings = list(mapping_data.get_editor_property("mappings"))
for mapping in mappings:
mapping_key = mapping.get_editor_property("key")
if (
mapping.get_editor_property("action") == action
and str(mapping_key.get_editor_property("key_name")) == key_name
):
return True
return False
def map_key(context, action, key_name):
if mapping_exists(context, action, key_name):
unreal.log(f"Mapping already exists: {action.get_name()} -> {key_name}")
return
key = unreal.Key()
key.set_editor_property("key_name", key_name)
mapping_data = context.get_editor_property("default_key_mappings")
mappings = list(mapping_data.get_editor_property("mappings"))
new_mapping = unreal.EnhancedActionKeyMapping()
new_mapping.set_editor_property("action", action)
new_mapping.set_editor_property("key", key)
mappings.append(new_mapping)
mapping_data.set_editor_property("mappings", mappings)
context.set_editor_property("default_key_mappings", mapping_data)
unreal.log(f"Added mapping: {action.get_name()} -> {key_name}")
def main():
interact_action = create_input_action("/Game/Input/Actions/IA_Interact")
set_boolean_value_type(interact_action)
context = load("/Game/Input/IMC_Default")
map_key(context, interact_action, "E")
map_key(context, interact_action, "Gamepad_FaceButton_Left")
unreal.EditorAssetLibrary.save_loaded_asset(context)
character_bp = load("/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter")
character_cdo = unreal.get_default_object(character_bp.generated_class())
character_cdo.set_editor_property("InteractAction", interact_action)
unreal.EditorAssetLibrary.save_loaded_asset(character_bp)
unreal.log("Agrarian interact input setup complete.")
main()