0 votes

I am trying to convert a script from GDScript into C#, but I'm having a difficult time finding out what a var is referencing.

Here's a part of the script I'm trying to convert.

    onready var collision_shape = get_node("KinematicBody/CollisionShape")
    collision_shape.shape.radius = player_radius
    collision_shape.shape.height = camera_height - player_radius
    collision_shape.transform.origin.y = (camera_height / 2.0)

Here's how I am converting it.

private CollisionShape collisionShape;
collisionShape = (CollisionShape)GetNode("KinematicBody/CollisionShape");
collisionShape.shape.radius = playerRadius;
collisionShape.shape.height = cameraHeight - playerRadius;
collisionShape.transform.origin.y = (cameraHeight / 2.0f)

However I'm getting an error.

'CollisionShape' does not contain a definition for 'shape'

I'm not sure what I should be converting collisionShape into. Is there anyway to see what a var type of object a var is referencing?

in Engine by (12 points)

1 Answer

0 votes

I can't test here now, but with the little knowledge I have about C#, try this:
collisionShape.shape -> collisionShape.Shape

Ref: https://docs.godotengine.org/en/3.1/getting_started/scripting/c_sharp/c_sharp_differences.html "As explained in the Introduction, C# generally uses PascalCase instead of the snake_case used in GDScript and C++."

by (21 points)

I understand that, maybe I didn't make my problem clear. In GDScript you can get the node of a mesh like this.

var hand_mesh = $Hand

However, when I try this in C# by doing this:

private Mesh handMesh;
handMesh = (Mesh) GetNode("HandMesh");

I get an error for trying to declare handMesh with GetNode. It says that a Node cannot be converted to a Mesh. Does this mean that the hand_mesh in GDScript isn't actually referencing a mesh? If so, what do I make my handMesh in C#?

Try this:

private MeshInstance handMesh;
handMesh = (MeshInstance) GetNode("HandMesh");

Mesh != MeshInstance

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.