Language Algorithm

Anonynn
So I got this idea mainly from a game a while ago called "Chrono Cross." It had an algorithm that dynamically modified dialogue to suit the character speaking it. Meaning they would type in like...

"Hello, how are you doing this morning? You look great!"

and then they would type a character's name. Let's say, "Anaïs" --- which would change the dialogue to...

"'Ello 'ow are you doing zis morning? You look great!"

So it got me thinking. If the player could choose like...."an accent" how would you program something like that into a Quest game?

The Pixie
Hmm, so code to convert a string to an accent...

s = "Hello, how are you doing this morning? You look great!"
// Start by splitting into sentences so we can get the capitalisation right
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
// Now build new array of sentences
l = NewStringList()
foreach (t, ary) {
// for each sentence, trim whitespave and make it lower case
// (so proper names will not be handled properly)
t = Trim(LCase(t))
// now change to the accent
t = Replace(t, " h", " '")
t = Replace(t, "th", "z")
// capital at start
t = CapFirst(t)
list add (l, CapFirst(t))
}
// join sentences together and out put
msg(Join(l, " "))

Anonynn
Dammit Pix! You're a genius xD

Alright, here's another for ya! Let's see if you can solve it!

What about you having a word like...

"And" and you want to add, "like" before each "And"

"And like", for example.

or "Like totally"

Also for that Language String how would you get that to pertain to one specific character? Take that!

The Pixie
Neonayon wrote:What about you having a word like...

"And" and you want to add, "like" before each "And"

"And like", for example.

or "Like totally"

The same general approach, but with different text substitutions. Lines like this:
  t = Replace(t, " h", " '")

... become:
  if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}

Also for that Language String how would you get that to pertain to one specific character? Take that!


I would have each accent done by a different function (which would return a string), and an attribute on the character saying what function to use. Then have a function that takes the character and a string, s, as parameters:
game.stringtoprocess = s
msg(eval(character.accent + "(game.stringtoprocess)"))

Anonynn
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
l = NewStringList()
foreach (t, ary) {
t = Trim(LCase(t))
if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you totally like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}
t = CapFirst(t)
list add (l, CapFirst(t))
}
msg (Join(l, " "))



So I tried the language thing, but it doesn't seem to be working on a text. Should I make this a function, because at the moment it's in my UpdateAttributes section.

The Pixie
Yes, I would make it a function that has a string parameter.

Anonynn
Okay, I did...

Function
Return Type: String
Parameters: ((Should I type string in there?)) it's currently empty.

And then I put this code.

if (player.speciality = "fashionable") {
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
l = NewStringList()
foreach (t, ary) {
t = Trim(LCase(t))
if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you totally like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}
t = CapFirst(t)
list add (l, CapFirst(t))
}
msg (Join(l, " "))
}


Still nothing yet though x)

The Pixie
As written it does not have a return type, instead it prints to screen. Change the "msg" to "return" on the penultimate if you want a return type.

You do need a parameter, s. Parameters work differently to types. Parameters you have to say the name of it, not what it is.

Anonynn
I suppose the return isn't necessary, so I removed it and added parameter, s.

Still doesn't appear to be working though. It's hard for me to isolate the problem since this type of coding is still beyond me. Appreciate the help so far, Pix!

Just to clarify as well, is it parameter:

s

or

s.

I put s seeing as a lot of the coding for the algorithm is written with just an s

HegemonKhan
here's a link of mine, where I talk about Parameters (which are used with Functions and~or Commands):

viewtopic.php?f=10&t=5333&p=36962&hilit=Parameters#p36962
(scroll down a bit to get to the section about parameters)

here's another post about Parameters:

viewtopic.php?f=10&t=5247&p=36195&hilit=Parameters#p36195
(again, scroll down to the parameter section)

----

(I can't find my really detailed post about Parameters yet, and am too lazy to keep searching more right now)

Anonynn
That helps, but I'm not sure if Pix's parameters are different or need a special case in this case :P

For example,

Parameter: s

seems a little empty lol.

HegemonKhan
the name of a Parameter doesn't really matter, Pixie likes short VARIABLE names ;)

the only thing that matters is the position of the Parameters and Arguments. Sorry, for the programming lingo, I know you've got no idea by what I mean by 'arguments', lol. And, it's too much of a pain to try to explain it... well... maybe not...

------------------------------

<function name="test" parameters="param_pos_1, param_pos_2, param_pos_3, param_pos_X'>
VARIABLE_1 = param_pos_1
VARIABLE_2 = param_pos_2
VARIABLE_3 = param_pos_3
VARIABLE_X = param_pos_X
</function>

scripting:

test (arg_pos_1, arg_pos_2, arg_pos_3, arg_pos_X)

a very simple example: test (53, 21, 68, 99)

---------

VARIABLE_1 <===== param_pos_1 <====== arg_pos_1 <====== your_value_1
VARIABLE_2 <===== param_pos_2 <====== arg_pos_2 <====== your_value_2
VARIABLE_3 <===== param_pos_3 <====== arg_pos_3 <====== your_value_3
VARIABLE_X <===== param_pos_X <====== arg_pos_X <====== your_value_X

Anonynn
I think I sort of understand. An argument is ....how to explain it, like a change in the code or a special circumstance? I might have that completely wrong. xD As for Pix's explanation, I think it would probably be an s without the . since I imagine that would make Quest have a hissy fit. Haha.

if (player.speciality = "fashionable") {
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
l = NewStringList()
foreach (t, ary) {
t = Trim(LCase(t))
if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you totally like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}
t = CapFirst(t)
list add (l, CapFirst(t))
}
msg (Join(l, " "))
}



Even with a parameter of s though the algorithm isn't working yet. I have a feeling it's very close though!

HegemonKhan
this explanation may not make sense to you as it uses some technical terms and programming knowledge, but here's the more formal explanation:

'arguments' are what you put into the actual function call, which will be passed onto-into and be used by the function's scripting (through the function's parameters). Arguments can be direct ("literal") Values, like '5' or '10', or arguments can be Attributes~Variables too.

a function call (it's like a command-order telling that function to run~activate~execute, think of a mom tell her child to do some chore):

sum = addition_function (5,9)

or

x = 5
y = 9

sum = addition_function (x, y)

then you got the actual function definition itself, which is what the function does (it's what that chore is, is the chore taking out the trash, cleaning the room, etc, what is the chore and what does that chore entail):

<function name="addition_function" parameters="value_1, value_2" type="int">
z = value_1 + value_2
return (z)
</function>

so, what is going on here?

'5' and '9' directly (or indirectly as represented as 'x' and 'y') is sent to be used by the 'addition_function' Function, and the Function is called~activated.

The activated Function takes the arguments (5 and 9, or: x and y) and puts them into the 'value_1' and 'value_2' Parameters. So, the 'value-1' Parameter now holds the (5, or the x which holds 5) and the 'value_2' parameter now holds the (9, or the y which holds 9), and these Parameters are then able to be used by the Function's body (its scripting ~ its scripts), which is to add them together, and put that result into the 'z' temporary-local variable:

z = value_1 + value_2
// z = 5 + 9
// z = 14

lastly, 'z' (which holds 14) is now returned back to the statement (code line) where it is put into the temp-local variable 'sum', such that it is:

// sum = 14

msg ("The sum is " + sum)
// The sum is 14

--------

you don't have to have a 'returning value', but I used this for my example, to show you the full functionality of a function.

in the function definition (actually in the function definition's header line), the, type="int", part is telling quest, what type of Value it is to return: does the function return a string Value, an integer Value, a boolean Value, a double Value, an object value, a list value, a dictionary value, etc...

as this will (or should, anyways, not sure if quest's internal parsing will make this still work or if an error will actually occur) produce an error:

(Object Name: game)
Attribute Name: sum
Attribute Type: int
Attribute Value: 0

game.sum = addition_function (5, 9)

<function name="addition_function" parameters="value_1, value_2" type="string">
z = ToString (value_1 + value_2)
return (z)
</function>

the returned Value is a String: z = "14"

but the 'game.sum' Integer Attribute only holds Integers (14, NOT a string: "14"), so you should get an error (baring quest's internal parsing that may prevent the error from occuring)

Anonynn
GAHHH! Math!! You've discovered my greatest weakness....bwane....desfunction...ing...er.........xP

You're right I don't get that at all LOL!

Do you understand Pix's language algorithm and what it's missing? If so, you must be a genius as well haha!


x.X

Anonynn
Still can't get this to work, not for lack of trying though!

The Pixie
What is not working? What is the error? How is it currently set up?

Anonynn

What is not working? What is the error? How is it currently set up?



Hey Pix :)

Function: Language Algorithm
Return Type: None
Parameter: s

if (player.speciality = "bimbo") {
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
l = NewStringList()
foreach (t, ary) {
t = Trim(LCase(t))
if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you totally like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}
t = CapFirst(t)
list add (l, CapFirst(t))
}
msg (Join(l, " "))
}
else {
msg ("You aren't a bimbo!")
}
if (player.speciality = "male model") {
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
l = NewStringList()
foreach (t, ary) {
t = Trim(LCase(t))
if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you totally like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}
t = CapFirst(t)
list add (l, CapFirst(t))
}
msg (Join(l, " "))
}
else {
msg ("You aren't a really, really ridiculously good-looking like a male model!")
}

Or maybe I'm not understanding what it's supposed to do. It's supposed to change the game's text right? As it is right now, it doesn't do anything even if those specialities are picked.

The Pixie
Where are using the function? You need to use it instead of the msg function, so instead of:

msg("'Oh, hello,' you say to the golblin.")

You do:

LanguageAlgorithm("'Oh, hello,' you say to the goblin.")

The function will then kick in, and do the conversion if required, printing the text to screen. Thinking about it, it may be best to have it return a string, so you use it only what what the player actually says. So this would become:

msg("'" + LanguageAlgorithm("Oh, hello,") + "' you say to the goblin.")

You need to change the code slightly too. If the player is a bimbo return one thing, if a male model return another and if neither, return the string unaltered.
if (player.speciality = "bimbo") {
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
l = NewStringList()
foreach (t, ary) {
t = Trim(LCase(t))
if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you totally like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}
t = CapFirst(t)
list add (l, CapFirst(t))
}
return (Join(l, " "))
}
else if (player.speciality = "male model") {
s = Replace(s, ". ", ".|")
s = Replace(s, "! ", "!|")
s = Replace(s, "? ", "?|")
ary = Split(s, "|")
l = NewStringList()
foreach (t, ary) {
t = Trim(LCase(t))
if (RandomChance(50)) {
t = Replace(t, " and ", " and like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you like ")
}
else if (RandomChance(50)) {
t = Replace(t, " you ", " you totally like ")
}
else {
t = Replace(t, " and ", " and like totally ")
}
t = CapFirst(t)
list add (l, CapFirst(t))
}
return (Join(l, " "))
}
else {
return (s)
}

Anonynn
Ah, okay then! I'll give it a shot and see if it works (which I'm sure it will)! Thank you, Pix!

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums