Meteo e Tempo
INFO
Il meteo e il ciclo giorno/notte possono essere sincronizzati per tutti i giocatori. Esistono risorse pronte (vSync, esx_weather) oppure script custom.
| Obiettivo | Dettagli |
|---|---|
| Cosa imparerai | • Nativi per meteo e tempo • Sincronizzare tempo tra i giocatori • Timecycle modifier • Weather per zona (base) |
| Prerequisiti | • Lua base (modulo 08) |
1. Nativi di Base
-- client.lua
SetWeatherTypeNow('EXTRASUNNY') -- Cambia subito il meteo
SetWeatherTypePersist('RAIN') -- Rende persistente il meteo
SetWeatherTypeOvertime('FOGGY', 5.0) -- Transizione di 5 secondi
SetClockTime(12, 0, 0) -- Imposta ora locale (non sincronizzata)| Meteo | Hash name |
|---|---|
| Sole pieno | EXTRASUNNY |
| Sole | CLEAR |
| Nuvole | CLOUDS |
| Pioggia | RAIN |
| Temporale | THUNDER |
| Nebbia | FOGGY |
| Neve | SNOWLIGHT / SNOW |
2. Sincronizzare per Tutti
Il tempo va deciso dal server e inviato ai client.
-- server.lua
local currentWeather = 'CLEAR'
local hour = 12
-- Cambia meteo (server-wide)
function SetWeather(weather)
currentWeather = weather
TriggerClientEvent('weather:update', -1, weather)
end
-- Cambia ora
function SetHour(h)
hour = h
TriggerClientEvent('weather:time', -1, h)
end-- client.lua
RegisterNetEvent('weather:update')
AddEventHandler('weather:update', function(weather)
SetWeatherTypeOverTime(weather, 5.0)
end)
RegisterNetEvent('weather:time')
AddEventHandler('weather:time', function(h)
SetClockTime(h, 0, 0)
end)
-- Al login, chiedi lo stato attuale
RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function()
TriggerServerEvent('weather:request')
end)3. Timecycle Modifier
I modifier del timecycle cambiano l’aspetto della luce e dei colori.
-- client.lua
SetTimecycleModifier('hud_def_blur') -- Applica un modifier
SetTimecycleModifierStrength(0.5) -- Intensità
ClearTimecycleModifier() -- RimuoviTIP
I timecycle modifier sono utili per zone speciali (es. interni bui, stati alterati). Non abusarne su tutto il server: possono togliere performance.
4. Meteo per Zona (avanzato)
Per avere meteo diverso in zone diverse serve una logica più complessa. Pattern base:
-- client.lua
CreateThread(function()
while true do
Wait(5000)
local coords = GetEntityCoords(PlayerPedId())
if #(coords - Config.SnowZone) < 100.0 then
SetWeatherTypeNow('SNOWLIGHT')
end
end
end)WARNING
Cambiare meteo continuamente per zona è costoso. Usalo solo per zone molto particolari e con transizioni lunghe.
5. vSync (risorsa pronta)
vSync (da https://github.com/Mobius1/vSync) è una risorsa pronta che sincronizza meteo e tempo. Installazione:
git clone https://github.com/Mobius1/vSync.gitensure vSyncvSync usa le sue convenzioni di config per orari di alba/tramonto e durata del giorno.
Verifica
- Meteo sincronizzato per tutti i giocatori
- Ora del giorno coerente tra i client
- Nessun cambio meteo troppo frequente
- Al login il giocatore riceve lo stato corrente
TIP
Prossimo passo: 05 - IPL e Interior
Torna alla: Indice Generale