0

Is it possible to modularize in GDScript?

What I have in mind is that I have a player class with variable input, of type IInput, like this:

Player.gd:

extends KinematicBody

var input = load("res://Scripts/Inputs/PlayerInput.gd").new()

func _physics_process(delta):
    if input.is_down("left_trigger"): speed = sprintSpeed
    else: speed = runSpeed

Where "res://Scripts/Inputs/PlayerInput.gd" would look like this

extends "res://Scripts/Inputs/IInput.gd"
class_name PlayerInput

var controlTranslatinos = {"left_stick_up" : "move_up",
    "left_stick_down" : "move_down",
    "left_stick_left" : "move_left",
    "left_stick_right" : "move_right",
    "right_stick_up" : "rotate_up",
    "right_stick_down" : "rotate_down",
    "right_stick_left" : "rotate_left",
    "right_stick_right" : "rotate_right",
    "x" : "attack",
    "left_trigger" : "defend",
    "a" : "jump",
    "right_trigger" : "sprint"}


func pressure(controlName):
    var translatedControl = controlTranslatinos[controlName]
    var preasure =  Input.get_action_strength(translatedControl)
    return preasure

func is_down(controlName):
    return Input.is_action_pressed(controlTranslatinos[controlName])

and where "res://Scripts/Inputs/IInput.gd" would look like this:

extends Node
class_name IInput

const controls = [ "move_up", "move_down", "move_left", "move_right", 
                "rotate_up", "rotate_down", "rotate_left", "rotate_right", 
                "attack", "defend", "jump", "sprint" ]

func pressure(controlName):
    return 0

func is_down(controlName):
    return false

Goal is to change actor's input to AIInput class and back to PlayerInput on demand. This would also be good for other stuff.

Is it possible to implement this in some other way?

Error at `PlayerInput.gd`

There's an error at first line of PlayerInput, saying: `Script not fully loaded (cyclic preload?): "res://Scripts/Inputs/IInput.gd"

I get what cyclic preload would mean, but I don't get how cyclic loading is happening at this instance. Can you explain how/why this is happening? How can I overcome it?

If this error wouldn't show up I think the modularization would work (the way I imagine it works).

Monset
  • 648
  • 5
  • 25
  • This may be due to an [engine bug](https://github.com/godotengine/godot/issues/21461). Try removing the `class_name PlayerInput` and `class_name IInput` declarations from their respective scripts. – Calinou Mar 09 '20 at 08:24
  • I just restarted godot and the error was no more (facepalm) – Monset Mar 09 '20 at 10:16

0 Answers0