Currently working on a chat system for my project and want to be able to switch chat channels. Default channel is “say” and by writing /yell and pressing enter (commit) the channel would then switch to “yell”. This works.
But after testing it out, I was reminded on how World of Warcraft has multiple ways of doing it. In wow you could write /yell and press space, creating a blank space, and it would automatically make the switch. I tried replicating this by using a switch on string, hoping I could simply add /yell(blankspace) as a pin. Sadly the switch ignores the blank space and removes it.
Now I do have another solution, but it involves using a lot of if statements and starts with nodes, which could be alright if I only wanted 2 channels, but will quickly become a mess if I want 10 channels.
You could templatize it so it can work with an infinite number of channels.
Whenever your chat text box changes, check if the first character is a ‘/’, the last character is ’ ’ and the text in-between is the same as a channel.
Templatize just means to make something into a template- which is an actual thing, but not in blueprints. It’s not the best word for the situation, but the best escapes me.
Yes, you can use the starts/ends with nodes to do what I’m talking about.
You’ll need to then use the substring node to get what’s in-between.
Using the Get Substring with an start index of 1 and length of original string length -1, I would be able to isolate yell from /yell(blankspace). I then do a string switch using the Get Substring output? Does that sound solid?
That is exactly what I meant, yea.
Though it’s probably best if you don’t use a switch statement.
Depending on how you’ve implemented the channel system, that may be really easy. But there are so many different ways you could’ve implemented it that I can’t really suggest anything concrete.
Maps simply map one thing to another. These two things can be the any variable type.
Meaning you can map a string to an int, an int to an object, or an object to and object. Any type to any type. All of the keys of a map are unique. You can have multiple identical values, but you can’t have identical keys.
This is what I quickly did. Doesn’t actually work yet. Might be something to do with Starts With/Ends With/GetSubstring. Went away for a bit and have not taken a look at it.
So the first thing I’d recommend is putting all the change logic inside a function. It’s highly unlikely that you’ll only want to change the channel type by just input.
We just use a select statement off our current type.
For the actual switching, I would make a String->EChannelType map.
You can then make it blueprint read only since you don’t want this to be changed:
And fill out the defaults to be your commands mapped to which channel they should switch to:
To actually implement this map, you can just use the find node. If it found something, that means it’s a valid channel. We then clear the text box and set to the correct channel.
Just wanted to let others know incase they need the same solution. The clear text function should either be placed inside of SetChannelType or after SetChannelType has been called. Not doing this will result the string we’re using to be empty.