[Tutorial] LuaEngine Development Tutorial (2) - Writing Simple Scripts

Following the previous tutorial, this time I will teach you how to write a simple script to get started with LuaEngine development. You can use any text editor to develop Lua scripts (even Notepad is fine), and the tool I use for demonstration is vscode. The goal of this tutorial is to set up the environment for script development, and write a simple Mod that uses shortcuts to teleport to a fixed point. The following content is based on the assumption that you already understand the basic usage of lua language. If you don’t, please go to the rookie website to learn the basics (you don’t need to learn too deep).

The first step is to modify the configuration file to open the command window of the front-end, which makes our script development more convenient. Open the loader-config.json file in the game root directory, change the value of logLevel to INFO, and save the file.

The second step is to create a new lua script. Go to the Lua folder in the game root directory. At this time, the folder contains Engine.lua and the modules folder, which are the engine files of LuaEngine. We don’t need these for today’s script. Next, we create a new test.lua file in the Lua directory.

The third step is to write the lua script. Open the test.lua file we created in the previous step and write function on_time() end in it.

The on_time function will be called every time the time changes in the game, which can be understood as a function that runs in real time.

Next, we need to sort out the functions we want to achieve in this script. Press the shortcut key (here we choose the numeric keypad 1 to record the coordinates, and the numeric keypad 2 to teleport) to record and teleport to the coordinates.

First, we need a function to detect whether the shortcut key is pressed. LuaEngine has a built-in function CheckKeyIsPressed(number key) to detect whether a key is pressed. We can use this function to achieve our goal.

According to the query vk table, we know that the numeric keypad 1 is 97 and the numeric keypad 2 is 98. We first write a function that outputs the word “record” when the numeric keypad 1 is pressed and “teleport” when the numeric keypad 2 is pressed.

After writing these, we open the game. First, we can see that the command window of the front-end has appeared. The content we want to output will be displayed here. We load the game and press the numeric keypad 1. If everything goes well, you can see that the command window has displayed a lot of “record”. This is because the print function will be triggered continuously when we press the key. Here we need to modify the code and add a trigger delay. Here we can use LuaEngine’s built-in timer function AddChronoscope(float time, string name), CheckChronoscope(string name), CheckPresenceChronoscope(string name) to achieve this (you can also use lua’s built-in time function to manually implement the timer).

At this point, we have limited the output text function to be executed only once per second. We reload the script in the game chat window by typing reload test (reload script name can reload the script) and see the effect. If everything is normal, when you press the numeric keypad 1 again, the command window will output “record” at a frequency of once per second.

So far, we have written the shortcut key function to trigger the recording coordinates and trigger the teleportation. Next, we will write the function of recording coordinates and teleporting.

First, we can see from the ct table that the current position coordinates of the player are offset as follows.

The offsets of the xyz axes are 160, 164, 168, respectively, and the data type is floating point. Next, we create a table variable to record the coordinates of these three axes. Then we create two functions to record and write the coordinates.

First, the variable used to record the coordinates. If you haven’t learned lua’s table yet, you can create three independent variables directly, but it is recommended that you first look at the basic usage of table.

You can see that the PlayerPos variable is defined outside the on_time function, so that this variable will be globally available in the current script and will not be refreshed by each call of on_time.

Next, we define two functions, one for recording the current position of the player and the other for modifying the current position of the player. Here we use LuaEngine’s memory reading function GetAddress(hex base, {hex offset, …}), GetAddressData(hex addr,string type) and memory modification function SetAddressData(hex addr,string type,value), and replace these two functions with the original output text.

Let’s take a look at the function of recording the player’s coordinates first. First, according to the ct table, we know that the player’s coordinates are located under the player entity with an offset of 160. We need to get the address of the player entity first. Here we use the GetAddress function, fill in the base address 0x145011760, and the pointer part is a table. You need to fill in each level of the pointer separately. Here we refer to the ct table. The player entity’s pointer has only one level, so we fill in {0x50}.

Then we judge whether this address exists. When getting the address, if LuaEngine fails to get it, it will return a false boolean value. Here we can directly use if to judge. Then we use GetAddressData to get and store the coordinate data of the player’s three axes. The first parameter of GetAddressData is the address to be read. According to the ct table, we know that the player’s x-axis coordinate is [player entity address + 160], so we fill in the player entity address + 160 we just got. The second parameter is the data type. According to the ct table, we know that the data type is floating point, which is ‘float’. At this point, we have written the function of recording the player’s coordinates.

Next, let’s take a look at the function of modifying the player’s coordinates. Similarly, we first get the player entity address. Then we do a judgment to see if we have recorded the coordinates. Do you remember that we gave the initial value of each axis as nil when we created PlayerPos? This means the value of empty. We can judge whether the three values are empty to determine whether we have recorded the coordinates. When there are recorded coordinates and the player entity also exists, we use SetAddressData to set the coordinates of the three axes separately. The usage of SetAddressData is basically the same as GetAddressData. The extra third parameter is the value to be set.

Now we reload the script in the game again. Do you remember how to operate it? reload test We go to the training area to try it. First, press the numeric keypad 1 to record the coordinates, then walk a few steps, and press the numeric keypad 2. If everything goes well, we have teleported back now.

At this point, we have written our core functions. However, this script still has some problems. Think about it, if you switch maps now, are the coordinates recorded by PlayerPos still valid? Will the player be blocked by the wall when teleporting? We will talk about these in the next issue.

by Alcedo Last Updated on 2024-01-11
Reward to instantly download attachments (amount set by author). Show your appreciation!
Default
Recent
1