🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C# alternative for Python attribute split()

Started by
6 comments, last by Alberth 2 years, 9 months ago

Hey everyone, I'm just started to learn C# for Unity and I'm a bit confused with a little problem. You see, I have learn a bit of Python before, and every single thing I want to interpretate is firstly coming in a form of a Python script ; I can't think with C# syntax yet. So, the thing I was thinking about was making a little text engine to display dialogues. The first thing I thought about was using coroutines, because I wanted each letter (or word) to appear at the screen with a delay, well, like in an average dialogue window. So I started with: // using System, public class MonoBehaviour blah-blah-blah

// in theory the script will be bound with an object activator already

private void OnMouseDown()

{

StartCoroutine(FirstDialogue());

}

IEnumerator FirstDialogue()

{

//and here comes the problem

}

So, what I would do in Python:

dialogueText = “Hello”

dialogueText.split("")

And the outcome would be an array of individual letters (and spaces if were some in the Text):

>>> ["H", “e”, “l”, “l”, “o”]

With that given array it would be much easier to create a cycle within that coroutine, so that I wouldn't write that:

IEnumerator FirstDialogue() // I will use just ‘print’ for example

{

print("H");

yield return new WaitForSecond(0.2f);

print("e");

yield return new WaitForSecond(0.2f);

print("l");

yield return new WaitForSecond(0.2f); //and so on…

}

So is there any way to make it easie, or should I just try to code a solution for myself? For me it's pretty challenging to code, I'm doing more stuff in 3D Modelling (Blender).

Advertisement

Maybe this could help you

https://docs.microsoft.com/en-us/dotnet/api/system.string.tochararray?view=net-5.0​

char[] letters = dialogueText.ToCharArray();
foreach (char c in letters)
{
	Console.WriteLine(c);
}

EDIT: removed the sleep to avoid confusions. Thanks Shaarigan.

None

Or you can just use directly the string, without the conversion in char array

foreach(char c in dialogueText)
{
	print(c);

	yield return new WaitForSecond(0.2f);
}

You don't need to split a string or convert it to char array. String should already inherits from IEnumerable and so you simply can use a foreach loop in C# to iterate through all the characters

foreach(char c in myString)
   doSomethingHere

!!! NEVER EVER use Thread.Sleep() in a game, expeicially in Unity where everything runs on the main thread !!!

If you want to go the coroutine way, then you should do what Revan sugested and have a call to WaitForSecond in the foreach loop. Unity e.g. the C# compiler (yes the yield keyword also exists in plain C# and this is a feature of the C# compiler, not Unity) will convert this into a safe call which will run well with Unity

@Shaarigan Thanks a lot! As I said, I don't really know all of the background and code efficiency stuff, this was helpfull!

I use my own function for this.


dialogueText = “Hello”
dialogueText.split()

You don't need to do that in Python either:

>>> for c in "Hello":
...   print(f"{c}")
... 
H
e
l
l
o

This topic is closed to new replies.

Advertisement