[Crash] Dynamic Weather Lua

Level 3
Joined
Nov 26, 2021
Messages
25
Anyone know possible causes of of editor crashing when trying to save map with this code

In log file says invalid jass script in lua, but it is not jass it’s lua

Code:
-- Global table to store original movement speeds of units
local originalMoveSpeeds = {}

-- Helper function to store the original movement speed of a unit
function StoreOriginalMoveSpeed(unit)
    originalMoveSpeeds[unit] = GetUnitMoveSpeed(unit)
end

-- Helper function to restore the original movement speed of a unit
function RestoreOriginalMoveSpeed(unit)
    if originalMoveSpeeds[unit] then
        SetUnitMoveSpeed(unit, originalMoveSpeeds[unit])
    end
end

-- Helper function to create a meteor at a random position
function CreateMeteorAtPosition(x, y)
    local meteor = AddSpecialEffect("Abilities\\Spells\\Other\\Inferno\\Inferno.mdl", x, y)
    local moveDuration = 2.0  -- Duration in seconds for meteor to fall
    local meteorSpeed = 10.0  -- Speed of falling meteor
    local targetHeight = 0  -- Target height for impact (ground level)

    -- Start a timer to animate the falling effect
    local timer = CreateTimer()
    TimerStart(timer, 0.03, true, function()
        local x, y = GetUnitX(meteor), GetUnitY(meteor)
        local z = GetUnitZ(meteor) - meteorSpeed

        -- Update meteor's position (fall downward)
        if z <= targetHeight then
            -- Deal damage to nearby units when meteor hits the ground
            local unitsInRange = GetUnitsInRangeOfLocAll(200, Location(x, y))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 100, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_FIRE, nil)  -- 100 damage
            end
            DestroyEffect(meteor)
            DestroyTimer(timer)
        else
            SetUnitPosition(meteor, x, y, z)
        end
    end)
end

-- Helper function to create a lightning strike at a random position
function CreateLightningAtPosition(x, y)
    AddSpecialEffect("Abilities\\Spells\\Human\\StormBolt\\StormBoltTarget.mdl", x, y)
    PlaySoundAtPoint("Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl", x, y)

    -- Deal lightning damage to nearby units
    local unitsInRange = GetUnitsInRangeOfLocAll(250, Location(x, y))  -- Lightning radius of 250
    for _, unit in ipairs(unitsInRange) do
        UnitDamageTarget(unit, 50, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_LIGHTNING, nil)  -- 50 damage
    end
end

-- Function to set the weather (Rain, Snow, Fog, Clear, etc.)
function SetWeather(weatherType)
    -- First, restore the movement speed of all units
    local units = GetUnitsInRangeOfLocAll(10000, Location(0, 0))  -- Check all units on the map
    for _, unit in ipairs(units) do
        RestoreOriginalMoveSpeed(unit)
    end

    -- Clear all weather effects
    BlzSetWeatherEffect(WEATHER_EFFECT_RAIN, false)
    BlzSetWeatherEffect(WEATHER_EFFECT_SNOW, false)
    SetFogState(0, false)  -- Clear fog
    PlaySoundAtPoint("", 0, 0)  -- Stop any active sound

    -- Now apply the new weather
    if weatherType == "Rain" then
        BlzSetWeatherEffect(WEATHER_EFFECT_RAIN, true)
        PlaySoundAtPoint("Environment\\Rain\\RainLoop1.mdl", 0, 0)  -- Rain sound

        -- Impact on gameplay: Slows down units
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.7)  -- 30% slower movement
        end
    elseif weatherType == "Snow" then
        BlzSetWeatherEffect(WEATHER_EFFECT_SNOW, true)
        PlaySoundAtPoint("Environment\\Snow\\SnowstormLoop.mdl", 0, 0)  -- Snow sound

        -- Impact on gameplay: Slows down units and reduces visibility
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.6)  -- 40% slower movement
        end

        -- Reduce vision range (simulating the blizzard effect)
        SetUnitFogState(0, true)  -- Use fog for reduced visibility
    elseif weatherType == "Fog" then
        SetFogState(1, true)  -- Apply heavy fog
        PlaySoundAtPoint("Environment\\Fog\\FogLoop1.mdl", 0, 0)  -- Fog sound

        for _, unit in ipairs(units) do
            SetUnitFogState(unit, true)  -- Apply reduced visibility
        end
    elseif weatherType == "Wind" then
        PlaySoundAtPoint("Environment\\Wind\\WindLoop1.mdl", 0, 0)  -- Wind sound

        -- Impact on gameplay: Affect movement speed and projectiles
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 1.2)  -- 20% faster movement
        end
    elseif weatherType == "Meteor" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateMeteorAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Meteors\\MeteorImpact.mdl", 0, 0)  -- Meteor impact sound
    elseif weatherType == "Lightning" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateLightningAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Lightning\\LightningStrike.mdl", 0, 0)  -- Lightning sound
    elseif weatherType == "Clear" then
        -- Clear weather: Stop all weather effects, restore normal conditions
        SetFogState(0, false)  -- Clear fog
        PlaySoundAtPoint("Environment\\Clear\\ClearWeatherLoop.mdl", 0, 0)  -- Calm ambient sound

        -- Impact on gameplay: Restore normal movement speeds
        for _, unit in ipairs(units) do
            RestoreOriginalMoveSpeed(unit)
        end
    elseif weatherType == "AcidRain" then
        -- Acid Rain Effect: Visual or sound effect for acid rain
        PlaySoundAtPoint("Environment\\AcidRain\\AcidRainLoop.mdl", 0, 0)  -- Acid rain sound
        local acidRainEffect = AddSpecialEffect("Abilities\\Spells\\Other\\AcidCloud\\AcidCloud.mdl", 0, 0)

        -- Deal 1 damage per second to all units for 1 minute
        local acidRainTimer = CreateTimer()
        TimerStart(acidRainTimer, 1.0, true, function()
            -- Deal 1 damage to all units on the map every second
            local unitsInRange = GetUnitsInRangeOfLocAll(10000, Location(0, 0))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 1, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_ACID, nil)  -- 1 damage per second
            end
        end)

        -- Stop the acid rain after 1 minute
        TimerStart(CreateTimer(), 60.0, false, function()
            DestroyEffect(acidRainEffect)
            DestroyTimer(acidRainTimer)
        end)
    end

    -- Set a timer to end the current weather effect after 5 minutes (except for AcidRain)
    local weatherDuration = (weatherType == "AcidRain") and 60.0 or 300.0  -- 1 minute for AcidRain, 5 minutes for others
    local weatherTimer = CreateTimer()
    TimerStart(weatherTimer, weatherDuration, false, function()
        -- Reset to clear weather after the weather duration
        SetWeather("Clear")
        DestroyTimer(weatherTimer)
    end)
end

-- Function to randomly create a weather event (rain, snow, fog, wind, meteor, lightning, clear, acid rain)
function RandomWeatherEvent()
    local eventType = GetRandomInt(0, 7)  -- 0: Rain, 1: Snow, 2: Fog, 3: Wind, 4: Meteor, 5: Lightning, 6: Clear, 7: Acid Rain

    -- Rare chance for Acid Rain (1 in 20 chance)
    if GetRandomInt(1, 20) == 1 then
        SetWeather("AcidRain")
    else
        if eventType == 0 then
            SetWeather("Rain")
        elseif eventType == 1 then
            SetWeather("Snow")
        elseif eventType == 2 then
            SetWeather("Fog")
        elseif eventType == 3 then
            SetWeather("Wind")
        elseif eventType == 4 then
            SetWeather("Meteor")
        elseif eventType == 5 then
            SetWeather("Lightning")
        elseif eventType == 6 then
            SetWeather("Clear")
        end
    end
end

-- Function to start the dynamic weather system
function StartDynamicWeatherSystem()
    local interval = 5.0 * 60.0  -- 5 minutes interval (in seconds)

    local timer = CreateTimer()

    -- Start the periodic weather system every 5 minutes
    TimerStart(timer, interval, true, function()
        -- Trigger a random weather event (rain, snow, fog, wind, meteor, lightning, clear, acid rain)
        RandomWeatherEvent()
    end)
end

-- Start the dynamic weather system when the game begins
StartDynamicWeatherSystem()
 
Level 29
Joined
Sep 26, 2009
Messages
2,594
Don't use the percent symbol '%' in your script nor your comments, that causes the crash. As stated in A comprehensive guide to Mapping in Lua:
1734604619174.png
 
Level 3
Joined
Nov 26, 2021
Messages
25
lol as u can see i didnt look at that post, but didnt think hey does % crash lua randomly

anyways it no longer crashes, when saving however the system dont work, so now seeing if anyone can help get it working.

  • WeatherStart
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: StartDynamicWeatherSystem()
and i use this temp for testing, be changed in end

  • TriggerWeather
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Custom script: RandomWeatherEvent()
Added a notification for weather change.

Lua:
-- Global table to store original movement speeds of units
local originalMoveSpeeds = {}
-- Helper function to store the original movement speed of a unit
function StoreOriginalMoveSpeed(unit)
    originalMoveSpeeds[unit] = GetUnitMoveSpeed(unit)
end
-- Helper function to restore the original movement speed of a unit
function RestoreOriginalMoveSpeed(unit)
    if originalMoveSpeeds[unit] then
        SetUnitMoveSpeed(unit, originalMoveSpeeds[unit])
    end
end
-- Helper function to create a meteor at a random position
function CreateMeteorAtPosition(x, y)
    local meteor = AddSpecialEffect("Abilities\\Spells\\Other\\Inferno\\Inferno.mdl", x, y)
    local moveDuration = 2.0  -- Duration in seconds for meteor to fall
    local meteorSpeed = 10.0  -- Speed of falling meteor
    local targetHeight = 0  -- Target height for impact (ground level)
    -- Start a timer to animate the falling effect
    local timer = CreateTimer()
    TimerStart(timer, 0.03, true, function()
        local x, y = GetUnitX(meteor), GetUnitY(meteor)
        local z = GetUnitZ(meteor) - meteorSpeed
        -- Update meteor's position (fall downward)
        if z <= targetHeight then
            -- Deal damage to nearby units when meteor hits the ground
            local unitsInRange = GetUnitsInRangeOfLocAll(200, Location(x, y))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 100, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_FIRE, nil)  -- 100 damage
            end
            DestroyEffect(meteor)
            DestroyTimer(timer)
        else
            SetUnitPosition(meteor, x, y, z)
        end
    end)
end
-- Helper function to create a lightning strike at a random position
function CreateLightningAtPosition(x, y)
    AddSpecialEffect("Abilities\\Spells\\Human\\StormBolt\\StormBoltTarget.mdl", x, y)
    PlaySoundAtPoint("Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl", x, y)
    -- Deal lightning damage to nearby units
    local unitsInRange = GetUnitsInRangeOfLocAll(250, Location(x, y))  -- Lightning radius of 250
    for _, unit in ipairs(unitsInRange) do
        UnitDamageTarget(unit, 50, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_LIGHTNING, nil)  -- 50 damage
    end
end
-- Function to display the current weather type on the screen
function DisplayWeatherNotification(weatherType)
    local textTag = CreateTextTag()
    SetTextTagText(textTag, "Weather: " .. weatherType, 0.024)  -- Text size
    SetTextTagPos(textTag, 0, 0, 0)  -- Position at center of the screen
    SetTextTagColor(textTag, 255, 255, 255, 255)  -- White color
    SetTextTagPermanent(textTag, false)
    SetTextTagLifespan(textTag, 1.0)  -- Display for 1 second
    SetTextTagFadepoint(textTag, 0.5)  -- Fade out at half duration
end
-- Function to set the weather (Rain, Snow, Fog, Clear, etc.)
function SetWeather(weatherType)
    DisplayWeatherNotification(weatherType)  -- Notify the current weather type
  
    -- First, restore the movement speed of all units
    local units = GetUnitsInRangeOfLocAll(10000, Location(0, 0))  -- Check all units on the map
    for _, unit in ipairs(units) do
        RestoreOriginalMoveSpeed(unit)
    end
    -- Clear all weather effects
    BlzSetWeatherEffect(WEATHER_EFFECT_RAIN, false)
    BlzSetWeatherEffect(WEATHER_EFFECT_SNOW, false)
    SetFogState(0, false)  -- Clear fog
    PlaySoundAtPoint("", 0, 0)  -- Stop any active sound
    -- Now apply the new weather
    if weatherType == "Rain" then
        BlzSetWeatherEffect(WEATHER_EFFECT_RAIN, true)
        PlaySoundAtPoint("Environment\\Rain\\RainLoop1.mdl", 0, 0)  -- Rain sound
        -- Impact on gameplay: Slows down units
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.7)  -- 30P slower movement
        end
    elseif weatherType == "Snow" then
        BlzSetWeatherEffect(WEATHER_EFFECT_SNOW, true)
        PlaySoundAtPoint("Environment\\Snow\\SnowstormLoop.mdl", 0, 0)  -- Snow sound
        -- Impact on gameplay: Slows down units and reduces visibility
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.6)  -- 40P slower movement
        end
        -- Reduce vision range (simulating the blizzard effect)
        SetUnitFogState(0, true)  -- Use fog for reduced visibility
    elseif weatherType == "Fog" then
        SetFogState(1, true)  -- Apply heavy fog
        PlaySoundAtPoint("Environment\\Fog\\FogLoop1.mdl", 0, 0)  -- Fog sound
        for _, unit in ipairs(units) do
            SetUnitFogState(unit, true)  -- Apply reduced visibility
        end
    elseif weatherType == "Wind" then
        PlaySoundAtPoint("Environment\\Wind\\WindLoop1.mdl", 0, 0)  -- Wind sound
        -- Impact on gameplay: Affect movement speed and projectiles
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 1.2)  -- 20P faster movement
        end
    elseif weatherType == "Meteor" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateMeteorAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Meteors\\MeteorImpact.mdl", 0, 0)  -- Meteor impact sound
    elseif weatherType == "Lightning" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateLightningAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Lightning\\LightningStrike.mdl", 0, 0)  -- Lightning sound
    elseif weatherType == "Clear" then
        -- Clear weather: Stop all weather effects, restore normal conditions
        SetFogState(0, false)  -- Clear fog
        PlaySoundAtPoint("Environment\\Clear\\ClearWeatherLoop.mdl", 0, 0)  -- Calm ambient sound
        -- Impact on gameplay: Restore normal movement speeds
        for _, unit in ipairs(units) do
            RestoreOriginalMoveSpeed(unit)
        end
    elseif weatherType == "AcidRain" then
        -- Acid Rain Effect: Visual or sound effect for acid rain
        PlaySoundAtPoint("Environment\\AcidRain\\AcidRainLoop.mdl", 0, 0)  -- Acid rain sound
        local acidRainEffect = AddSpecialEffect("Abilities\\Spells\\Other\\AcidCloud\\AcidCloud.mdl", 0, 0)
        -- Deal 1 damage per second to all units for 1 minute
        local acidRainTimer = CreateTimer()
        TimerStart(acidRainTimer, 1.0, true, function()
            -- Deal 1 damage to all units on the map every second
            local unitsInRange = GetUnitsInRangeOfLocAll(10000, Location(0, 0))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 1, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_ACID, nil)  -- 1 damage per second
            end
        end)
        -- Stop the acid rain after 1 minute
        TimerStart(CreateTimer(), 60.0, false, function()
            DestroyEffect(acidRainEffect)
            DestroyTimer(acidRainTimer)
        end)
    end
    -- Set a timer to end the current weather effect after 5 minutes (except for AcidRain)
    local weatherDuration = (weatherType == "AcidRain") and 60.0 or 300.0  -- 1 minute for AcidRain, 5 minutes for others
    local weatherTimer = CreateTimer()
    TimerStart(weatherTimer, weatherDuration, false, function()
        -- Reset to clear weather after the weather duration
        SetWeather("Clear")
        DestroyTimer(weatherTimer)
    end)
end
-- Function to randomly create a weather event (rain, snow, fog, wind, meteor, lightning, clear, acid rain)
function RandomWeatherEvent()
    local eventType = GetRandomInt(0, 7)  -- 0: Rain, 1: Snow, 2: Fog, 3: Wind, 4: Meteor, 5: Lightning, 6: Clear, 7: Acid Rain
    -- Rare chance for Acid Rain (1 in 20 chance)
    if GetRandomInt(1, 20) == 1 then
        SetWeather("AcidRain")
    else
        if eventType == 0 then
            SetWeather("Rain")
        elseif eventType == 1 then
            SetWeather("Snow")
        elseif eventType == 2 then
            SetWeather("Fog")
        elseif eventType == 3 then
            SetWeather("Wind")
        elseif eventType == 4 then
            SetWeather("Meteor")
        elseif eventType == 5 then
            SetWeather("Lightning")
        elseif eventType == 6 then
            SetWeather("Clear")
        end
    end
end
-- Function to start the dynamic weather system
function StartDynamicWeatherSystem()
    local interval = 5.0 * 60.0  -- 5 minutes interval (in seconds)
    local timer = CreateTimer()
    -- Start the periodic weather system every 5 minutes
    TimerStart(timer, interval, true, function()
        -- Trigger a random weather event (rain, snow, fog, wind, meteor, lightning, clear, acid rain)
        RandomWeatherEvent()
    end)
end
-- Start the dynamic weather system when the game begins
StartDynamicWeatherSystem()

i have done some Debugging, it says

StartDynamicWeatherSystem Called
RandomWeatherEvent Triggered

But it doesnt trigger the weather effect, or the notification of weather type.
 
Last edited:
Level 29
Joined
Sep 26, 2009
Messages
2,594
Use debug messages to display how far your script execution got. Once you find where it stopped, try commenting out parts of the code to pinpoint where it goes wrong.

From what I can see in the script, there are two things I've noticed - one small one:
Lua:
SetTextTagPos(textTag, 0, 0, 0)  -- Position at center of the screen
This is not center of screen. This sets x/y/z coordinates of the map, so [0,0] is center of map. I am just pointing it out in case you were expecting to see the text on your screen and your camera was not at the center of map.

Second, bigger, problem: What is this function?
Lua:
BlzSetWeatherEffect(WEATHER_EFFECT_RAIN, false)
It doesn't compile with jass, I cannot even find it in docs Search - BlzSetWeatherEffect
 
Level 3
Joined
Nov 26, 2021
Messages
25
I am new to War3 lua so makes sense if I do something wrong, and is why I am asking on here to get help with it

So I assume I should be using AddWeatherEffecT

And for Text meant to centre of screen not map, so to fix that too

I do remember in testing the weather did work but only rain and snow, so I changed a stem and I guess changed it too much

Ain’t gonna lie I do t remember how I had it when only rain and snow worked
 
Last edited:
Level 3
Joined
Nov 26, 2021
Messages
25
ok i changed it some, but still cant get weather working, it notify perfectly this time, however weather doesnt change

Lua:
-- Global table to store original movement speeds of units
local originalMoveSpeeds = {}
-- Helper function to store the original movement speed of a unit
function StoreOriginalMoveSpeed(unit)
    originalMoveSpeeds[unit] = GetUnitMoveSpeed(unit)
end
-- Helper function to restore the original movement speed of a unit
function RestoreOriginalMoveSpeed(unit)
    if originalMoveSpeeds[unit] then
        SetUnitMoveSpeed(unit, originalMoveSpeeds[unit])
    end
end
-- Helper function to create a meteor at a random position
function CreateMeteorAtPosition(x, y)
    local meteor = AddSpecialEffect("Abilities\\Spells\\Other\\Inferno\\Inferno.mdl", x, y)
    local moveDuration = 2.0  -- Duration in seconds for meteor to fall
    local meteorSpeed = 10.0  -- Speed of falling meteor
    local targetHeight = 0  -- Target height for impact (ground level)
    -- Start a timer to animate the falling effect
    local timer = CreateTimer()
    TimerStart(timer, 0.03, true, function()
        local x, y = GetUnitX(meteor), GetUnitY(meteor)
        local z = GetUnitZ(meteor) - meteorSpeed
        -- Update meteor's position (fall downward)
        if z <= targetHeight then
            -- Deal damage to nearby units when meteor hits the ground
            local unitsInRange = GetUnitsInRangeOfLocAll(200, Location(x, y))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 100, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_FIRE, nil)  -- 100 damage
            end
            DestroyEffect(meteor)
            DestroyTimer(timer)
        else
            SetUnitPosition(meteor, x, y, z)
        end
    end)
end
-- Helper function to create a lightning strike at a random position
function CreateLightningAtPosition(x, y)
    AddSpecialEffect("Abilities\\Spells\\Human\\StormBolt\\StormBoltTarget.mdl", x, y)
    PlaySoundAtPoint("Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl", x, y)
  
    -- Deal lightning damage to nearby units
    local unitsInRange = GetUnitsInRangeOfLocAll(250, Location(x, y))  -- Lightning radius of 250
    for _, unit in ipairs(unitsInRange) do
        UnitDamageTarget(unit, 50, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_LIGHTNING, nil)  -- 50 damage
    end
end
-- Function to display the current weather type to all players
function DisplayWeatherNotification(weatherType)
    local message = "Weather: " .. weatherType
    -- Show the message to all players
    for i = 0, bj_MAX_PLAYER_SLOTS - 1 do
        local player = Player(i)
        if GetPlayerController(player) ~= MAP_CONTROL_NEUTRAL then
            DisplayTextToPlayer(player, 0, 0, message)
        end
    end
end
-- Function to set the weather (Rain, Snow, Fog, Clear, etc.)
function SetWeather(weatherType)
    DisplayWeatherNotification(weatherType)  -- Notify the current weather type
    -- First, restore the movement speed of all units
    local units = GetUnitsInRangeOfLocAll(10000, Location(0, 0))  -- Check all units on the map
    for _, unit in ipairs(units) do
        RestoreOriginalMoveSpeed(unit)
    end
    -- Clear all weather effects
    RemoveWeatherEffect(WEATHER_EFFECT_RAIN)
    RemoveWeatherEffect(WEATHER_EFFECT_SNOW)
    RemoveWeatherEffect(WEATHER_EFFECT_FOG)
    -- Now apply the new weather across the entire map
    local mapWidth = 320  -- Map width
    local mapHeight = 320  -- Map height
    if weatherType == "Rain" then
        -- Apply rain across the map
        AddWeatherEffect(WEATHER_EFFECT_RAIN, Location(mapWidth / 2, mapHeight / 2))  -- Middle of the map
        EnableWeatherEffect(WEATHER_EFFECT_RAIN, true)
        PlaySoundAtPoint("Environment\\Rain\\RainLoop1.mdl", 0, 0)  -- Rain sound
        -- Impact on gameplay: Slows down units
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.7)  -- 30P slower movement
        end
    elseif weatherType == "Snow" then
        -- Apply snow across the map
        AddWeatherEffect(WEATHER_EFFECT_SNOW, Location(mapWidth / 2, mapHeight / 2))
        EnableWeatherEffect(WEATHER_EFFECT_SNOW, true)
        PlaySoundAtPoint("Environment\\Snow\\SnowstormLoop.mdl", 0, 0)  -- Snow sound
        -- Impact on gameplay: Slows down units and reduces visibility
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.6)  -- 40P slower movement
        end
        -- Reduce vision range (simulating the blizzard effect)
        SetUnitFogState(0, true)
    elseif weatherType == "Fog" then
        -- Apply fog across the map
        AddWeatherEffect(WEATHER_EFFECT_FOG, Location(mapWidth / 2, mapHeight / 2))
        EnableWeatherEffect(WEATHER_EFFECT_FOG, true)
        PlaySoundAtPoint("Environment\\Fog\\FogLoop1.mdl", 0, 0)  -- Fog sound
        for _, unit in ipairs(units) do
            SetUnitFogState(unit, true)  -- Apply reduced visibility
        end
    elseif weatherType == "Wind" then
        -- Apply wind across the map
        PlaySoundAtPoint("Environment\\Wind\\WindLoop1.mdl", 0, 0)  -- Wind sound
        -- Impact on gameplay: Affect movement speed and projectiles
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 1.2)  -- 20P faster movement
        end
    elseif weatherType == "Meteor" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateMeteorAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Meteors\\MeteorImpact.mdl", 0, 0)  -- Meteor impact sound
    elseif weatherType == "Lightning" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateLightningAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Lightning\\LightningStrike.mdl", 0, 0)  -- Lightning sound
    elseif weatherType == "Clear" then
        -- Clear weather: Stop all weather effects, restore normal conditions
        RemoveWeatherEffect(WEATHER_EFFECT_RAIN)
        RemoveWeatherEffect(WEATHER_EFFECT_SNOW)
        RemoveWeatherEffect(WEATHER_EFFECT_FOG)
        PlaySoundAtPoint("Environment\\Clear\\ClearWeatherLoop.mdl", 0, 0)  -- Calm ambient sound
        -- Impact on gameplay: Restore normal movement speeds
        for _, unit in ipairs(units) do
            RestoreOriginalMoveSpeed(unit)
        end
    elseif weatherType == "AcidRain" then
        -- Acid Rain Effect: Visual or sound effect for acid rain
        PlaySoundAtPoint("Environment\\AcidRain\\AcidRainLoop.mdl", 0, 0)  -- Acid rain sound
        local acidRainEffect = AddSpecialEffect("Abilities\\Spells\\Other\\AcidCloud\\AcidCloud.mdl", 0, 0)
        -- Deal 1 damage per second to all units for 1 minute
        local acidRainTimer = CreateTimer()
        TimerStart(acidRainTimer, 1.0, true, function()
            -- Deal 1 damage to all units on the map every second
            local unitsInRange = GetUnitsInRangeOfLocAll(10000, Location(0, 0))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 1, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_ACID, nil)  -- 1 damage per second
            end
        end)
        -- Stop the acid rain after 1 minute
        TimerStart(CreateTimer(), 60.0, false, function()
            DestroyEffect(acidRainEffect)
            DestroyTimer(acidRainTimer)
        end)
    end
    -- Set a timer to end the current weather effect after 5 minutes (except for AcidRain)
    local weatherDuration = (weatherType == "AcidRain") and 60.0 or 300.0  -- 1 minute for AcidRain, 5 minutes for others
    local weatherTimer = CreateTimer()
    TimerStart(weatherTimer, weatherDuration, false, function()
        -- Reset to clear weather after the weather duration
        SetWeather("Clear")
        DestroyTimer(weatherTimer)
    end)
end
-- Function to randomly create a weather event (rain, snow, fog, wind, meteor, lightning, clear, acid rain)
function RandomWeatherEvent()
    local eventType = GetRandomInt(0, 7)  -- 0: Rain, 1: Snow, 2: Fog, 3: Wind, 4: Meteor, 5: Lightning, 6: Clear, 7: Acid Rain
    -- Rare chance for Acid Rain (1 in 20 chance)
    if GetRandomInt(1, 20) == 1 then
        SetWeather("AcidRain")
    else
        if eventType == 0 then
            SetWeather("Rain")
        elseif eventType == 1 then
            SetWeather("Snow")
        elseif eventType == 2 then
            SetWeather("Fog")
        elseif eventType == 3 then
            SetWeather("Wind")
        elseif eventType == 4 then
            SetWeather("Meteor")
        elseif eventType == 5 then
            SetWeather("Lightning")
        elseif eventType == 6 then
            SetWeather("Clear")
        end
    end
end
-- Function to start the dynamic weather system
function StartRandomWeatherCycle()
    TimerStart(CreateTimer(), 300.0, true, function()
        RandomWeatherEvent()
    end)
end
-- Initialize the system at the start of the game
function StartDynamicWeatherSystem()
    -- Start the periodic random weather cycle
    StartRandomWeatherCycle()
end
-- Call the Init function to start the weather system
StartDynamicWeatherSystem()
 

Attachments

  • [UDA] Red vs Blue v1.0.w3m
    116 KB · Views: 4
Level 29
Joined
Sep 26, 2009
Messages
2,594
what is WEATHER_EFFECT_RAIN and other similar arguments? I don't see anywhere in your map defining them.

Edit: Also, I see for example this:
Lua:
if weatherType == "Rain" then
        -- Apply rain across the map
        AddWeatherEffect(WEATHER_EFFECT_RAIN, Location(mapWidth / 2, mapHeight / 2))  -- Middle of the map
        EnableWeatherEffect(WEATHER_EFFECT_RAIN, true)
but that's not how it works. The output from AddWeatherEffect function is an object of type 'weathereffect'. This object should be tracked by you (similar to how you track units you have slowed down).
Parameter of 'EnableWeatherEffect' and 'RemoveWeatherEffect' is an object of type weathereffect, but the argument you select for both those functions is the same as for AddWeatherEffect, which is definitely wrong.
 
Level 3
Joined
Nov 26, 2021
Messages
25
I still cant get it working, maybe should give up


Lua:
-- Global table to store original movement speeds of units
local originalMoveSpeeds = {}
-- Helper function to store the original movement speed of a unit
function StoreOriginalMoveSpeed(unit)
    originalMoveSpeeds[unit] = GetUnitMoveSpeed(unit)
end
-- Helper function to restore the original movement speed of a unit
function RestoreOriginalMoveSpeed(unit)
    if originalMoveSpeeds[unit] then
        SetUnitMoveSpeed(unit, originalMoveSpeeds[unit])
    end
end
-- Helper function to create a meteor at a random position
function CreateMeteorAtPosition(x, y)
    local meteor = AddSpecialEffect("Abilities\\Spells\\Other\\Inferno\\Inferno.mdl", x, y)
    local moveDuration = 2.0  -- Duration in seconds for meteor to fall
    local meteorSpeed = 10.0  -- Speed of falling meteor
    local targetHeight = 0  -- Target height for impact (ground level)
    -- Start a timer to animate the falling effect
    local timer = CreateTimer()
    TimerStart(timer, 0.03, true, function()
        local x, y = GetUnitX(meteor), GetUnitY(meteor)
        local z = GetUnitZ(meteor) - meteorSpeed
        -- Update meteor's position (fall downward)
        if z <= targetHeight then
            -- Deal damage to nearby units when meteor hits the ground
            local unitsInRange = GetUnitsInRangeOfLocAll(200, Location(x, y))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 100, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_FIRE, nil)  -- 100 damage
            end
            DestroyEffect(meteor)
            DestroyTimer(timer)
        else
            SetUnitPosition(meteor, x, y, z)
        end
    end)
end
-- Helper function to create a lightning strike at a random position
function CreateLightningAtPosition(x, y)
    AddSpecialEffect("Abilities\\Spells\\Human\\StormBolt\\StormBoltTarget.mdl", x, y)
    PlaySoundAtPoint("Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl", x, y)
  
    -- Deal lightning damage to nearby units
    local unitsInRange = GetUnitsInRangeOfLocAll(250, Location(x, y))  -- Lightning radius of 250
    for _, unit in ipairs(unitsInRange) do
        UnitDamageTarget(unit, 50, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_LIGHTNING, nil)  -- 50 damage
    end
end
-- Function to display the current weather type to all players
function DisplayWeatherNotification(weatherType)
    local message = "Weather: " .. weatherType
    -- Show the message to all players
    for i = 0, bj_MAX_PLAYER_SLOTS - 1 do
        local player = Player(i)
        if GetPlayerController(player) ~= MAP_CONTROL_NEUTRAL then
            DisplayTextToPlayer(player, 0, 0, message)
        end
    end
end
-- Function to set the weather (Rain, Snow, Fog, Clear, etc.)
function SetWeather(weatherType)
    DisplayWeatherNotification(weatherType)  -- Notify the current weather type
    -- First, restore the movement speed of all units
    local units = GetUnitsInRangeOfLocAll(10000, Location(0, 0))  -- Check all units on the map
    for _, unit in ipairs(units) do
        RestoreOriginalMoveSpeed(unit)
    end
    -- Remove any existing weather effects
    if currentWeatherEffect then
        RemoveWeatherEffect(currentWeatherEffect)
    end
    -- Now apply the new weather across the entire map
    local mapWidth = 320  -- Map width
    local mapHeight = 320  -- Map height
    local currentWeatherEffect = nil
    if weatherType == "Rain" then
        -- Apply rain across the map
        currentWeatherEffect = AddWeatherEffect("Environment\\Rain\\Rain.mdl", Location(mapWidth / 2, mapHeight / 2))  -- Rain effect model path
        PlaySoundAtPoint("Environment\\Rain\\RainLoop1.mdl", 0, 0)  -- Rain sound
        -- Impact on gameplay: Slows down units
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.7)  -- 30P slower movement
        end
    elseif weatherType == "Snow" then
        -- Apply snow across the map
        currentWeatherEffect = AddWeatherEffect("Environment\\Snow\\Snow.mdl", Location(mapWidth / 2, mapHeight / 2))  -- Snow effect model path
        PlaySoundAtPoint("Environment\\Snow\\SnowstormLoop.mdl", 0, 0)  -- Snow sound
        -- Impact on gameplay: Slows down units and reduces visibility
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 0.6)  -- 40P slower movement
        end
        -- Reduce vision range (simulating the blizzard effect)
        SetUnitFogState(0, true)
    elseif weatherType == "Fog" then
        -- Apply fog across the map
        currentWeatherEffect = AddWeatherEffect("Environment\\Fog\\Fog.mdl", Location(mapWidth / 2, mapHeight / 2))  -- Fog effect model path
        PlaySoundAtPoint("Environment\\Fog\\FogLoop1.mdl", 0, 0)  -- Fog sound
        for _, unit in ipairs(units) do
            SetUnitFogState(unit, true)  -- Apply reduced visibility
        end
    elseif weatherType == "Wind" then
        -- Apply wind across the map
        PlaySoundAtPoint("Environment\\Wind\\WindLoop1.mdl", 0, 0)  -- Wind sound
        -- Impact on gameplay: Affect movement speed and projectiles
        for _, unit in ipairs(units) do
            SetUnitMoveSpeed(unit, GetUnitMoveSpeed(unit) * 1.2)  -- 20P faster movement
        end
    elseif weatherType == "Meteor" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateMeteorAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Meteors\\MeteorImpact.mdl", 0, 0)  -- Meteor impact sound
    elseif weatherType == "Lightning" then
        local x = GetRandomReal(-500, 500)
        local y = GetRandomReal(-500, 500)
        CreateLightningAtPosition(x, y)
        PlaySoundAtPoint("Environment\\Lightning\\LightningStrike.mdl", 0, 0)  -- Lightning sound
    elseif weatherType == "Clear" then
        -- Clear weather: Stop all weather effects, restore normal conditions
        if currentWeatherEffect then
            RemoveWeatherEffect(currentWeatherEffect)
        end
        PlaySoundAtPoint("Environment\\Clear\\ClearWeatherLoop.mdl", 0, 0)  -- Calm ambient sound
        -- Impact on gameplay: Restore normal movement speeds
        for _, unit in ipairs(units) do
            RestoreOriginalMoveSpeed(unit)
        end
    elseif weatherType == "AcidRain" then
        -- Acid Rain Effect: Visual or sound effect for acid rain
        PlaySoundAtPoint("Environment\\AcidRain\\AcidRainLoop.mdl", 0, 0)  -- Acid rain sound
        local acidRainEffect = AddSpecialEffect("Abilities\\Spells\\Other\\AcidCloud\\AcidCloud.mdl", 0, 0)
        -- Deal 1 damage per second to all units for 1 minute
        local acidRainTimer = CreateTimer()
        TimerStart(acidRainTimer, 1.0, true, function()
            -- Deal 1 damage to all units on the map every second
            local unitsInRange = GetUnitsInRangeOfLocAll(10000, Location(0, 0))
            for _, unit in ipairs(unitsInRange) do
                UnitDamageTarget(unit, 1, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_ACID, nil)  -- 1 damage per second
            end
        end)
        -- Stop the acid rain after 1 minute
        TimerStart(CreateTimer(), 60.0, false, function()
            DestroyEffect(acidRainEffect)
            DestroyTimer(acidRainTimer)
        end)
    end
    -- Set a timer to end the current weather effect after 5 minutes (except for AcidRain)
    local weatherDuration = (weatherType == "AcidRain") and 60.0 or 300.0  -- 1 minute for AcidRain, 5 minutes for others
    local weatherTimer = CreateTimer()
    TimerStart(weatherTimer, weatherDuration, false, function()
        -- Reset to clear weather after the weather duration
        SetWeather("Clear")
        DestroyTimer(weatherTimer)
    end)
end
-- Function to randomly create a weather event (rain, snow, fog, wind, meteor, lightning, clear, acid rain)
function RandomWeatherEvent()
    local eventType = GetRandomInt(0, 7)  -- 0: Rain, 1: Snow, 2: Fog, 3: Wind, 4: Meteor, 5: Lightning, 6: Clear, 7: Acid Rain
    -- Rare chance for Acid Rain (1 in 20 chance)
    if GetRandomInt(1, 20) == 1 then
        SetWeather("AcidRain")
    else
        if eventType == 0 then
            SetWeather("Rain")
        elseif eventType == 1 then
            SetWeather("Snow")
        elseif eventType == 2 then
            SetWeather("Fog")
        elseif eventType == 3 then
            SetWeather("Wind")
        elseif eventType == 4 then
            SetWeather("Meteor")
        elseif eventType == 5 then
            SetWeather("Lightning")
        elseif eventType == 6 then
            SetWeather("Clear")
        end
    end
end
-- Function to start the dynamic weather system
function StartRandomWeatherCycle()
    TimerStart(CreateTimer(), 300.0, true, function()
        RandomWeatherEvent()
    end)
end
-- Initialize the system at the start of the game
function StartDynamicWeatherSystem()
    -- Start the periodic random weather cycle
    StartRandomWeatherCycle()
end
-- Call the Init function to start the weather system
StartDynamicWeatherSystem()
 
Level 29
Joined
Sep 26, 2009
Messages
2,594
yeah, it won't work. I put the script in a folder along with Blizzard's API files and it underlined a lot of your function calls.
I think you should read the A comprehensive guide to Mapping in Lua
Especially I believe you should check:
  • Section "Getting Started" --> "IDE Setup". The tutorial also contains blizzard's API files (blizzard*.lua, common*.lua, hiddenatives*.lua). Install VS Code + VS Code's extension for lua, as mentioned in the tutorial. Then in the same folder put your script and the blizzard API files, and VS Code will then underline all issues it can find.
  • Section "Lua fundamentals" --> "Closures and scopes" to see how to encapsulate your script.

Also check Jassbot - although it is written using JASS syntax, the function names, list of parameters, their type and order is same for both JASS and Lua.
For example it highlighted this line:
1734718716965.png

You can see that second argument is expected to be the target that is damaged, but you forgot that so all your arguments are offset by one.

All the yellow lines are some warnings in your script that VS Code detected:
1734718809692.png
 
Level 3
Joined
Nov 26, 2021
Messages
25
ty for help so far, atm i have a working weather system however doesnt stop the meteors lightning or the acid damage when weather changes, so few more things to tweak, also plan to add ambient sound per weather type in end too.

Lua:
-- =====================================
-- Warcraft 3 Dynamic Weather System
-- =====================================
-- Global Variables
local weather_types = {"Rain", "Snow", "Clear", "Windy", "Blizzard", "Thunderstorm", "Firestorm", "Acid Rain"}
local current_weather = ""
local weather_timer = nil
local active_weather_effect = nil
-- Function to Clear Current Weather
local function clearCurrentWeather()
    if active_weather_effect then
        RemoveWeatherEffect(active_weather_effect)
        active_weather_effect = nil
    end
end
-- Apply Weather Effects Based on Type
local function applyWeatherEffect(weather)
    -- Clear any active weather
    clearCurrentWeather()
    -- Display the weather notification to all players
    for i = 0, bj_MAX_PLAYERS - 1 do
        if IsPlayerInForce(Player(i), bj_FORCE_ALL_PLAYERS) then
            DisplayTextToPlayer(Player(i), 0, 0, "Weather: " .. weather)
        end
    end
    -- Apply effects for the current weather type
    if weather == "Rain" then
        -- Add Rain weather effect
        active_weather_effect = AddWeatherEffect(GetPlayableMapRect(), FourCC("RLlr"))
        EnableWeatherEffect(active_weather_effect, true)
        -- Reduce attack damage by 10P
        for i = 0, bj_MAX_PLAYERS - 1 do
            if IsPlayerInForce(Player(i), bj_FORCE_ALL_PLAYERS) then
                DisplayTextToPlayer(Player(i), 0, 0, "Rain: Attack damage reduced by 10 Percent!")
            end
        end
    elseif weather == "Snow" then
        -- Add Snow weather effect
        active_weather_effect = AddWeatherEffect(GetPlayableMapRect(), FourCC("SNhs"))
        EnableWeatherEffect(active_weather_effect, true)
        -- Reduce movement speed by 20P
        for i = 0, bj_MAX_PLAYERS - 1 do
            if IsPlayerInForce(Player(i), bj_FORCE_ALL_PLAYERS) then
                DisplayTextToPlayer(Player(i), 0, 0, "Snow: Movement speed reduced by 20 Percent!")
            end
        end
    elseif weather == "Clear" then
        -- Clear weather (no special effects)
        clearCurrentWeather()
        -- No changes
        for i = 0, bj_MAX_PLAYERS - 1 do
            if IsPlayerInForce(Player(i), bj_FORCE_ALL_PLAYERS) then
                DisplayTextToPlayer(Player(i), 0, 0, "Clear: No effects!")
            end
        end
    elseif weather == "Windy" then
        -- Add Windy weather effect
        active_weather_effect = AddWeatherEffect(GetPlayableMapRect(), FourCC("WOlw"))
        EnableWeatherEffect(active_weather_effect, true)
        -- Increase attack speed by 15P
        for i = 0, bj_MAX_PLAYERS - 1 do
            if IsPlayerInForce(Player(i), bj_FORCE_ALL_PLAYERS) then
                DisplayTextToPlayer(Player(i), 0, 0, "Windy: Attack speed increased by 15 Percent!")
            end
        end
    elseif weather == "Blizzard" then
        -- Add Blizzard weather effect
        active_weather_effect = AddWeatherEffect(GetPlayableMapRect(), FourCC("SNbs"))
        EnableWeatherEffect(active_weather_effect, true)
        -- Movement and attack speed reduced
        for i = 0, bj_MAX_PLAYERS - 1 do
            if IsPlayerInForce(Player(i), bj_FORCE_ALL_PLAYERS) then
                DisplayTextToPlayer(Player(i), 0, 0, "Blizzard: Movement and attack speed reduced!")
            end
        end
    elseif weather == "Thunderstorm" then
        -- Add Thunderstorm weather effect
        active_weather_effect = AddWeatherEffect(GetPlayableMapRect(), FourCC("RLhr"))
        EnableWeatherEffect(active_weather_effect, true)
        -- Random lightning strikes
        TimerStart(CreateTimer(), 1.0, true, function()
            local x = GetRandomReal(GetRectMinX(GetPlayableMapRect()), GetRectMaxX(GetPlayableMapRect()))
            local y = GetRandomReal(GetRectMinY(GetPlayableMapRect()), GetRectMaxY(GetPlayableMapRect()))
            local effect = AddSpecialEffect("war3mapImported\\LightningWrath.mdl", x, y)
            BlzSetSpecialEffectScale(effect, 2.0)
            DestroyEffect(effect)
            local group = CreateGroup()
            GroupEnumUnitsInRange(group, x, y, 150, nil)
            while true do
                local unit = FirstOfGroup(group)
                if unit == nil then break end
                UnitDamageTarget(unit, unit, 50.0, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_FIRE, nil)
                GroupRemoveUnit(group, unit)
            end
            DestroyGroup(group)
        end)
    elseif weather == "Firestorm" then
        -- Falling meteors every second
        TimerStart(CreateTimer(), 1.0, true, function()
            local x = GetRandomReal(GetRectMinX(GetPlayableMapRect()), GetRectMaxX(GetPlayableMapRect()))
            local y = GetRandomReal(GetRectMinY(GetPlayableMapRect()), GetRectMaxY(GetPlayableMapRect()))
            local effect = AddSpecialEffect("war3mapImported\\DarknessMeteor.mdl", x, y)
            BlzSetSpecialEffectScale(effect, 2.0)
            DestroyEffect(effect)
            local group = CreateGroup()
            GroupEnumUnitsInRange(group, x, y, 150, nil)
            while true do
                local unit = FirstOfGroup(group)
                if unit == nil then break end
                UnitDamageTarget(unit, unit, 75.0, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_FIRE, nil)
                GroupRemoveUnit(group, unit)
            end
            DestroyGroup(group)
        end)
    elseif weather == "Acid Rain" then
        -- Add Rain weather effect
        active_weather_effect = AddWeatherEffect(GetPlayableMapRect(), FourCC("RLlr"))
        EnableWeatherEffect(active_weather_effect, true)
        -- Damage over time
        TimerStart(CreateTimer(), 1.0, true, function()
            local group = CreateGroup()
            GroupEnumUnitsInRect(group, GetPlayableMapRect(), nil)
            while true do
                local unit = FirstOfGroup(group)
                if unit == nil then break end
                UnitDamageTarget(unit, unit, 5.0, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_POISON, nil)
                GroupRemoveUnit(group, unit)
            end
            DestroyGroup(group)
        end)
    end
end
-- Function to Change Weather
local function changeWeather()
    -- Select a random weather type
    local new_weather = weather_types[GetRandomInt(1, #weather_types)]
    -- Ensure the new weather is different from the current one
    while new_weather == current_weather do
        new_weather = weather_types[GetRandomInt(1, #weather_types)]
    end
    -- Update the current weather
    current_weather = new_weather
    -- Apply the new weather effect
    applyWeatherEffect(current_weather)
end
-- Initialize Weather System
function InitWeatherSystem()
    -- Initialize the first weather
    changeWeather()
    -- Set up a repeating timer to change weather every 5 minutes
    weather_timer = CreateTimer()
    TimerStart(weather_timer, 300.0, true, function()
        changeWeather()
    end)
end
-- Main Function to Start the System
function main()
    InitWeatherSystem()
end
 
Level 29
Joined
Sep 26, 2009
Messages
2,594
ty for help so far, atm i have a working weather system however doesnt stop the meteors lightning or the acid damage when weather changes, so few more things to tweak, also plan to add ambient sound per weather type in end too.
That is to be expected. You do this:
Lua:
TimerStart(CreateTimer(), 1.0, true, function() ... this function creates lightning effects
You have created a timer and set it to periodically run the function. So what is telling the timer to stop? The answer is nothing :)
What you should do is create a timer, assign it to a variable and similar to how you keep track of current weather via active_weather_effect and use that to stop it, you will track the timer via variable and stop it via PauseTimer function when you change weather.

A few other things I've noticed in your script:
1. You have this to display text to all players in "all players" group:
Lua:
for i = 0, bj_MAX_PLAYERS - 1 do
    if IsPlayerInForce(Player(i), bj_FORCE_ALL_PLAYERS) then
      DisplayTextToPlayer(Player(i), 0, 0, "Blizzard: Movement and attack speed reduced!")
   end
end
But I believe you can replace that by just calling DisplayTimedTextToForce. See Doc - DisplayTimedTextToForce
The implementation of that function is same for both lua and jass and I think it will do exactly the same as what you are doing but with less code, no? Maybe just the duration of the text will be different, but I don't think that really matters to you since the text is short.

2. You have this:
Lua:
GroupEnumUnitsInRange(group, x, y, 150, nil)
while true do
    local unit = FirstOfGroup(group)
    if unit == nil then break end
    UnitDamageTarget(unit, unit, 50.0, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_FIRE, nil)
    GroupRemoveUnit(group, unit)
end
This is fine. I just wanted to point out that blizz also introduced following functions for iterating unit groups:
  • BlizGroupGetSize (Doc - BlzGroupGetSize) which returns size of the internal array used by unit groups
  • BlzGroupUnitAt (Doc - BlzGroupUnitAt) which returns unit at specified index (starting at 0 for first unit) in given unit group.

Note that the internal array may contain empty values at some indices (this usually happens if you remove units from game or the unit group), so you should not break the loop if unit == nil. However you could have code like this
Lua:
for i = 0, BlzGroupGetSize(g) do
    target = BlzGroupUnitAt(g, i)
    if target != nil then
        //do damage to target
    end
end
 
Top