Lua Choice System for my Visual Novel Engine

Started by
3 comments, last by Tangletail 8 years, 7 months ago

Hello Gamedev-community!

I am working on a Visual Novel engine which originally just worked fine with XML but now shall use Lua.

Thus I try to provide a quite clean scripting interface for myself. I already took some inspiration from the Ren'Py-engine which is using Python.

Though now I ended up with an issue for which my solutions feel dirty or too complicated while there might be a much faster and less complex way.

By the way, I am using Lua as a scripting language passing all the information to C++.

This is how my current Lua-file looks like:


	
	   location "school_outdoor"       -- Changes the current location to the outdoor of a school.
	   blendin ("angry", "y")          -- This blends a new character in, the y is already defined.
	   
           y "Hi! How are you?"            -- This is the first dialogue message on the screen.
	   m "I am fine - how about you?"  -- This will appear once clicking any key.
	   

However when I try to think of a choice system it ends up relying on Lua-tables. On the one side that must not be a bad thing I presume but on the other side it does not feel right.

I am going to try to explain what I want: Let us imagine there are 2 choices (in real there will be more though):

A question would be like: Do you want to eat cake or pizza?

Answer 1: I'd like to eat pizza!

Answer 2: I wanna eat cake!

On each answer there are commands/functions to be done.

Answer 1 or 2 would end up with dialogue message according to the selection and commands to run a different path of the dialogue tree. For example switching to another file/label but also increasing or decreasing stats of the player.

Ren'Py handles it like this:


menu:
    "Buy her an Iguana":
        $ gift = "iguana"
        b "I'll take an iguana, my good man!"
    "Buy her a Corn Snake":
        $ gift = "corn snake"
        b "I'll take a Corn Snake, my good man!"
    "Buy her a tortoise":
        $ gift = "tortoise"
        b "Give me your slowest tortoise, please!"

Source: http://renpy.org/wiki/renpy/doc/tutorials/Remembering_User_Choices

Is there any possibility to provide something similar in Lua? The variables are no deal, but the general structure. I also like how they can declare the choice-state by saying menu:.

I thought about using goto to seperate certain paths in my dialogue tree once an answer got selected.

Currently I am using Lua 5.1, so I would just update it for that.

However goto does not feature variability, like doing goto var in the very beginning of the script. I could not track the current position of the dialogue once the document is closed and then re-entered.

Unless I use a variable representing the label and then run if-loops. Might be a solution but makes it all more and more blown up of code.

In contrast I could just define a new dialogue-file which is planned anyway. Although I wanted to use new files only for completely new sections of the game and huge changes in the storyline.

Is it worth using goto and representing flags (which will be checked the beginning with ifs) to return the current label or is it better to keep everything seperated in new files? Especially: Are Lua-labels considered good or bad design decision?

Thinking about how to structure the choices gave me so many (bad) solutions which would not even fully work out.

For example:


choice "I want a cake."
	   y "Oh, we can buy one at the store."

choice "I want a pizza!"
	   y "Pizza is super delicious!"

choice_end

The first choice-function could initiate the choice selection (turn a boolean true) and also add it's string to the first dialogue-variable for choices (maybe in some Lua-table instead of a variable).

Since the choice selection is now true, the y "Oh, we can buy one at the store." would be stored in the mentioned Lua-table to be executed when the first selection is done. This is the point were I start disliking the idea because I would need a Lua-table and I feel like I am thinking way too complicated. I would have to check what entries in the table are the choices and then find their according commands.

Finally the choice_end would stop the searching for new choice-functions.

Is using a Lua-table to store all the data in a rather bad solution? Would it not be a quite slow method to operate that much with a Lua-table? This questions is a bit related to the first, too.

Thanks for taking your time to read this thread and I hope you might know some solution and could explain how I could tackle my issue.

Advertisement
It looks like you are trying to make a DSL (domain specific language) to express interaction.

A DSL is like a completely new language, you can just define what you want, rather than having to fold it into the constraints of the existing language, like you are doing now. All your examples can be coded without trouble in a DSL.

If that's the direction you want to go, you may want to look into parser and scanner generators, like yacc and lex for C/C++, or LPEG for Lua.

As for your questions, I would not worry about speed. A user will need time to read it all, which is way slower than the computing speed. As a general rule, don't optimize before you established there is a problem :)

(and no, you're not doing anything big in Lua, by the looks of it, CorsixTH does a lot more and runs fine).

Thanks for taking your time to reply Alberth. I will look into LPEG.

However why did somebody vote your post down? I am a bit confused now whether what you suggested is bad or a good thing to do.

Can anyone tell me why this post may got voted down?

So I notice, maybe I shouldn't have moved to discussing DSLs or so? No idea, tbh
Honestly, he provided a really good answer. I guess some people are just feeling hateful? Voted up.

This topic is closed to new replies.

Advertisement