• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Lua : How to override global functions properly ?

Status
Not open for further replies.
Level 12
Joined
Jan 30, 2020
Messages
875
Hello there !

I am trying to override global functions, like those created by World Editor when saving the map script.

I was hoping to use the context to modify them.
I did a test with several of them, including "main", "config" or "InitBlizzard" :

Lua:
do
    local oldmain = main
    main = function()
        return newmain()
    end

    local oldconfig = io.config
    config = function()
        return newconfig()
    end

    local oldInitBlizzard = InitBlizzard
    InitBlizzard = function()
        return newInitBlizzard()
    end

    function newmain()
        SetCameraBounds(-8192.0 + GetCameraMargin(CAMERA_MARGIN_LEFT), -8192.0 + GetCameraMargin(CAMERA_MARGIN_BOTTOM), 8192.0 - GetCameraMargin(CAMERA_MARGIN_RIGHT), 8192.0 - GetCameraMargin(CAMERA_MARGIN_TOP), -8192.0 + GetCameraMargin(CAMERA_MARGIN_LEFT), 8192.0 - GetCameraMargin(CAMERA_MARGIN_TOP), 8192.0 - GetCameraMargin(CAMERA_MARGIN_RIGHT), -8192.0 + GetCameraMargin(CAMERA_MARGIN_BOTTOM))
        SetDayNightModels("Environment\\DNC\\DNCLordaeron\\DNCLordaeronTerrain\\DNCLordaeronTerrain.mdl", "Environment\\DNC\\DNCLordaeron\\DNCLordaeronUnit\\DNCLordaeronUnit.mdl")
        SetWaterBaseColor(0, 0, 255, 255)
        NewSoundEnvironment("Default")
        SetAmbientDaySound("IceCrownDay")
        SetAmbientNightSound("IceCrownNight")
        SetMapMusic("Music", true, 0)
        InitBlizzard()
        Init()
    end

    function newconfig()
        SetMapName("TRIGSTR_003")
        SetMapDescription("TRIGSTR_1139")
        SetPlayers(5)
        SetTeams(5)
        SetGamePlacement(MAP_PLACEMENT_TEAMS_TOGETHER)
        DefineStartLocation(0, -2752.0, 6528.0)
        DefineStartLocation(1, 6528.0, 2752.0)
        DefineStartLocation(2, 2752.0, -6528.0)
        DefineStartLocation(3, -6528.0, -2752.0)
        DefineStartLocation(4, 64.0, 64.0)
        InitCustomPlayerSlots()
        InitCustomTeams()
        InitAllyPriorities()
    end

    function newInitBlizzard()
        -- Set up the Neutral Victim player slot, to torture the abandoned units
        -- of defeated players.  Since some triggers expect this player slot to
        -- exist, this is performed for all maps.
        ConfigureNeutralVictim()
        InitBlizzardGlobals()
        InitQueuedTriggers()
        InitRescuableBehaviorBJ()
        InitDNCSounds()
        InitMapRects()
        InitSummonableCaps()
        InitNeutralBuildings()
        DetectGameStarted()
    end
end

Note that the only thing I changed from the original functions is the last 3 lines of the "main" function to call my custom Init function rather than InitGlobals, InitCustomTriggers and RunInitializationTriggers that are useless in my map.

The problem is, when I try to do this, and test the map from WE, the game starts up, but hangs on the lion logo, the map not loading anything, so I guess it means the original global functions are overridden, but my custom versions like newmain do not run.

Also note that unlike in @Bribe 's [Lua] Global Initialization, I don't want to run the original function to just add mine, but I would rather like to gain full control of what is loaded with my map, to prevent it from loading useless things.

I know I can do this by editing the wc3map.lua script directly, but I would then lose the ability to work on my map from the World Editor.

So, what have I done wrong ?

EDIT 1: oops seems like I once again have done a typo

EDIT 2: ok, after removing the io. from io.config, the map loads, but then nothing happens when the map is started (or rather the screen is black like full Fog Of War and only the preliminary UI shows)
 
Last edited:

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Hello there !

I am trying to override global functions, like those created by World Editor when saving the map script.

I was hoping to use the context to modify them.
I did a test with several of them, including "main", "config" or "InitBlizzard" :

Lua:
do
    local oldmain = main
    main = function()
        return newmain()
    end

    local oldconfig = io.config
    config = function()
        return newconfig()
    end

    local oldInitBlizzard = InitBlizzard
    InitBlizzard = function()
        return newInitBlizzard()
    end

    function newmain()
        SetCameraBounds(-8192.0 + GetCameraMargin(CAMERA_MARGIN_LEFT), -8192.0 + GetCameraMargin(CAMERA_MARGIN_BOTTOM), 8192.0 - GetCameraMargin(CAMERA_MARGIN_RIGHT), 8192.0 - GetCameraMargin(CAMERA_MARGIN_TOP), -8192.0 + GetCameraMargin(CAMERA_MARGIN_LEFT), 8192.0 - GetCameraMargin(CAMERA_MARGIN_TOP), 8192.0 - GetCameraMargin(CAMERA_MARGIN_RIGHT), -8192.0 + GetCameraMargin(CAMERA_MARGIN_BOTTOM))
        SetDayNightModels("Environment\\DNC\\DNCLordaeron\\DNCLordaeronTerrain\\DNCLordaeronTerrain.mdl", "Environment\\DNC\\DNCLordaeron\\DNCLordaeronUnit\\DNCLordaeronUnit.mdl")
        SetWaterBaseColor(0, 0, 255, 255)
        NewSoundEnvironment("Default")
        SetAmbientDaySound("IceCrownDay")
        SetAmbientNightSound("IceCrownNight")
        SetMapMusic("Music", true, 0)
        InitBlizzard()
        Init()
    end

    function newconfig()
        SetMapName("TRIGSTR_003")
        SetMapDescription("TRIGSTR_1139")
        SetPlayers(5)
        SetTeams(5)
        SetGamePlacement(MAP_PLACEMENT_TEAMS_TOGETHER)
        DefineStartLocation(0, -2752.0, 6528.0)
        DefineStartLocation(1, 6528.0, 2752.0)
        DefineStartLocation(2, 2752.0, -6528.0)
        DefineStartLocation(3, -6528.0, -2752.0)
        DefineStartLocation(4, 64.0, 64.0)
        InitCustomPlayerSlots()
        InitCustomTeams()
        InitAllyPriorities()
    end

    function newInitBlizzard()
        -- Set up the Neutral Victim player slot, to torture the abandoned units
        -- of defeated players.  Since some triggers expect this player slot to
        -- exist, this is performed for all maps.
        ConfigureNeutralVictim()
        InitBlizzardGlobals()
        InitQueuedTriggers()
        InitRescuableBehaviorBJ()
        InitDNCSounds()
        InitMapRects()
        InitSummonableCaps()
        InitNeutralBuildings()
        DetectGameStarted()
    end
end

Note that the only thing I changed from the original functions is the last 3 lines of the "main" function to call my custom Init function rather than InitGlobals, InitCustomTriggers and RunInitializationTriggers that are useless in my map.

The problem is, when I try to do this, and test the map from WE, the game starts up, but hangs on the lion logo, the map not loading anything, so I guess it means the original global functions are overridden, but my custom versions like newmain do not run.

Also note that unlike in @Bribe 's [Lua] Global Initialization, I don't want to run the original function to just add mine, but I would rather like to gain full control of what is loaded with my map, to prevent it from loading useless things.

I know I can do this by editing the wc3map.lua script directly, but I would then lose the ability to work on my map from the World Editor.

So, what have I done wrong ?

EDIT 1: oops seems like I once again have done a typo

EDIT 2: ok, after removing the io. from io.config, the map loads, but then nothing happens when the map is started (or rather the screen is black like full Fog Of War and only the preliminary UI shows)

I would not recommend hooking main but rather just the Init Blizzard and do your stuff from there. The main function had problems when I tried to override it too.
 
Level 12
Joined
Jan 30, 2020
Messages
875
Thanks, it worked, I altered InitBlizzard successfully and everything runs fine.

I suppose Blizzard added some protections for the config and the main functions.
 
Level 12
Joined
Jan 30, 2020
Messages
875
Thanks for this important information, so does the fact that I use "do...............end" mean that I create a new scope ?

Thats probably where my mistake was then.
 
Status
Not open for further replies.
Top