Access Script of predefined Node via C#

FabIVFabIV Posts: 3Member
edited July 2018 in Programming

Hello Guys,

currently I try to get my hands on Godot and I like it so far, but currently I have one problem with accessing a script that is attached to an AnimationPlayer-Node.

What I want to do:
Use several AnimationPlayer and give it some extra properties like Enums that can be read out by a parent node.

What I did:
Create a node with my Main-script, added a Sprite and an AnimationPlayer-Node and added a Script to the AnimationPlayer Node.

Accessing nodes works just fine and I can also access everything of my own classes but not when the Node has a Godot-functionality and an additional script is attached.

To access the node I just type in:

AnimationPlayer animPlayer = (AnimationPlayer)GetNode("name");

Then I want to access the Script and I tried

(ScriptName)animPlayer.GetScript("ScriptName");
or
(ScriptName)animPlayer.GetNode("ScriptName");
or
(ScriptName)animPlayer.GetChild(0);

In all cases I cannot acces the script.

I found a video that shows how to access Scripts with GD-Script https://www.youtube.com/watch?v=4TYh6ZLOHiU , but I cannot make it work in c#.

If you have any ideas that could help me out, I would be very greatful.

Thanks in advance.


Tags :

Best Answer

  • bluepangolin55bluepangolin55 Posts: 12
    edited July 2018 Accepted Answer

    Hey FabIV,
    Maybe using 'script' to refer to C# code is a bit misleading since C# is not a scripting language.
    I would instead think of your nodes as being objects of certain types (for example AnimationPlayer). The properties of a class are defined in its fields and the functionality is contained in its methods. Now when you add additional functionality to your animation players, you typically create a new class that inherits AnimationPlayer and add additional fields or methods to it. Let's
    call this MyAnimationPlayer and let's assume this class has an additional field x and an additional method m() that executes some additional functionality that you want to give to your animation players.

    So what you want to do is:
    1a) Anywhere in your code you create objects of the type MyAnimationPlayer and add them as children to your node tree.
    or 1b) Define AnimationPlayer nodes through the Godot Editor and attach the MyAnimationPlayer class.
    2) Access them from any other place in your code with
    MyAnimationPlayer animationPlayer = GetNode("/root/.....") as MyAnimationPlayer;
    3) Access or modify the field of that object with
    animationPlayer.x = ....
    or call the method implemented with
    animationPlayer.m() .

    I really think if you use C#, you should think in an object oriented way and not treat C# class files as scripts that can be loaded and run.

    In GD script it seems that a script that is attached to a node (just for pre-defined nodes) is used as an extension of this class.

    This should be the same in C#. If I'm not mistaken it means two things:

    • The node will be of the extended type that is declared in the C# class file. For example if we attach the MyAnimationPlayer
      class to an AnimationPlayer node, that node will be of dynamic type MyAnimationPlayer and will hence have access to all the fields and methods defined in that class.

    • Methods like _Ready(), _Process(), _Input(), etc. that you defined in MyAnimationPlayer will be executed. This is what you might call script-like behaviour.

    With (Class Name) Getnode("Node Name") I can access this class. But I do not know how to get the extension of this node ( attached script)

    Yes, this is right. And that way you have already correctly accessed the object you wanted :) It's dynamic type is the extended type. Just cast it to MyAnimationPlayer and not AnimationPlayer. You can now simply call it's methods or access it's properties using the code I wrote above. There is no 'attached script'. The object itself has access to the methods and properties that are defined by its class.

    I hope this helps.

Answers

  • duke_meisterduke_meister Posts: 11Member
    edited July 2018

    Hi, have you used C# outside of Godot? I'm not sure what you're trying to do when you say "Use several AnimationPlayer and give it some extra properties". You can create your own class (e.g. MyAnimationPlayer) that adds the properties you need and just derive from AnimationPlayer, or whatever. I'm at work and I haven't watched that video but I'll try and write an example when I'm at home.
    ed: ok I've re-read your post and I can see my answer is off the mark, I'll update it tonight.

  • duke_meisterduke_meister Posts: 11Member

    deleted

  • FabIVFabIV Posts: 3Member
    edited July 2018

    Hello duke_meister,
    First thanks for the response.
    Yes I have used c# before, but sadly I am no pro.
    An own class is what I use to manage all the properties I need. I also create a List of this container class to access all the parameters in one object, but my problem is something else:
    In GD script it seems that a script that is attached to a node (just for pre-defined nodes) is used as an extension of this class. This is at least shown in the video.
    I do not know if this is possible in c# anyway, but the script is defined with
    class name: otherclass
    Normally behind the : is written Node
    With
    (Class Name)Getnode("Node Name") I can access this class. But I do not know how to get the extension of this node ( attached script) . At the moment I just think it is a problem of name, because the script is no actual node with a name.
    But the node also tells me that the are no child's attached to this node

  • bluepangolin55bluepangolin55 Posts: 12Member
    edited July 2018 Accepted Answer

    Hey FabIV,
    Maybe using 'script' to refer to C# code is a bit misleading since C# is not a scripting language.
    I would instead think of your nodes as being objects of certain types (for example AnimationPlayer). The properties of a class are defined in its fields and the functionality is contained in its methods. Now when you add additional functionality to your animation players, you typically create a new class that inherits AnimationPlayer and add additional fields or methods to it. Let's
    call this MyAnimationPlayer and let's assume this class has an additional field x and an additional method m() that executes some additional functionality that you want to give to your animation players.

    So what you want to do is:
    1a) Anywhere in your code you create objects of the type MyAnimationPlayer and add them as children to your node tree.
    or 1b) Define AnimationPlayer nodes through the Godot Editor and attach the MyAnimationPlayer class.
    2) Access them from any other place in your code with
    MyAnimationPlayer animationPlayer = GetNode("/root/.....") as MyAnimationPlayer;
    3) Access or modify the field of that object with
    animationPlayer.x = ....
    or call the method implemented with
    animationPlayer.m() .

    I really think if you use C#, you should think in an object oriented way and not treat C# class files as scripts that can be loaded and run.

    In GD script it seems that a script that is attached to a node (just for pre-defined nodes) is used as an extension of this class.

    This should be the same in C#. If I'm not mistaken it means two things:

    • The node will be of the extended type that is declared in the C# class file. For example if we attach the MyAnimationPlayer
      class to an AnimationPlayer node, that node will be of dynamic type MyAnimationPlayer and will hence have access to all the fields and methods defined in that class.

    • Methods like _Ready(), _Process(), _Input(), etc. that you defined in MyAnimationPlayer will be executed. This is what you might call script-like behaviour.

    With (Class Name) Getnode("Node Name") I can access this class. But I do not know how to get the extension of this node ( attached script)

    Yes, this is right. And that way you have already correctly accessed the object you wanted :) It's dynamic type is the extended type. Just cast it to MyAnimationPlayer and not AnimationPlayer. You can now simply call it's methods or access it's properties using the code I wrote above. There is no 'attached script'. The object itself has access to the methods and properties that are defined by its class.

    I hope this helps.

  • FabIVFabIV Posts: 3Member
    edited August 2018

    Hello bluepangolin55
    thanks for the help. I finally could get it to work.

    @bluepangolin55 said:
    Maybe using 'script' to refer to C# code is a bit misleading since C# is not a scripting language.

    I just used this expression, because it is called that way in the editor ;)

    @bluepangolin55 said:
    2) Access them from any other place in your code with
    MyAnimationPlayer animationPlayer = GetNode("/root/.....") as MyAnimationPlayer;

    This did the trick. I did not even thought about access the new class of the node, although it is quite obvious after rethink the situation that this node now carries two classes.

    @bluepangolin55 said:

    With (Class Name) Getnode("Node Name") I can access this class. But I do not know how to get the extension of this node ( attached script)

    Yes, this is right. And that way you have already correctly accessed the object you wanted :) It's dynamic type is the extended type. Just cast it to MyAnimationPlayer and not AnimationPlayer.

    This is how it works in my normal writing style.
    For documentation purpose it would be :
    MyAnimationPlayer animationPlayer = (MyAnimationPlayer)GetNode("/root/....");

    Thanks again

Sign In or Register to comment.