[SOLVED] How can I add external resources with GDSCript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vmpajares
:warning: Old Version Published before Godot 3 was released.

I’m making a little to plugin for Escoria to get used to gdscript. Is a bunch of simple dialogs to get user data and generate the full tree assigning the data so I don’t need to put the info in different nodes, or forgot anything, and make the life a little easier.

It works ok, but I can’t find how to assign external scripts to the tree. If I do it with the editor, the .tscn file starts with:

[gd_scene load_steps=4 format=1]

[ext_resource path="res://globals/player.gd" type="Script" id=1]
[ext_resource path="res://scenes/testplayer_anims.gd" type="Script" id=2]

[sub_resource type="SpriteFrames" id=1]

But my code generate a .tscn that starts with:

[gd_scene load_steps=3 format=1]

[ext_resource path="res://globals/player.gd" type="Script" id=1]

[sub_resource type="SpriteFrames" id=1]

CODE:

func _on_ConfirmationDialog_confirmed():
    var dir = Directory.new()
    var scene_name = get_node("vbox/hbox1/filePath_txt").get_text()
    var telekinetic = get_node("vbox/hbox2/Telekinetic_checkbox").is_pressed()
    var anims_script = scene_name.get_base_dir() + "/"+  scene_name.get_file().basename() + "_anims.gd"
    var player

    dir.copy("res://addons/Easycoria/templates/player_anims_empty.gd", anims_script)
    player = preload("res://addons/Easycoria/templates/player.tscn").instance()
    player.set_animations(anims_script)
    player.set_telekinetic(telekinetic)
    # TODO Add anims script as external resource

    var packed_scene = PackedScene.new()
    packed_scene.pack(player)
    ResourceSaver.save(scene_name, packed_scene)

I suppose that the main problem is that I created the set_animations and set_telekinetic functions in the original player.gd script from escoria and It doesn’t add the script, only assign it. But I can’t find the code to add it.

Thanks

:bust_in_silhouette: Reply From: wombatstampede

I think I didn’t understand your code fully but my guess is that you need to add the scene to the tree (root).

I. e. look here:

get_tree().get_root().add_child(player)

And you probably need to make it the current scene if you want to see it.

get_tree().set_current_scene( player )

This is the main problem. I don’t want to work in the current scene.

I’m opening a template scene, changing values exported in its script and saving the scene with a different name. Then I can add it to different rooms as a scene child.

But the new scene isn’t ended if I can’t add the second external .gd script.

I can add more info if you need it.

vmpajares | 2017-06-20 14:56

To just save a scene it won’t have to be added to the tree or set as current IMHO.

If I see it correctly, your sample code doesn’t show which methods you actually call to assign the script. That is probably hidden in the method “set_animations”. So I can’t tell what that code is doing wrong.

Basically:
It only makes sense to set one script per node.
Object::set_script()
Object — Godot Engine (latest) documentation in English

Naturally, you can have multiple nodes in a scene and each can have an own script.
And that is probably inside set_animations.

If that is all performed correctly but not saved correctly then it might be some kind of timing/signal propagation problem. But I’m going to far ahead. First, we got to see where and how the set_script is actually done. Second, you need to check if godot puts out any warnings/error on stdout at that time.

wombatstampede | 2017-06-20 16:03

SOLVED!

You are right, I was assigning the script wrong in set_animations.

My original code was:

func set_animations(anims_script):
    animations = anims_script

And it must be:

func set_animations(anims_script):
    animations = load(anims_script)

I know that it only makes senses to set one script per node, but Escoria works this way. It had a globals/generic_element.gd (player.gd, item.gd), that the designer doesn’t need to edit, to manage the logic and a second xxx_anims.gd where the designer edits some arrays to asign the animations.

I suppose that it must be to keep the roles separated (designer, coder, gfx artist) so they work in different files.

Thanks for your time

vmpajares | 2017-06-21 07:36