How to passing gdscript variable values to c # and vice versa.
I have nothing else to say
This worked for me.
Node setup:
CS code:
using Godot;
using System;
public class Node : Godot.Node
{
public int b = 2;
public override void _Ready()
{
GD.Print(GetTree().Root.GetChild(0).Get("a"));
}
}
GD script code:
extends Node2D
var a = 2
func _ready():
print(get_child(0).get("b"))
Just make sure that the variables are of the same type (int in this case).
Related
How can I make my method have defined list of values that can be passed. I've seen this in VB.net, but can't find it in C#, and it dosen't look like enum.
class Test
{
List { active, all, completed}
public string get(string a, List b)
{
// some code
}
string a = get("foo", active);
string b = get("foo", all);
}
If enum is called test, I need to pass test.active and I don't want that. I need to pass only active
What you can do is use the using static keyword when you want to use the enum, then you can just use the word you want.
namespace Foo
{
class Test
{
public string get(string a, List b)
{
// some code
}
}
public enum List { active, all, completed}
}
used like
using Foo;
using static Foo.List;
public void Example()
{
var test = new Test();
//Because of "using static Foo.List;" you don't need to use "List.active"
string a = test.get("foo", active);
string b = test.get("foo", all);
}
I want to get value from other cs file.
for example. there are two cs files in same Project.
one is A.cs, another is B.cs
And there is a variable in A.cs
int a = 1;
i want to use variable 'a' in B.cs, like this
int b = a;
then what should i do?
try making your variables static then you can access it any other file
Here is Explanation:
First file: A.cs
class A
{
public static int a=4;
}
Secound file: B.cs
class B
{
public int b=A.a;
}
If they are in the same namespace you will need to change the default access modifier to allow access. I believe the default is protected. More info here https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx
You would need to change int a = 1 to something like
internal class ClassA
{
internal int a = 2;
}
then you would instiate a from b
ClassA classA = new ClassA();
b = classA.a;
public class A
{
public int intA = 1;
}
now, call it in B.cs
public class B
{
A = new A();
var valueA = A.intA;
}
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
Closed 9 years ago.
Say I have a simple sample console program like below. My question is in regards to this. Is the sole use of this just so you can use the input variable name to assign to the instance variable? I was wondering what the use of this is other than used in the context of this program?
public class SimpleClass {
int numberOfStudents;
public SimpleClass(){
numberOfStudents = 0;
}
public void setStudent(int numberOfStudents){
this.numberOfStudents = numberOfStudents;
}
public void printStudents(){
System.out.println(numberOfStudents);
}
public static void main(String[] args) {
SimpleClass newRandom = new SimpleClass();
newRandom.setStudent(5);
newRandom.printStudents();
}
}
Previously, when I needed to assign a value to an instance variable name that shares similarities to the input value, I had to get creative with my naming scheme (or lack of). For example, the setStudent() method would look like this:
public void setStudent(int numberOfStudentsI){
numberOfStudents = numberOfStudentsI;
}
From that example above, it seems like using this replaces having to do that. Is that its intended use, or am I missing something?
Things are quite the opposite of how you perceive them at the moment: this is such an important and frequently used item in Java/C# that there are many special syntactical rules on where it is allowed to be assumed. These rules result in you actually seeing this written out quite rarely.
However, except for your example, there are many other cases where an explicit this cannot be avoided (Java):
referring to the enclosing instance from an inner class;
explicitly parameterizing a call to a generic method;
passing this as an argument to other methods;
returning this from a method (a regular occurrence with the Builder pattern);
assigning this to a variable;
... and more.
this is also used if you want to a reference to the object itself:
someMethod(this);
There is no alternative to this syntax (pun intended).
It's also used to call co-constructors, and for C# extension methods.
'this' simply refers to the object itself.
When the compilers looks for the value of 'numberOfStudents', it matches the 'closest' variable with this name. In this case the argument of the function.
But if you want to assign it to the class variable, you need to use the 'this.'-notation!
In the method
public void setStudent(int numberOfStudents){
this.numberOfStudents = numberOfStudents;
}
for example.
'this.numberOfStudents' references the class variable with the name 'numberOfStudents'
'numberOfStudents' references the argument of the method
So, this method simple assigns the value of the parameter to the class variable (with the same name).
in c# you use this to refer the current instance of the class object immagine you have class like this from msdn
class Employee
{
private string name;
private string alias;
private decimal salary = 3000.00m;
// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
// Printing method:
public void printEmployee()
{
Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
// Passing the object to the CalcTax method by using this:
Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
}
public decimal Salary
{
get { return salary; }
}
}
class Tax
{
public static decimal CalcTax(Employee E)
{
return 0.08m * E.Salary;
}
}
class MainClass
{
static void Main()
{
// Create objects:
Employee E1 = new Employee("Mingda Pan", "mpan");
// Display results:
E1.printEmployee();
}
}
/*
Output:
Name: Mingda Pan
Alias: mpan
Taxes: $240.00
*/
You have different scopes of variables in Java/C#. Take this example below. Although this.numberOfStudents and numberOfStudents have the same name they are not identical.
public void setStudent(int numberOfStudents){
this.numberOfStudents = numberOfStudents;
}
this.numberOfStudents is a variable called numberOfStudents that is in the instance of this class. this always points on the current instance.
public void setStudent(int numberOfStudents) that numberOfStudents is a new variable that is just available in this method.
keyword "this" refers to an object of the current class (SimpleClass) on the fly.
public class SimpleClass(){
private int a;
private int b;
private int c;
public SimpleClass(int a, int b){
this.a = a;
this.b = b;
}
public SimpleClass(int a, int b, int c){
// this constrcutor
this(a,b);
this.c = c;
}
}
Assume that I have 2 classes: A & B
public class A
{
public string p1{get;set};
public string p2{get;set};
}
public class B : A
{
public string p3{get;set};
}
I have an object 'a' from class A, I want to create an object 'b' from class B which copy all property values from 'a'. Normally, I must do like following:
B b = new B();
b.p1 = a.p1;
b.p2 = a.p2;
With this solution, I must lose many codes if I must assign manually. Is there any solutions? Thanks.
You can look into using something like http://automapper.codeplex.com/ that will automatically map properties from a source object into a destination object for you using predefined rules.
then its as simple as configuring once like so:
Mapper.CreateMap<A, B>();
And creating your new object like this:
B b = Mapper.Map<B>(a);
You can write your own hydrator, if you like to, using reflection, so it will compare object properties, names and types.
Or you can use automapper as mentioned in another answer.
The third solution is to internalize adaption in code. Since "B" knows about "A", do
public class B : A
{
public string p3{get;set};
public void Hydrate{A a}
{
this.p1 = a.p1;
this.p2 = a.p2;
}
}
Then your code will be like this
B b = new B();
b.Hydrate(a);
I am using MS ScriptControl to add scripting to my application. Everything works fine except when I have a class that is visible to the script engine that has an array. I can't access the array elements from the script.
Example C# class:
using System.Runtime.InteropServices;
namespace my_app
{
[ComVisible(true)]
public class test
{
public string this_works = "Hello";
public string[] no_workey = new string[4];
public test()
{
no_workey[0] = "I";
no_workey[1] = "can't";
no_workey[2] = "see";
no_workey[3] = "you!";
}
}
}
I add the class to the script engine:
script.AddObject("test", new test(), true);
I've built in some functionality to print an object. Here are my results:
test.this_works -> "Hello"
test.no_workey -> System.String[]
test.no_workey[0] -> 'test.no_workey.0' is null or not an object.
Does anyone know how I can do this?
Thanks!