multitheftauto / mtasa-blue

Multi Theft Auto is a game engine that incorporates an extendable network play element into a proprietary commercial single-player game.
https://multitheftauto.com
GNU General Public License v3.0
1.38k stars 427 forks source link

Add enginePreloadWorldArea function for preloading areas #3646

Closed MegadreamsBE closed 1 month ago

MegadreamsBE commented 1 month ago

Adds a new function enginePreloadWorldArea. This function takes in three required parameters, x, y and z for the position and a fourth optional parameter, option, which is a string (or actually an enum in the code) with the following valid values:

The game determines the zone the given coordinates are in and loads everything in it. This function is not radius based (as the underlying game functions aren't either).

The game will automatically unload this area based on its own internal rule, often this can already happen the next frame. This functions loads in the area immediately so it's advisable to do the desired action, such as teleporting a player/ped/vehicle, or getting the position of the ground in the same frame.

Some use cases: Load in the area, then retrieve the ground position at the desired location, then teleport the location to that location. The map will already be loaded, and the player is neatly put on the ground.

enginePreloadWorldArea(1000, 500, 0)   
local z = getGroundPosition(1000, 500, 100)   
setElementPosition(localPlayer, 1000, 500, z)

Load in the area (collisions only), then retrieve the ground position at the desired location and output it.

enginePreloadWorldArea(1000, 500, 0, "collisions")   
local z = getGroundPosition(1000, 500, 100)   
iprint("The ground position is: "..z)

NOTE: This function has been tested with the building removal functions and is not conflicting with them.

MegadreamsBE commented 1 month ago

Updated based on the reviews (except the enum name, which I kept the way it is as Hungarian notation is not used in the codebase anymore).

Dutchman101 commented 1 month ago

Good code reviews, and a fairly small change. Thanks!

Xenius97 commented 1 month ago

This won't work with objects (tested) but with buildings everything is fine.