Is there a way to "individualize" my Scenes and interact one Node with a Node of another Scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lucasfazzi

So, I made a scene with a main 2Dnode called Characters;
Below, I put 2 Knematic2D nodes: Player and Enemy 1 (print1: Imgur: The magic of the Internet
);

I made the movement scripts to Player 1 ( print2: Imgur: The magic of the Internet ) and Enemy 1(print3: Imgur: The magic of the Internet ); Enemy 1 follows Player with a ‘get_parent().get_node()’ method.

But I was thinking; is there any way to individualize this with their own scenes?
Maybe a Player Scene and a Enemy 1 Scene? If I individualize, how can I link the scripts without a parent?

I was thinking maybe put a Level1 scene and inherit Player Scene + Enemy 1 Scene to make easy in the future, every Node with their respective childs… but how can i link Enemy 1 script to get Player info? How can I make Enemy 1 recognize Player withou get_parent().get_node()?

I’m a lil but noob and I don’t know how can I make this. Trying figure out with Class or add_to_group() but i don’t thhink if is the best way.

I don’t know why but image links is unstable and keeps crashing in imgur

lucasfazzi | 2019-01-21 22:39

Try to format your question in a better way next time, this was a simple one and was hard to understand, if it was complex, it would be almost impossible. Here are a few tips to write a better question:

  • When adding code, o use `code` to short terms to do this os use the code formatting (beside the image icon) when writing more than one line of code
  • When adding images, use the image icon so who is trying to help you be able to the images here (remember to get the image url - left button>open in a new tab)

fpicoral | 2019-01-22 01:08

:bust_in_silhouette: Reply From: fpicoral

About the scenes

You can instantiate scenes. To do that, you need to click on that chain icon, beside the plus icon on the top of the scene tree.

enter image description here

About the scripts

  • If you want the exact same script in multiple scenes, you can use a
    Singleton
  • If you wan’t a base script for multiple scenes, you can inherit
    scripts from others.

Doing that, the inherited script will have access to all methods and variables of the parent but it also can have it own methods and variables, without sharing them with the parent. To do that, in the very top of the script, change the extends Object to extends "res://path/to/parent.gd".

You can also easily do that when creating a new script by browsing the parent script using the folder icon:

More

It’s a good idea to use at the same time singletons and inherited scripts, so you won’t need to copy and paste your code that is comon in a bunch of different scripts and, by using singletons, you won’t deppend on your scene tree.

What I said above may be a litte confusing, but come with me:

Let’s suppose that you have your Player.tscn scene and you are instantiating it at you Level1.tscn scene.
Your Level1.tscn scene has the following tree:

- Level1 (root node)
- Player
- Enemy 

And in your Player.tscn root node you have the following code:

extends KinematicBody2D

onready var enemy = get_parent().get_node("Enemy")

It will work fine BUT, imagine if you renamed the “Enemy” node or changed your scene tree to someting like:

- Level1 (root node)
- Player
- Enemies (container)
  - Enemy 

It would not work anymore because you were getting the node “Enemy” for that scene tree.
To avoid that, you can use global scripts (singletons).

Let’s suppose that you created the singleton Global.gd and added the var enemy but did not set it to any value.
What you could do was, in the Enemy script, add the following code:

func _ready():
    Global.enemy = self

Doing that, you can access this node from ANYWHERE in your game just by typing Global.enemy.

Read the singleton docs for more info on how to create one.

As you are very new to Godot, I would recommend the course made by GameDev available on Udemy.
I saw it myself and it’s really great. There are also great tutorials on Youtube for free, such as the channel KidsCanCode.
There are also great tutorials on the Godot Docs.
Take a look at them and you will improve your GDScript skills very fast.

If you have more doubts, please ask!
If this question solved your problem, please set it as the best answer and remember to help others to keep the community running.

Sorry about format, my bad.

But your help solved my problem. The Singletons are the best way for that what I think.

About the course; I started a few days the GameDev on Udemy, I’m enjoying the lectures, but I don’t think I reached Singleton parts already. And I’ll take a look in KidsCanCode youtube!

Thanks for the help!

lucasfazzi | 2019-01-22 18:54