Include file, heritage and interfaces

TomLaPommeTomLaPomme Posts: 19Member
edited September 16 in Programming

Hello everyone !

I use GDscript for coding and I'm trying to implement this kind of architecture :

First, I want my "Character.gd" can access (but not modify) the dictionnary contains in "Character_parameters.gd".
And then, I want to create three other KinematicBody2D which inherits from Character.gd. This way, I don't have to write again moving functions and player and clones move thogether in the same time. My questions are "is each scene a class callable by it name ?", "is there a way to implement interface or access to variable of another script?", "how can I make heritage?"
This is my severals script :

#Character_parameters.gd
const param = {
    "WEIGHT" : 5,
    "SPEED"  : 200,
    "JUMP_HEIGHT" : -200,
    "JUMP_NB" : 2
}

And in this script I wanna use param dictionary instead of declare several constants.
I don't declared it in Character.gd because, I want parameter can be modify but not Character objects.
#Character.gd
extends KinematicBody2D
var motion = Vector2();
const WEIGHT = 5;
const SPEED = 200;
const JUMP_HEIGHT = -200;
const JUMP_NB = 2;
const UP = Vector2(0,-1);
func _physics_process(delta): 
    motion.y += WEIGHT;
    if is_on_floor() : JUMP_NB =2;
    if Input.is_action_pressed("ctrl_right"):motion.x =SPEED; 
    elif Input.is_action_pressed("ctrl_left"):motion.x =-SPEED;
    else : motion.x = 0;
    if Input.is_action_pressed("ctrl_jump") and JUMP_NB !=0:
        if is_on_floor() or is_on_wall() :
            motion.y =JUMP_HEIGHT; 
    motion = move_and_slide(motion,UP);

and then coding Clone_1 and Clone_2 extends both KinematicBody2D and Character.gd
Tagged:

Tags :

Answers

  • ZenzorZenzor Posts: 6Member
    edited September 16

    extending a class, you won't need to instantiate, just call your code

    #Clone_1.gd
    extends "Character.gd"
    func _init():
    SPEED = 100

    and call your other class instantiating it

    var myclass = preload("Character_parameters.gd") 
    func _init(): 
        myclass.param

    If you want extends both KinematicBody2D and Character.gd, i reccomend you do your Character.gd extending Character_parameters.gd and your parameters class extends KinematicBody2D

Sign In or Register to comment.