Custom Scripts ~ Using Custom Scripts - uchicago-cs/chiventure GitHub Wiki
How Does Lua Work?
The first thing necessary for understanding custom scripts is understanding the interactions between Chiventure and Lua.
For the purpose of Chiventure, Lua is a way to store information. We can pass in arguments to Lua in the form of Functions, Ints, Bools, Chars and Strings. This information is stored in the Lua Game State, and it can be accessed and changed through an Instance of Chiventures. Everything in Lua is being stored on the stack, and can be popped off the stack and returned to C at any point. Lua will return a virtual pointer to the position of the variable or function for easy access.
What does Custom Scripts do?
What custom scripts does is handle most of the more complex parts of dealing with Lua. Custom Scripts will handle initializing the Lua game state, and will record where you put your variables and functions in the Lua stack. When you pass something to Lua, Custom Scripts will create a struct that will record what type of information you've stored in Lua, and return the pointer to the struct.
Afterwards, you can use the pointer to access and change the information in the Lua stack at any point, making it significantly easier to create a more dynamic game in Chiventure
How can I use Custom Scripts to make my game in Chiventure more interesting?
Custom Scripts allows you to dynamically generate different things depending on what you want to do.
One example is custom strings. You can use Custom Scripts to remember the player characters name, and make all strings then change to match the player's name from that point forward.
Another example is conditional actions. You can store a boolean value or an integer in Lua, and make actions dependent on that variable. You can make custom scripts so that an action is only possible if a certain item is in your inventory, or if you have a certain amount of coins.
The purpose of Custom Scripts is to make Chiventure more dynamic! Use Custom Scripts to make your game more unique and responsive!
That's super cool and all, but how do I actually use Custom Scripts?
The Interfacing functions of Custom scripts are the Object functions and the Get functions. The object function will store your variable in the Lua Stack, and return a pointer to a struct that will preserve all the information Lua has returned to find your variable. Afterwards, you can use the get function to return the variable you have stored in Lua using the pointer the the struct you have.
You can also call functions by writing a short Lua script that you can call within C and link to your instance of Chiventure. You can find examples of all these techniques in the "demo_2022.c" file within the Custom scripts folder.
If you have a more complex understanding of Lua, it is also possible to create functions to interact with your variables and create a more dynamic game environment.
How do I integrate LUA and WDL file together to make Custom Scripts?
The work around for using LUA in conjunction with WDL files is that you can set up a system that opens a corresponding WDL file depending on the user input. For example, if the user inputs '1', it can open 'example1.wdl' and if the user inputs '2', it can be linked to open 'example2.wdl'. Because WDL is loaded in with the game, it cannot dynamically be updated with a user input once it has been called.