Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

C# alternative for Python attribute split()

Started by ian3 Sep 29, 2021 at 7:06 PM 6 replies 10k views
Original Post
ian3
ian3

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).

Revan1985
Revan1985

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);
}
Shaarigan
Shaarigan

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

ian3
ian3

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

Edpad
Edpad

I use my own function for this.

Alberth
Alberth

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.