• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Problem with LUA

Level 4
Joined
Jun 6, 2015
Messages
77
Ok so I am trying to learn LUA but I cant get anything to work. Yes I have changed to LUA in the Map Options. Example trigger:

Lua:
function StartEffectOfAbility()
    local trigger = CreateTrigger()
    TriggerRegisterAnyUnitEventBJ(trigger, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    TriggerAddAction(trigger, function()
        local caster = GetTriggerUnit()
        if GetSpellAbilityId() == FourCC("AHwe") then
            KillUnit(caster)
        end
    end)
end

This should kill my unit when he uses Summon Water Element. However, nothing happens, as with any other code.
I use Visual Studio Code and copy the code from there to a new custom script.

I thought I just did the codes wrong for a like 2 days now (maybe I did? Is the code above wrong?) but now it just seems that absolutely nothing works.
I am trying to learn programming in general, so I thought wc3 would be a great place to learn because here I can actually create something. I started with python the other day but I couldnt find anything to "create". In wc3, I can create anything.

I am aware that the solution is probably super easy, but I cant seem to find it.

If someone could tell me what I am doing wrong in the above script I'd be very happy.

Thanks in advance.
 
Level 21
Joined
Jul 10, 2009
Messages
481
Your code is absolutely correct.
My best guess is that you forgot to execute StartEffectOfAbility().
You have defined the function, but it won't do anything, until you actually execute it.

Just to try, could you create a simple GUI trigger like this:
  • someTrigger
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: StartEffectOfAbility()
That would execute your function once at map init, so the trigger should exist and work upon game start.
 
Level 21
Joined
Jul 10, 2009
Messages
481
Lua in Wc3 has the disadvantage of that it doesn't provide an easy way to setup triggers (in contrast to GUI and Jass), which you need to some extend to start executing anything.
One way to work with it is to still rely on GUI map init triggers to run the functions that create your own triggers (as shown above).
Another way that I strongly recommend is to use the resource [Lua] Global Initialization
That resource provides the hook onTriggerInit (among others), which can execute any function at a particular point during map loading screen. Instead of creating a GUI trigger, it would have allowed you to write the following:
Lua:
function StartEffectOfAbility()
    local trigger = CreateTrigger()
    TriggerRegisterAnyUnitEventBJ(trigger, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    TriggerAddAction(trigger, function()
        local caster = GetTriggerUnit()
        if GetSpellAbilityId() == FourCC("AHwe") then
            KillUnit(caster)
        end
    end)
end
onTriggerInit(StartEffectOfAbility)
 
Level 4
Joined
Jun 6, 2015
Messages
77
Nice, that is an appropriate resource as I was kind of planning on making a 100% LUA map without even a single GUI trigger (for educational purposes). I will not download and use other's spells and systems as I don't learn much from that, but I consider stuff like Global Initialization resource OK as it only makes the coding more appropriate.
Terrain template is being used ofc as I dont want to waste time placing rocks and trees all day.:plol:

Edit: Btw while you're here do you know any good basic tutorials I should look up. There are some things that I don't get, like why people start their scripts with "do" and some don't (pun intended).

Anyway I copied the Global Init script into my map but the only thing it does is making my map impossible to "test". Before loading screen it redirects me to game menu. This even happens after deleting the script so had to exit without saving and reopening my map. I think I should stay away from such "advanced" systems for now, untill I have more knowledge.
 
Last edited:
Level 21
Joined
Jul 10, 2009
Messages
481
My personal Lua education mainly comes from reading through Programming in Lua 4th Edition. If you already have beginner programming knowledge, it will help a ton and teach all the Lua-isms that you need for your daily coding.

Coding in Wc3 is another topic that comes on top. There are many quirks specifically for this game that affect the way you need to write your Lua code.
One example is that certain Lua libraries have been deactivated in Wc3 (supposable for security reasons), such as the io and the debug-library. I've written down some of these things here.

To my knowledge, there currently is no comprehensive Lua introduction tutorial on hive. The information available is rather scattered across the site.

The purpose of do ... end is to define a scope block for your local variables.
You maybe know that literally all variables in your code should be made local, i.e. avoid global scope as much as possible (local scope avoids name clashes and makes execution of your code faster).
As a rule of thumb, the scope of local variables end at the next end-keyword in your code, so do... end is used to restrict the space where they can be accessed.
As a consequence, do... end isn't necessary to use, where you don't have local variables, so some resources can rightfully skip on it.

Another thing that you should know is that all code that you just write "freely" in your scripts (i.e. all code that is not part of any function) is directly executed at an early step during loading screen.
Wc3 is not fully loaded at that point, so avoid using Warcraft API functions in free code.
Whenever you need the Wc3 API, write that code into some function and make sure it will not get executed before the Map Init event (or just game start, if you are unsure).
 
Last edited:
Level 4
Joined
Jun 6, 2015
Messages
77
Thank you for your taking your time in writing all this. I do have some experience with MatLab as I study mathematics. But I am not "programmer". Though I have learnt very much in just a few days with LUA. I will just keep experimenting with Wc3 LUA modding untill it sits naturally in my fingers before anything else. I have been doing GUI triggering for half my life, but I already see how powerful LUA (and jazz) is compared to GUI, especially having the ability to have local variables.
 
Top