Environment

CLIENT SIDE ONLY

Small selection of environment related functions.

Functions

get_game_time

Retrieves the current game time and a formatted version.

Function

local function get_game_time()
    local hour = GetClockHours()
    local minute = GetClockMinutes()
    return {
        time = {hour = hour, minute = minute},
        formatted = string.format('%02d:%02d', hour, minute)
    }
end

exports('get_game_time', get_game_time)
utils.environment.get_game_time = get_game_time

Example

--- Utils object
local time_data = utils.environment.get_game_time()

print('Time:',  time_data.time)
print('Formatted Time:', time_data.formatted)

--- Direct export
local time_data = exports.boii_utils:get_game_time()

get_game_date

Retrieves games current data and a formatted version.

Function

local function get_game_date()
    local day = GetClockDayOfMonth()
    local month = GetClockMonth()
    local year = GetClockYear()
    return {
        date = {day = day, month = month, year = year},
        formatted = string.format('%02d/%02d/%04d', day, month, year)
    }
end

exports('get_game_date', get_game_date)
utils.environment.get_game_date = get_game_date

Example

--- Utils object
local date_data = utils.environment.get_game_date()

print('Current Date:',  date_data.time)
print('Formatted Date:', date_data.formatted)

--- Direct export
local date_data = exports.boii_utils:get_game_date()

get_sunrise_sunset_times

Retrieves sunrise and sunset times based on weather type. These are entirely fictional, you can adjust the time values to whatever you like.

Function

local function get_sunrise_sunset_times(weather)
    local times = {
        CLEAR = { sunrise = '06:00', sunset = '18:00' },
        CLOUDS = { sunrise = '06:15', sunset = '17:45' },
        OVERCAST = { sunrise = '06:30', sunset = '17:30' },
        RAIN = { sunrise = '07:00', sunset = '17:00' },
        THUNDER = { sunrise = '07:00', sunset = '17:00' },
        SNOW = { sunrise = '08:00', sunset = '16:00' },
        BLIZZARD = { sunrise = '09:00', sunset = '15:00' },
    }
    return times[weather] or {sunrise = '06:00', sunset = '18:00'}
end

exports('get_sunrise_sunset_times', get_sunrise_sunset_times)
utils.environment.get_sunrise_sunset_times = get_sunrise_sunset_times

Example

--- Define weather type
local weather = 'CLEAR'

--- Utils object
local times = utils.environment.get_sunrise_sunset_times(weather)
print('Sunrise:', times.sunrise, 'Sunset:', times.sunset)

--- Direct export
exports.boii_utils:get_sunrise_sunset_times(weather)

is_daytime

Checks if current time is considered day time.

Function

local function is_daytime()
    local hour = GetClockHours()
    return hour >= 6 and hour < 18
end

exports('is_daytime', is_daytime)
utils.environment.is_daytime = is_daytime

Example

--- Utils object
local is_daytime = utils.environment.is_daytime()

print('Is Daytime:', tostring(is_daytime))

--- Direct export
local is_daytime = exports.boii_utils:is_daytime()

get_current_season

Retrieves current season.

Function

local function get_current_season()
    local month = GetClockMonth()
    if month >= 3 and month <= 5 then
        return 'Spring'
    elseif month >= 6 and month <= 8 then
        return 'Summer'
    elseif month >= 9 and month <= 11 then
        return 'Autumn'
    else
        return 'Winter'
    end
end

exports('get_current_season', get_current_season)
utils.environment.get_current_season = get_current_season

Example

--- Utils object
local season = utils.environment.get_current_season()

--- Direct export
local season = exports.boii_utils:get_current_season()

get_distance_to_water

Get the distance from the player to the nearest body of water.

Function

local function get_distance_to_water()
    local player_coords = GetEntityCoords(PlayerPedId())
    local water_test_result, water_height = TestVerticalProbeAgainstAllWater(player_coords.x, player_coords.y, player_coords.z, 0)
    if water_test_result then
        return #(player_coords - vector3(player_coords.x, player_coords.y, water_height))
    else
        return -1
    end
end

exports('get_distance_to_water', get_distance_to_water)
utils.environment.get_distance_to_water = get_distance_to_water

Example

--- Utils object
local distance_to_water = utils.environment.get_distance_to_water()

if distance_to_water >= 0 then
    print('Distance to water:', distance_to_water)
else
    print('No water nearby.')
end

--- Direct export
local distance_to_water = exports.boii_utils:get_distance_to_water()

get_water_height_at_coords

Gets water height at specified coordinates.

Function

local function get_water_height_at_coords(coords)
    local water_test_result, water_height = GetWaterHeight(coords.x, coords.y, coords.z)
    if water_test_result then
        return water_height
    else
        return -1
    end
end

exports('get_water_height_at_coords', get_water_height_at_coords)
utils.environment.get_water_height_at_coords = get_water_height_at_coords

Example

--- Define coords
local coords = vector3(123.45, 678.90, 21.0)

---  Utils object
local water_height = utils.environment.get_water_height_at_coords(coords)

if water_height >= 0 then
    print('Water height at coordinates:', water_height)
else
    print('No water found at the given coordinates.')
end

--- Direct export
local water_height = exports.boii_utils:get_water_height_at_coords(coords)

get_environment_details

Retrieves comprehensive environment details.

Function

local function get_environment_details()
    return {
        weather = get_weather_name(GetPrevWeatherTypeHashName()),
        time = get_game_time(),
        date = get_game_date(),
        season = get_current_season(),
        wind_speed = wind.get_speed(),
        wind_direction = wind.get_direction(),
        sunrise_sunset = get_sunrise_sunset_times(get_weather_name(GetPrevWeatherTypeHashName())),
        is_daytime = is_daytime(),
        distance_to_water = get_distance_to_water()
    }
end

exports('get_environment_details', get_environment_details)
utils.environment.get_environment_details = get_environment_details

Example

--- Utils object
local env_details = utils.environment.get_environment_details()

print('Weather:', env_details.weather)
print('Time:', env_details.time.formatted)
print('Date:', env_details.date.formatted)
print('Season:', env_details.season)
print('Wind Speed:', env_details.wind_speed)
print('Wind Direction:', env_details.wind_direction)
print('Sunrise:', env_details.sunrise_sunset.sunrise)
print('Sunset:', env_details.sunrise_sunset.sunset)
print('Daytime:', env_details.is_daytime and 'Yes' or 'No')
print('Distance to water:', env_details.distance_to_water)

--- Direct export
local env_details = exports.boii_utils:get_environment_details()

Last updated