Variable Query

somecreep32somecreep32 Posts: 10Member
edited June 28 in Programming

Hello world again,

I find myself struggle on something with variables.

I have a sprite in my scene, and I am unsure of what variable to use to declare this as to access all parts of the sprite. I have snippets of what I am referencing

And yes, I am aware that object does not work for that.

Again, I appreciate all help! Thank you.

Best Answers

  • GnattyGnatty Posts: 1
    Accepted Answer

    Maybe can use variable type "Sprite".

    Sprite mySprite = GetNode<Sprite>("MySprite");

    From the manual.

    https://docs.godotengine.org/en/latest/getting_started/scripting/c_sharp/c_sharp_features.html

  • TwistedTwiglegTwistedTwigleg Posts: 2,641
    edited June 27 Accepted Answer

    @somecreep32 said:
    Actually I am getting a new error now.

    "A field initializer cannot reference the non-static field, method, or property 'Node.GetNode(NodePath)'"

    (Images removed to save space)

    Not sure what is exactly wrong, I thank you for the help this far by the way.

    You cannot get a node in the definition for the variable. You will need to define the variable as a Sprite, and then call get node later in a function like _Ready. For example:

    public class Panel : Godot.Panel {
        # other code here...
        Sprite mySprite;
        # other code here...
    
        public override void _Ready() {
            mySprite = GetNode<Sprite>("Cookie");
            # other code here...
        }
    }
    

    Then you can use it as expected.

Answers

  • GnattyGnatty Posts: 1Member
    Accepted Answer

    Maybe can use variable type "Sprite".

    Sprite mySprite = GetNode<Sprite>("MySprite");

    From the manual.

    https://docs.godotengine.org/en/latest/getting_started/scripting/c_sharp/c_sharp_features.html

  • somecreep32somecreep32 Posts: 10Member

    Awesome! I thank you sincerely, I did not know this was a thing. Are there any other variable types like this?

  • somecreep32somecreep32 Posts: 10Member

    Actually I am getting a new error now.

    "A field initializer cannot reference the non-static field, method, or property 'Node.GetNode(NodePath)'"

    Not sure what is exactly wrong, I thank you for the help this far by the way.

  • TwistedTwiglegTwistedTwigleg Posts: 2,641Admin
    edited June 27 Accepted Answer

    @somecreep32 said:
    Actually I am getting a new error now.

    "A field initializer cannot reference the non-static field, method, or property 'Node.GetNode(NodePath)'"

    (Images removed to save space)

    Not sure what is exactly wrong, I thank you for the help this far by the way.

    You cannot get a node in the definition for the variable. You will need to define the variable as a Sprite, and then call get node later in a function like _Ready. For example:

    public class Panel : Godot.Panel {
        # other code here...
        Sprite mySprite;
        # other code here...
    
        public override void _Ready() {
            mySprite = GetNode<Sprite>("Cookie");
            # other code here...
        }
    }
    

    Then you can use it as expected.

  • somecreep32somecreep32 Posts: 10Member

    Oh alright, let me try this out.

  • somecreep32somecreep32 Posts: 10Member

    Is this only limited to "_Ready"? Or am I able to set this anywhere?

  • somecreep32somecreep32 Posts: 10Member

    Actually disregard that last comment. Many thanks guys!

  • MegalomaniakMegalomaniak Posts: 2,625Admin

    Changed the topic type to a question and removed the superfluous bit from the title. You can now mark the appropriate post as an accepted answer. Note that there is no need to reject the other posts, it's just possible to accept multiple posts.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file