« first day (3828 days earlier)      last day (1488 days later) » 
00:00 - 21:0021:00 - 00:00

21:03
@DMGregory You said "it looks up which key that was, then calls PressKey with that information". To do that, wouldn't I use like "If the Ray hit "Qkey", call PressKey(Q)"? And then as you instructed, have the keyboard script that detects the PressKey() call and then makes a char called '\x0051', and then takes that char and any new chars and combines them into a script, which is then displayed?
Something a bit like that.
Good! I'm getting somewhere!
21:15
What do I use to say "If the Collider hit "Qkey"??
There are a couple of ways you could do it. One would be to make a Key script that just serves as a data container for a key code. When you fire your ray and get the collider the ray hit, you check if that collider has a Key script attached. If so, you read its key code variable.
Another way would be for your Keyboard script to hold a list of key codes and the collider associated with each one. Then when it gets its raycast result, it can look up the matching key code in that list (you can convert this to a dictionary to make that lookup easy)
Yes. So how do I get the collider the ray hit? I try and compiler tells me "It's read-only. You can't use this."
You try what?
You could even just use the names of the game objects that hold each collider. Name the Q key's collider object "Q" and then you can just grab raycastHit.collider.name as a string you can use. A little hacky, but simple.
I tried that. Looks like it will work. This is what I got:
An object reference is required for the non-static field, method, or property 'RaycastHit.collider'
user92578
C#'s error messages are nice because they tell you what is wrong and what you need to do
21:24
Remember, there's a difference between an instance of a type and the type as a concept.
What you are doing is asking the concept of raycast hits "did you hit anything?" - and it cannot answer you, because it's just the idea of "hits" as a category of thing.
What you need is to get an actual instance of a raycast hit - one specific impact that actually happened - from a ray that you fired with eg. Physics.Raycast()
Whenever you see "An object reference is required for the non-static..." that means you need an actual specific thing here (instance), not the category of thing (type).
Okay I used hit.collider.q
And it said that 'Collider' does not contain a definition of 'q'
What leads you to believe that the Collider type has a property called q?
Continuing the discussion of types vs instances, in C# naming conventions, types get names that start with an uppercase letter (like MonoBehaviour or Collider or GameObject), so that makes it easy to see when you're referencing a type instead of a variable that's storing an instance (which usually start with a lowercase letter or underscore)
Okay I used if (hit.collider.name = q){ and it says 'q' does not exist in the current context. FYI 'q' is the name of my Q Key.
Probably a dumb mistake on my part, though
Remember that string literals need to be inside quotation marks.
This is the same as in Python, which I think you mentioned you're already familiar with?
Yes.
But Gah! Now it says I cannot implicitly apply 'string' to 'bool'.
21:35
And remember that to do a comparison you want "==" not "="
I don't think you want to write 50 if statements, one for each letter/number/punctuation key.
Yes! Now it worked! All that's left is "The name 'PressKey' does not exist in the current context" which s fine because I was going to do as you said, which was to "have something like a Typing script that exposes a method like public void PressKey(KeyCode key)"
You can save yourself some grief with something like System.Enum.Parse(typeof(KeyCode), keyName) so you don't need 50 different if clauses to convert your strings into key codes. Or you could just pass the string through directly.
Just note that if you use the parse approach, the object names need to match the keycode enumeration names exactly. (So, uppercase Q)
Wait where are you saying that I use the parse approach?
Instead of if (hit.collider.name == "Q") { typing.PressKey(KeyCode.Q); }
You could write something like...
if (System.Enum.TryParse(hit.collider.name, true, out KeyCode code) ) {
    typing.PressKey(code);
}
This will work for all keys, and it even ignores upper/lowercase mismatches for you.
Thanks!
user92578
21:48
Damn declaring an out parameter in the call is a nifty feature
Yeah, I think we got that in C# 7?
Nice bit of syntactic sugar.
user92578
All though with C++'s functional style optional-type I suppose out parameters in C++ land are somewhat deprecated
Now I need to define the PressKey.
I keep on getting this:
typing.cs(7,35): error CS1001: Identifier expected
And here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class typing : MonoBehaviour
{
    public void PressKey(code);
}
user92578
Method parameters need a type
Again, this isn't valid C# syntax - this is what I mean about learning the language.
user92578
21:52
^
Types like "typing" should start with an uppercase letter, as I mentioned earlier.
Your method declaration should look more like public void PressKey(KeyCode code) { /*...*/ }
Okay.
Great. That issue is fixed now, and all that's left is what it thinks is an unexpected '\'
Don't include the comment.
21:56
/* ... */ is a placeholder to say "your code belongs here" - don't copy and paste it into your project.
Okay. But it keeps on telling me over and over "Identifier Expected"
Oh actually it was something else. I fixed it.
Now just to make the script that takes the KeyCode and turns it into a char and then puts it into a string.
Or you can just pass it through as a string from the start, as I mentioned earlier.
oh. Yeah. Why don't I do that?
I should.
@Wasabi C# is a case-sensitive language, which means you must type things exact, or it won’t work.
Yeah, I see that
Okay. Last dumb question: How do I take the PressKey(KeyCode code) and turn the (KeyCode code) part into whatever letter or symbol that (KeyCode code) is?
Basically how do I pull what symbol/letter KeyCode code is?
22:05
As I've said a few times now, the easier option is to make the argument a string, then you don't need to convert back and forth at all. ;)
Oh. Yeah.
For letter keys though, code.ToString() will give you the key label as an uppercase string.
Thanks.
This probably has something to do with the language, but once I turn code into string, what is this string called?
It's still called a string.
No, what is the string called? Or is the string called "string", and that is how I would refer to it.
22:19
It's called whatever name you saved it to, because that's your job.
I see.
eg. string myCodeAsString = code.ToString();
Now it's called "myCodeAsString"
Declaring variables is always something you do explicitly. They're never created as an invisible side effect of a method call.
Oh yeah!
Now how do I store the previously hit key? I know my final product should be someText = someText + newText, but how do I tell it that when the new "code" replaces the old "code", turn the old "code" into someText?
Don't you just want to add the new text to the end of your string each time the key gets pressed, even if the same key is pressed twice in a row?
Something like...
public void PressKey(KeyCode code) {
    someText = someText + code.ToString();
}
Well yes, but then it tells me that "someText" does not exist in the current context.
22:30
Right, because once again, declaring variables is your job.
They don't get magically declared for you. If you want someText as a member variable, it's up to you to write private string someText; inside your class.
Alright. But now all it tells me is that there is a } expected.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Typing : MonoBehaviour {
    public void PressKey(KeyCode code)
    {
     someText = someText + code.ToString();
     private string someText;
    }

}
There's my code. I don't understand where it wants a "}"
Member variables don't get declared inside a function. They get declared inside your class. Move private string someText; outside the PressKey method.
Oh, thank you! I can't believe I didn't catch that.
ooops
Finally!
I know I'm a broken record at this point, but none of this is stuff you need a game developer to tell you. This is bare bones intro-to-C# material you can find detailed tutorials for all over the net.
You're right.
But...
One last thing:
Do I turn my someText into TextMeshProUGUI in my Typing script, or a new one?
22:39
I think it's reasonable for Typing to hold a reference to your text mesh, and be responsible for assigning the text to it.
Yes by using something like: exampleText.text = exampleString;
Thanks.
Or, if you want to display a float use something like exampleFloat.ToString();
So float then, right?
Wait no nevermind
Yes, but their are many different number storage types
22:41
Don't confuse Wasabi. 😉
In this context, sticking to strings will cover what you need.
I know it’s a lot, but they really helped me get a better understanding of what I was working with.
@Wasabi Don’t worry though, just take your time and it will all come together. 😉
@DMGregory The funny thing is, I had no idea that there were different ways numbers or strings were stored a couple of months ago. I guess that means I’m getting better.
It blew my mind a little when I learned that Facebook implemented their own version of string to use in their C++ codebase.
22:57
Wait their own string? Why not just use a normal one?
Oh, they optimized it and made it better.
user92578
Like everything, the history of strings in C++ is long and complicated
user92578
And implementation dependant
user92578
That CppCon talk is one of my favorites, the small buffer optimization tricks are genious
00:00 - 21:0021:00 - 00:00

« first day (3828 days earlier)      last day (1488 days later) »