ID:1397631
 
(See the best response by Ter13.)
Heya, I'm trying to figure out the proper code sequence to combine 2 strings (2 player names, to be exact), but only take the first half of the 1st string and the second half of the 2nd string.

I plan to use it in a proc

mob/proc/NameCombine(mob/M in view(1))
src.random = rand(1,2)
if(src.random ==1)
src.namecombine1 = "(this is where the code I'm looking for would be added)"
src.namecombine2 = "(same story only taking M.name)"
src.name = "[src.namecombine1][src.namecombine2]"

else
M.namecombine1 = "(same story taking M.name)"
M.namecombine2 = "(same story taking src.name)"
M.name = "[M.namecombine1][M.namecombine2]"

This code is to be used for "combining 2 players, and making the person in control be chosen by random chance"

Thanks ahead!

Best response
I edited your post to contain DM tags.

In the future, please use DM tags for any code you are going to post to the forums. It will help you quite a lot. Nobody reads code that isn't in DM tags.

As for how to copy half of a string:

var/str1 = copytext(string1,1,round(length(string1)/2))
var/str2 = copytext(string2,round(length(string2),1),0)


The length() function returns the length of either a string or a list. The round() function will round a numeric value down to the first whole number below the current decimal value. When supplied with 1 as the second argument, it will round it up.

Finally, copytext() allows you to copy a certain portion of a string from the specified offsets. Supplying 0 as the second argument equates to the end of the list.


String concatenation, on the other hand can be done in two ways:

var/str3 = "[str1][str2]"
var/str4 = str1+str2
Ahh thank you, didn't know about the DM tags, I'll keep that in mind.

as for
var/str3 = "[str1][str2]"
var/str4 = str1+str2


I was aware of this, using quotes is simply my preference as I can see it involves either a Char or a String without actually reading what it says.

Also, thanks for the lenght() function, didn't know how to use that, learned a new thing about BYOND.

Bonus Question; was lenght() taken from C++,C, or was it created for BYOND specifically?
Bonus Question; was lenght() taken from C++,C, or was it created for BYOND specifically?

It's a more or less irrelevant question.

DM is its own animal, though it operates similarly to a simplified C/C++. In programming, any and all features are utilitarian, created for the sole sake of their utility.

Thus, it's irrelevant where the idea came from, because eventually, the need for it would have presented itself, ergo its function/utility being absent would have resulted in its creation.
In response to Ter13
Ter13 wrote:
Bonus Question; was lenght() taken from C++,C, or was it created for BYOND specifically?

It's a more or less irrelevant question.

DM is its own animal, though it operates similarly to a simplified C/C++. In programming, any and all features are utilitarian, created for the sole sake of their utility.

Thus, it's irrelevant where the idea came from, because eventually, the need for it would have presented itself, ergo its function/utility being absent would have resulted in its creation.

I understand that, but I was asking because it's college related for me. (we work with C++) And as such, if taken from C++, I could apply that in schoolwork as well if I got some spare time
In DM, length() is sort of a hybrid between C++ string::length() and array::size(). In C++, there's also sizeof(), which gives you the size in bytes of a piece of data.
So for an array it would read the size of the array, which would be the last var in the array? (i.e size() for array[10] would return the value 10?)

And for lenght() string it would count the amount of characters and return it as a value?
In response to Ter13
Ter13 wrote:
String concatenation, on the other hand can be done in two ways:

> var/str3 = "[str1][str2]"
> var/str4 = str1+str2
>


Just for completeness, I'll leave this here.

var/str5 = text("[][]",str1,str2,[arg3..arg*])

In response to Dm31st3r
Yes, kinda. It's worth noting that DM has lists, not arrays. They are dynamic, can be associative etc. I can stick 11+ elements into a var/something[10], and it'll expand to accommodate. For associative lists, it'll provide the number of entries.

Such is the folly of trying to force description of one language in terms of the features of another, sadly. Try to embrace each language on it's own terms.
In response to Pirion
Pirion wrote:
Ter13 wrote:
String concatenation, on the other hand can be done in two ways:

> > var/str3 = "[str1][str2]"
> > var/str4 = str1+str2
> >

Just for completeness, I'll leave this here.

> var/str5 = text("[][]",str1,str2,[arg3..arg*])
>


I see what most of that does, except for the bracketed arg statement, what is its function?

The brackets around [arg3..arg*] are just for example I think., showing you can have as many arguments as you like. An actual call would be

var/str5 = text("[][]",str1,str2)
var/str6 = text("[][][]",str1,str2,str3)


The empty brackets inside the "" text string are placeholders. Things are subsituted in argument order. So the following are equivalent statements.

var/str5 = text("[][]",str1,str2)
var/str6 = "[str1][str2]"