Hi, instead of having a single script handling all actions from a character, i want to spread actions across diferents scripts or nodes for readability, so far i've tried this methods without luck.
Character (kinematicbody)
Sprite (sprite)
Motion(kinematicbody)
first one is based on inheritance
This is the Character node code
extends KinematicBody2D
#this is character node
var joypad = { D = null, L = null, R = null, U = null }
var motion = Vector2()
var veloct = 7.5
func _ready():
pass
This is the Motion node code
extends "Character.gd"
func _ready():
pass
func _physics_process(delta):
get_Action()
set_Action()
pass
func get_Action():
joypad.U = Input.is_action_pressed("ui_up")
joypad.D = Input.is_action_pressed("ui_down")
joypad.L = Input.is_action_pressed("ui_left")
joypad.R = Input.is_action_pressed("ui_right")
pass
func set_Action():
motion.x = int(joypad.R) - int(joypad.L)
motion.y = int(joypad.U) - int(joypad.D)
motion = motion.normalized() * veloct
motion = move_and_slide(motion)
pass
It didn't work, if you add a print(motion) inside set_Action() you get some values but Character node isn't moving, in fact what's moving it's the Motion node itself.
-
Second one is this
This is the Character node code
extends KinematicBody2D
#this is character node
var joypad = { D = null, L = null, R = null, U = null }
var motion = Vector2()
var veloct = 7.5
func _ready():
pass
This is the Motion node code
extends KinematicBody2D
var parent = null
func _ready():
parent = get_parent()
pass
func _physics_process(delta):
get_Action()
set_Action()
pass
func get_Action():
parent.joypad.U = Input.is_action_pressed("ui_up")
parent.joypad.D = Input.is_action_pressed("ui_down")
parent.joypad.L = Input.is_action_pressed("ui_left")
parent.joypad.R = Input.is_action_pressed("ui_right")
pass
func set_Action():
parent.motion.x = int(parent.joypad.R) - int(parent.joypad.L)
parent.motion.y = int(parent.joypad.U) - int(parent.joypad.D)
parent.motion = parent.motion.normalized() * parent.veloct
parent.motion = parent.move_and_slide(parent.motion)
pass
It didnt work same as above
-
third one, more spaghetti oriented and more uglier cuz im poluting Character with methods, thing i wanted to avoid.
This is the Character node code
extends KinematicBody2D
#this is character node
var joypad = { D = null, L = null, R = null, U = null }
var motion = Vector2()
var veloct = 7.5
var child = null
func _ready():
child = $Motion
pass
func _physics_process(delta):
child.get_Action()
child.set_Action()
pass
This is the Motion node code
extends KinematicBody2D
var parent = null
func _ready():
parent = get_parent()
pass
func get_Action():
parent.joypad.U = Input.is_action_pressed("ui_up")
parent.joypad.D = Input.is_action_pressed("ui_down")
parent.joypad.L = Input.is_action_pressed("ui_left")
parent.joypad.R = Input.is_action_pressed("ui_right")
pass
func set_Action():
parent.motion.x = int(parent.joypad.R) - int(parent.joypad.L)
parent.motion.y = int(parent.joypad.U) - int(parent.joypad.D)
parent.motion = parent.motion.normalized() * parent.veloct
parent.motion = parent.move_and_slide(parent.motion)
pass
More of the same, im moving Motion node instead of Character node, is there any option out there without having to declare functions inside the Character node