• 🏆 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!

Getting started with Lua scripting in Wc3 maps

Status
Not open for further replies.
Level 2
Joined
Feb 23, 2017
Messages
9
I'm not quite sure how to get started with a map using Lua as the primary scripting language. My initial method for exploring Jass was often creating a trigger and converting to custom text, but that doesn't seem to work with the scripting mode set to Lua. Does anyone know of documentation/tutorials or examples of Lua in wc3 maps? I know it only became supported as a beta feature a few days ago, but I'm eager to try it out.
 
Level 3
Joined
Mar 10, 2019
Messages
32
I am interested in this as well. Also does lua provide some kind of intellisense? Like Autocompletion for your typing?

Edit:
Wow this is a old post sorry didn't see!
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,539
EDIT:
There are other options to use besides pure Lua. Here's a link to C# and TypeScript.
These all compile to Lua so you're still required to use Lua as the supported language in your map:


HOW TO GET STARTED USING LUA:
Lua VSCode Integration <--- Follow the instructions in this
Then setup your map to be compatible with Lua (picture 1).
Then create a custom script in the trigger editor and paste your Lua code in it (picture 2).

You'll have to disable any existing triggers that use Custom Script before converting to Lua. This is because that Custom Script was using Jass and as of now Lua cannot be used with Jass/vJass.

You'll also have to update that Custom Script to follow Lua's syntax rules. Some examples, Lua doesn't require you to type "call" or "set" and I believe you don't have to null local variables (look into the Garbage Collector, you probably still want to null some things and clean up leaks). "null" also becomes "nil". "endif" becomes "end". NOT EQUAL TO changes from 5 != 10 to 5 ~= 10. These are just a few examples, you can get a much better understanding of this if you read through the Lua manual:

Functions no longer use "takes nothing returns nothing" and instead look like this:
Lua:
function HelloWorldTest()
    print("Hello World")
end

function CreateAUnit()
    local newUnit = CreateUnit(Player(0), FourCC("hfoo"), 0, 0, 270)
    DisplayUnitName(newUnit)
end

--Comments use this double hyphen instead of double slash //
--If you wanted to pass some data in the parameters you would do it like so:
function DisplayUnitName(unit)
    local stringExample = GetUnitName(unit)
    print(stringExample)
end

Also, rawcodes in Lua require the FourCC() prefix:
Lua:
FourCC("hfoo")
FourCC("A000")
If memory serves me right then apostrophes in the rawcode DON'T work either -> 'hfoo'

This link has some important information in it. I find some of the comments in there (see Eikonium's post) very resourceful when it comes to structure, formatting, desyncs, what to avoid doing, etc: Lua Submission Rules Discussion


This MIGHT be a workaround to the compatibility restrictions, I haven't tried it:
JNGP Lua Edition - Combining JASS/vJass/Lua
 

Attachments

  • lua 1.png
    lua 1.png
    2.8 MB · Views: 1,614
  • lua 2.png
    lua 2.png
    41.1 KB · Views: 1,674
Last edited:
Level 3
Joined
Mar 10, 2019
Messages
32
Thanks for your reply :) I will try that out as soon as I get reforged! (hopefully beta) Will look into lua in general in the mean time.
 
Level 1
Joined
Nov 26, 2019
Messages
3
How use it into the world editor? Simply i can write the code but i dont know how initiate script, where i can use it for events?
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,539
DISCLAIMER: I'm no Lua expert and I'm probably doing things wrong here! Wrong in the sense that it's bad practice, not wrong that it doesn't work.

Look at the picture "lua 2" that I uploaded above. You press "Control + U" to create a new Custom Script and then paste your code inside of it.

Also, you can call functions in GUI like this:
  • HelloWorld
    • Events
      • Player - Player 1 (Red) types a chat message containing test as An exact match
    • Conditions
    • Actions
      • Custom script: HelloWorld()

Here is an example of creating a trigger in Lua:
Lua:
function StartEffectOfAbility()
    local trigger = CreateTrigger()
    TriggerRegisterAnyUnitEventBJ(trigger, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    TriggerAddAction(trigger, function()
        local caster = GetTriggerUnit() --GetTriggerUnit references the Triggering Unit
        if GetSpellAbilityId() == FourCC('AUan') then --If the ability being cast is equal to Animate Dead
            KillUnit(caster) --Kill the triggering unit (casting unit) to show that it worked
        end
    end)
end
We can also create one function that runs ALL of our other functions. Then run this one function at the start of the map.
Lua:
function MapSetup()
    --Call our functions
    HelloWorld()
    StartEffectOfAbility()
end
I run MapSetup() at the start of the game, which then runs HelloWorld() and StartEffectOfAbility().
  • Lua Setup
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • Custom script: MapSetup()

I attached a map with a bunch of examples. Note that I'm no expert on Lua and there's probably a way to do all this without the need of a single GUI trigger.
 

Attachments

  • Lua Example Map.w3x
    17.8 KB · Views: 421
Last edited:
Level 3
Joined
Sep 1, 2009
Messages
37
Look at the picture "lua 2" that I uploaded above. You press "Control + U" to create a new Custom Script and then paste your code inside of it.

Also, you can call functions in GUI like this:
  • HelloWorld
    • Events
      • Player - Player 1 (Red) types a chat message containing test as An exact match
    • Conditions
    • Actions
      • Custom script: HelloWorld()

Here is an example of creating a trigger in Lua:
Lua:
function StartEffectOfAbility()
    local trigger = CreateTrigger()
    TriggerRegisterAnyUnitEventBJ(trigger, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    TriggerAddAction(trigger, function()
        local caster = GetTriggerUnit() --GetTriggerUnit references the Triggering Unit
        if GetSpellAbilityId() == FourCC('AUan') then --If the ability being cast is equal to Animate Dead
            KillUnit(caster) --Kill the triggering unit (casting unit) to show that it worked
        end
    end)
end
We can also create one function that runs ALL of our other functions. Then run this one function at the start of the map.
Lua:
function MapSetup()
    --Call our functions
    HelloWorld()
    StartEffectOfAbility()
end
I run MapSetup() at the start of the game, which then runs HelloWorld() and StartEffectOfAbility().
  • Lua Setup
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • Custom script: MapSetup()

I attached a map with a bunch of examples. Note that I'm no expert on Lua and there's probably a way to do all this without the need of a single GUI trigger.


How did you get your syntax to highlight like that? I came back to warcraft with the release of reforged beta but none of the custom modding tools seem to work, there is no syntax auto complete nor any highlighting and the trigger editor looks like the poster's above me.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,539
Yeah, that syntax highlighting is from the Hive code tags. The World Editor doesn't support that yet.

Some random things to note:

I do everything on VS Code then copy and paste it into the World Editor.

If you see weird characters when you try to paste your code then do the following: Delete the existing code (Control + A -> Delete) and then paste your code.

I wouldn't use the Reforged Editor until the issues have been fixed. Fortunately, you can work on your map in Classic and then play it on Reforged. Or import it from Classic to Reforged. However, I don't think you can import Reforged back to Classic.
 
Level 2
Joined
Apr 1, 2019
Messages
156
Hey one thing I've been doing being new to Lua also, is copying my GUI triggers in my Lua map to a new Jass map then I convert them to Jass and then I copy paste them to Visual Studios.

Its probably more work than just writing from scratch but for someone who is new to Lua it helps.
 
Level 3
Joined
Sep 1, 2009
Messages
37
Hey one thing I've been doing being new to Lua also, is copying my GUI triggers in my Lua map to a new Jass map then I convert them to Jass and then I copy paste them to Visual Studios.

Its probably more work than just writing from scratch but for someone who is new to Lua it helps.

Yea thats what Im doing except Ive never done anything in jass either so I work with the documentation from here lep/jassdoc too.
 
Status
Not open for further replies.
Top