0 votes

with the following code, I can access an object with a colliding raycast, get a single string and add it to a textbox.

Object A

        var test = _raycast.GetCollider().Get("testString");
        _richTextLabel.Text = test.ToString();

Object B

public string testString;
public String[] stringArray = new String[5];

    testString = "anything"

Unfortunatly, replacing "testString" by "stringArray" only seem to return a string called "System.String[]" (which I'm not able to parse to a textbox).

What I'm trying to do is to replace "testString" by an array ("stringArray" in the above example) that would be set in the inspector of object B.

How can I get that "stringArray" from object B with a raycast and store the strings contained to a variable in object A?

Thank you for your time.

Godot version 3.4 mono
in Engine by (36 points)

it looks like you defined stringArray as array of 5 strings. Did you tried to output it's value as test[0].ToString() in this case?

Yep. But, since the array from object A is empty (and not filled as I wanted by the strings from the array of object B) it crashes the game as it tries to add a "null" value "To string" (ToString()). But I can see your point, and that's what's bugging me. The following code from object A returns indeed "System.String[]" from object B :

var test = _raycast.GetCollider().Get("stringArray");

But... I am completely puzzled as of how I could extract a string from that "System.String[]".

2 Answers

0 votes
Best answer

The answer is: I-nearly-got-it-right-from-the-beginning. So, for anyone interested, the following code...

var test = _raycast.GetCollider().Get("stringArray");

... returned, with "GD.print()", as stated above, "System.String[]" (which I thought was just some kind of generic answer of the program). But it really was the array. Only, it was stored as an object. So what simply had to be done was that:

var test = _raycast.GetCollider().Get("stringArray") as String[];

And then GD.print(test[0]) will return the first string of the array.

by (36 points)
+1 vote

Maybe try that:

B b = _raycast.GetCollider() as B;
if(b != null)
    // However you can't just do that.
    // The reason is simple: `Text` is a string, `stringArray` is an array of strings.
    //_richTextLabel.Text = b.stringArray;

I'm just not sure what you want to do with your array of strings once you get it from the raycast. Do you want to concatenate the strings? Or show multiple lines where each line is one element of the array?

To do the latter, you can do that:

_richTextLabel.Text = string.Join("\n", b.stringArray)
by (28,788 points)
edited by

Thank you for your answer and time. Actually, in my game, there's a room filled with objects. Object A is the player, and objects B represents any object that can be examinated. For each object "B", there is a script attached that contains an array of strings. For example "{descriptionA1; descriptionA2;}". Back to object A, I have a dictionary (a converted .json file) that is loaded and which contains all the text lines of the game. So, what I am trying to do is to catch that array from object B through a raycast. Then I would "somehow" (and this is where I'm stuck ^^) copy it to object A. Finally, those strings could be used as a reference of which lines from the dictionary should be displayed in the textbox.

About your proposition. Isn't "B b = _raycast.GetCollider() as B;" the same as "var B = _raycast.GetCollider();"? What it returns is the reference of the object that the raycast collide with (the rigidbody in my case). Am I misunderstanding something? (Thank you again for your time)

Isn't "B b = _raycast.GetCollider() as B;" the same as "var B = _raycast.GetCollider();"?

No. var B = _raycast.GetCollider(); will not compile because GetCollider does not return type B. You have to cast it to obtain B. So you could write (B)_raycast.GetCollider(), but the result can also be null if there is no collision, and if there is, there is no guarantee it will be a B. So that's why I chose as B instead.

What it returns is the reference of the object that the raycast collide with (the rigidbody in my case)

I did this choice instead of Get("stringArray") also because it is typed and faster, contrary to the latter.

Very informative. Thank you.

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.