So I'm trying to make a title generator for one of my games, and I want to essentially have a few formats for sentences, but I'm not quite sure how to go about it... I'm trying to have them grouped together in a table (For a function I'll create later), but I'm not entirely sure how to do that.
Sorry if I'm not explaining this well, post a comment if you're confused
1 | local nouns = { "dogs" , "cats" } |
2 | local verbs = { "run" , "walk" } |
3 | local adjectives = { "fast" , "slow" } |
4 | local titleformats = { nouns+verbs+adjectives,nouns+verbs } -- This is where I'm not sure what to do... |
Hope this helps!
Hi. On line 4 you are trying to add the tables which is not possible, you can only add numbers together. Also you want to select a random value from the table? To do so you have to get a random value from the array uses array[math.random(1, #array)]
the brackets indicate getting a value from the array and the random function is going to choose 1 of all items in it.
Also to "add" strings; simply concatenate them using two periods as so print("Hello" .. " " .. "World!")
.
01 | local nouns = { "dogs" , "cats" } |
02 | local verbs = { "run" , "walk" } |
03 | local adjectives = { "fast" , "slow" } |
04 |
05 | function randomDataCell(array) |
06 | return array [ math.random( 1 , #array) ] |
07 | end |
08 |
09 | local chosen_noun = randomDataCell(nouns) |
10 | local chosen_verb = randomDataCell(verbs) |
11 | local chosen_adjective = randomDataCell(adjectives) |
12 |
13 | local titleformats = chosen_noun.. " " ..chosen_verb.. " " ..chosen_adjective |
14 | print (titleformats) |
You can just make a list of each:
1 | -- your set of "adjectives" really are adverbs btw |
2 | local titleFormats = { |
3 | { nouns, verbs, adverbs } , |
4 | { nouns, verbs } , |
5 | } |
Given a particle "format" (a list of word-lists, e.g., {nouns, verbs, adverbs}
) you can generate a particular example by pulling from the lists:
01 | function choose(list) |
02 | return list [ math.random(#list) ] |
03 | end |
04 |
05 | function randomFromFormat(format) |
06 | local out = { } |
07 | for i = 1 , #format do |
08 | out [ i ] = choose(format [ i ] ) |
09 | end |
10 | return table.concat(out, " " ) |
11 | end |
You can re-use choose
to pick a random format from all formats, and then use exampleFromFormat
to pick a random choice from there:
1 | function randomTitle() |
2 | return randomFromFormat(choose(titleFormats)) |
3 | end |
Another interpretation is to actually expand a "format" into all of the titles of that format.
Given options from a set A
and options from a set B
, you can write the set of all pairs of values from A and from B as A × B
(in math notation, not Lua), called the Cartesian Product.
In this case, what you want to do is pick a random element from nouns × verbs × adverbs
, so an option is to just generate all of these ahead of time.
That isn't really a good idea, because if you have 1000 of each type of word, you have one-billion possible titles, and storing them all is really wasteful (you'll only ever ask for a few).
Still, it's an interesting function, and can be easily written recursively.
01 | function cartesianProduct(firsts, ...) |
02 | if not firsts then |
03 | return { { } } |
04 | end |
05 | local out = { } |
06 | local rests = cartesianProduct(...) |
07 | for _, rest in pairs (rests) do |
08 | for _, first in pairs (firsts) do |
09 | local copy = { first } |
10 | for i = 1 , #rest do |
11 | copy [ i+ 1 ] = rest [ i ] |
12 | end |
13 | table.insert(out, copy) |
14 | end |
15 | end |