Random banter using context free grammar?

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
Wussel
Posts: 623
Joined: July 28th, 2012, 5:58 am

Random banter using context free grammar?

Post by Wussel »

https://wiki.wesnoth.org/Context-free_grammar

I would like to make a simple in game message using this context free grammar.

Basically like 2 random strings combined.

Say Hello or Howdy and Hero or Stranger.

How do I do that? I tried and I failed. Is it even possible?

Purpose would be some more randomized dialogs for traders and such.

By the way I got the new name generator working today for custom names. Hurray, it is actually part of Wesnoth 1.14!
User avatar
Heindal
Posts: 1071
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Random banter using context free grammar?

Post by Heindal »

Sure that´s possible. I´m using it in Strange Legacy for random greetings.
First your message must contain two variables.

Code: Select all

[message]
speaker=narrator
message= "$VariableA $VariableB"
[/message]
As you know the $ means that the content of the variable is displayed.
Before displaying the message, you have to randomize these two variables with this code:

Code: Select all

[set_variable]
name=VariableA
rand=Hello,Hi,Ni Hao,Bonjour,Salut,G'day,Ahoy,Ola
[/set_variable]

[set_variable]
name=VariableB
rand=Stranger,Ranger,Hero,Dr. Mausgewitz
[/set_variable]
Be careful, as "Dr. Mausgewitz" and "Ni Hao" contains a space, which can lead to some unwanted effects, so you better keep them in quotes "".
Even so I do this, occasionally the space misses for some reason.

So the full code will be:

Code: Select all

[set_variable]
name=VariableA
rand=Hello,Hi,"Ni Hao",Bonjour,Salut,G'day,Ahoy,Ola
[/set_variable]

[set_variable]
name=VariableB
rand=Stranger,Ranger,Hero,"Dr. Mausgewitz"
[/set_variable]

[message]
speaker=narrator
message= "$VariableA $VariableB"
[/message]
You can also create a new variable "dialogue" where you set the variable


For selecting a random image you might want to use this code:

Code: Select all

[set_variable]
name=randomportrait
rand=portraits/humans/mage-red.png,portraits/humans/footpad.png
[/set_variable]

[message]
image=$randomportrait
speaker=narrator
message= "$VariableA $VariableB"
[/message]

Hopefully I could help you with that. Thanks again for supporting me with great images.
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses
Wussel
Posts: 623
Joined: July 28th, 2012, 5:58 am

Re: Random banter using context free grammar?

Post by Wussel »

Ah, I should have looked through your latest versions!

Anyway that looks very doable to me. Thanks for the precise explanations.

I actually do not really know anything about code as a language, I Just copy fragment faithfully until they work. Amazingly it gives results based on the quality of the fragments. Sometimes our programmers cannot even copy the effects. lol.

If you have an art wishlist please keep me in the loop.
User avatar
Heindal
Posts: 1071
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Random banter using context free grammar?

Post by Heindal »

I`m glad I could help. If you need help with something else, just tell me.

The "Talk" Option is an older feature, but I also overworked it and added a chance to get a "random" mission. But I think it´s in the code for years now.
At the moment I don`t need any images, but thanks :).
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses
User avatar
Celtic_Minstrel
Developer
Posts: 1666
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Random banter using context free grammar?

Post by Celtic_Minstrel »

If you did want to use the context-free grammar system for more complex messages (probably not worth it for your simple example case though), you can do so in Lua. An advantage of context-free over variables is that you can easily have variations in message structure, without adding any additional code complexity.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.
Post Reply