• 🏆 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 endblock error

Status
Not open for further replies.
Level 2
Joined
Jun 8, 2020
Messages
11
I have a Lua script which I wanna import in my map, but when I'm trying to save the map it says "endblock error at line 547" while the test map has 546 lines
I'm a noob at Lua, I know zero
Also I noticed that if I delete everything and leave a single function it starts giving me another error, "expected takes" in that function
And right now I'm very confused, maybe someone can help me with that issue

Here's the code, in the attached files
 
Level 2
Joined
Jun 8, 2020
Messages
11
Here
P.S:It's supposed to be a WASD moving system + camera control but it doesn't work
The core code was Jass and I used a tool to convert it, maybe that's why it has some mistakes
 

Attachments

  • LuaScriptORIG.txt
    11.4 KB · Views: 48

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,537
1) Your map needs to be in Lua mode.
2) That Jass -> Lua tool doesn't always work perfectly.
3) The variable Anims had a "1" after it for some reason. This was throwing an error multiple times throughout the code. Note that I renamed it to WalkAnim.

I fixed #3 and removed the AbilityLevel checks from the ConditionToMove function because they seemed unnecessary.
Lua:
COUNT_OF_PLAYERS = 12   --type integer
INVERSE = 1             --type integer
Units = {}              --type unit
Up = {}                 --type boolean
Down = {}               --type boolean
Left = {}               --type boolean
Right = {}              --type boolean
ApplyCam = {}           --type boolean
ApplyControl = {}       --type boolean
WalkAnim = {}           --type integer
Around = {}             --type boolean
FlagAround = {}         --type boolean
ResetFlag = {}          --type boolean
function ConditionToMove(u)
    return (GetUnitState(u, UNIT_STATE_LIFE) >= 0.45) -- i deleted the ability checks
end
function UpUp_Func(i)
    Up[i] = false
    if GetUnitState(Units[i], UNIT_STATE_LIFE) >= 0.45 then
        SetUnitTimeScale(Units[i], 1)
        SetUnitAnimation(Units[i], "stand")
    end
    if (Down[i]) and (Around[i]) then
        FlagAround[i] = true
        SetUnitFacingTimed(Units[i], (GetUnitFacing(Units[i]) + 180), 0.01)
    end
    if ResetFlag[i] then
        ResetFlag[i] = false
        if GetUnitState(Units[i], UNIT_STATE_LIFE) >= 0.45 and ApplyControl[i] and WalkAnim[i] then
            SetUnitAnimationByIndex(Units[i], WalkAnim[i])
        end
        if ( not Up[i]) and (Around[i]) then
            SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i]) / GetUnitDefaultMoveSpeed(Units[i]))
            FlagAround[i] = true
            SetUnitFacingTimed(Units[i], (GetUnitFacing(Units[i]) + 180), 0)
        else
            SetUnitTimeScale(Units[i], -GetUnitMoveSpeed(Units[i]) / GetUnitDefaultMoveSpeed(Units[i]))
        end
    end
end
function DownUp_Func(i)
    Down[i] = false
    if GetUnitState(Units[i], UNIT_STATE_LIFE) >= 0.45 then
        SetUnitTimeScale(Units[i], 1)
        SetUnitAnimation(Units[i], "stand")
    end
    if FlagAround[i] then
        FlagAround[i] = false
        SetUnitFacingTimed(Units[i], (GetUnitFacing(Units[i]) + 180), 0)
    end
    if ResetFlag[i] then
        ResetFlag[i] = false
        if GetUnitState(Units[i], UNIT_STATE_LIFE) >= 0.45 and ApplyControl[i] then
            if (WalkAnim[i]) then
                SetUnitAnimationByIndex(Units[i], WalkAnim[i])
            end
            SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i]) / GetUnitDefaultMoveSpeed(Units[i]))
        end
    end
end
function LeftUp_Func(i)
    Left[i] = false
    if ( not Up[i]) and ( not Down[i]) then
        IssueImmediateOrder(Units[i], "stop")
    end
end
function RightUp_Func(i)
    Right[i] = false
    if ( not Up[i]) and ( not Down[i]) then
        IssueImmediateOrder(Units[i], "stop")
    end
end
function GetUnit(p)
    return Units[GetPlayerId(p)]
end
function RemoveAssociate(p)
    Units[GetPlayerId(p)] = nil
end
-- unit, player, walk anim index, around?
function Associate(u, p, a, b)
    if (Units[GetPlayerId(p)] ~= nil) then
        SetUnitTimeScale(Units[GetPlayerId(p)], 1)
        SetUnitAnimation(Units[GetPlayerId(p)], "stand")
    end
    Units[GetPlayerId(p)] = u
    WalkAnim[GetPlayerId(p)] = a
    Around[GetPlayerId(p)] = b
    FlagAround[GetPlayerId(p)] = false
end
function RemoveControlUnit(p)
    RemoveUnit(Units[GetPlayerId(p)])
    Units[GetPlayerId(p)] = nil
end
-- player, apply controls boolean
function SetControl(p, b)
    local i = GetPlayerId(p)
    ApplyControl[i] = b
    if  not b then
        UpUp_Func(i)
        DownUp_Func(i)
        RightUp_Func(i)
        LeftUp_Func(i)
    end
end
-- player, lock camera boolean
function SetCamera(p, b)
    ApplyCam[GetPlayerId(p)] = b
    if (GetLocalPlayer() == p) then
        if  not b then
            ResetToGameCamera(0)
            CameraSetSmoothingFactor(0)
        else
            SetCameraTargetController(Units[GetPlayerId(p)], 0, 0, false)
            CameraSetSmoothingFactor(1)
        end
    end
end
function GetControl(p)
    return ApplyControl[GetPlayerId(p)]
end
function GetCamera(p)
    return ApplyCam[GetPlayerId(p)]
end
function UpUp_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    UpUp_Func(i)
end
function UpDown_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    Up[i] = true
    if GetUnitState(Units[i], UNIT_STATE_LIFE) >= 0.45 and ApplyControl[i] then
        if (WalkAnim[i]) then
            SetUnitAnimationByIndex(Units[i], WalkAnim[i])
        end
        SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i]) / GetUnitDefaultMoveSpeed(Units[i]))
    end
end
function DownUp_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    DownUp_Func(i)
end
function DownDown_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    Down[i] = true
    if GetUnitState(Units[i], UNIT_STATE_LIFE) >= 0.45 and ApplyControl[i] and WalkAnim[i] then
        SetUnitAnimationByIndex(Units[i], WalkAnim[i])
    end
    if ( not Up[i]) and (Around[i]) then
        SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i]) / GetUnitDefaultMoveSpeed(Units[i]))
        FlagAround[i] = true
        SetUnitFacingTimed(Units[i], (GetUnitFacing(Units[i]) + 180), 0)
    else
        SetUnitTimeScale(Units[i], -GetUnitMoveSpeed(Units[i]) / GetUnitDefaultMoveSpeed(Units[i]))
    end
end
function RightUp_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    RightUp_Func(i)
end
function RightDown_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    Right[i] = true
end
function LeftUp_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    LeftUp_Func(i)
end
function LeftDown_Actions()
    local i = GetPlayerId(GetTriggerPlayer())
    Left[i] = true
end
function Move_Actions()
    local X
    local Y
    local Xh
    local Yh
    local bx
    local by
    local Angle
    local i = 0
    local iflag = 0
    local bflag = false
    while true do
        if i >= COUNT_OF_PLAYERS then break end
        if (Units[i] ~= nil and ApplyControl[i]) then
            if Up[i] and ( not Down[i]) and ConditionToMove(Units[i]) then
                bflag = true
                iflag = 1
            elseif ( not Up[i]) and Down[i] and ConditionToMove(Units[i]) then
                bflag = true
                if (FlagAround[i]) then
                    iflag = 1
                else
                    iflag = -1
                end
            else
                bflag = false
            end
            if bflag then
                Angle = GetUnitFacing(Units[i])
                X = GetUnitX(Units[i]) + iflag * (GetUnitMoveSpeed(Units[i]) / 100) * Cos(Angle * bj_DEGTORAD)
                Y = GetUnitY(Units[i]) + iflag * (GetUnitMoveSpeed(Units[i]) / 100) * Sin(Angle * bj_DEGTORAD)
                Xh = GetUnitX(Units[i])
                Yh = GetUnitY(Units[i])
                SetUnitPosition(Units[i], X, Y)
                ClearTextMessages()
                if (RAbsBJ(GetUnitX(Units[i]) - X) > 0.5) or (RAbsBJ(GetUnitY(Units[i]) - Y) > 0.5) then
                    SetUnitPosition(Units[i], X, Yh)
                    bx = RAbsBJ(GetUnitX(Units[i]) - X) <= 0.5
                    SetUnitPosition(Units[i], Xh, Y)
                    by = RAbsBJ(GetUnitY(Units[i]) - Y) <= 0.5
                    if bx then
                        SetUnitPosition(Units[i], X, Yh)
                    elseif by then
                        SetUnitPosition(Units[i], Xh, Y)
                    else
                        SetUnitPosition(Units[i], Xh, Yh)
                    end
                end
            end
            if Right[i] and ( not Left[i]) and ConditionToMove(Units[i]) then
                bflag = true
                iflag = 1
            elseif ( not Right[i]) and Left[i] and ConditionToMove(Units[i]) then
                bflag = true
                iflag = -1
            else
                bflag = false
            end
            if bflag then
                if Down[i] then
                    SetUnitFacingTimed(Units[i], (GetUnitFacing(Units[i]) + iflag * INVERSE * (8.00)), 0.01)
                else
                    SetUnitFacingTimed(Units[i], (GetUnitFacing(Units[i]) - iflag * (8.00)), 0.01)
                end
            end
        end
        i = i + 1
    end
end
function Animation_Actions()
    local i = 0
    while true do
        if i >= COUNT_OF_PLAYERS then break end
        if Up[i] and Down[i] and  not ResetFlag[i] then
            ResetFlag[i] = true
            SetUnitAnimation(Units[i], "stand")
        end
        if  not ResetFlag[i] then
            if (Up[i] or Down[i]) and ConditionToMove(Units[i]) and WalkAnim[i] then
                SetUnitAnimationByIndex(Units[i], WalkAnim[i])
            end
        end
        i = i + 1
    end
end
function Camera_Actions()
    local i = 0
    local Z1
    local Z2
    local Angle1
    local offset
    local Angle2
    while true do
        if i >= COUNT_OF_PLAYERS then break end
        if (Units[i] ~= nil) and (ApplyCam[i]) then
            MoveLocation(l, GetUnitX(Units[i]), GetUnitY(Units[i]))
            Z1 = GetLocationZ(l)
            MoveLocation(l, GetUnitX(Units[i]) - 400 * Cos(GetUnitFacing(Units[i]) * bj_DEGTORAD), GetUnitY(Units[i]) - 400 * Sin(GetUnitFacing(Units[i]) * bj_DEGTORAD))
            Z2 = GetLocationZ(l)
            if (Z1 + 100 >= Z2) then
                offset = GetCameraField(CAMERA_FIELD_ZOFFSET) + GetUnitFlyHeight(Units[i]) + 150 + Z1 - GetCameraEyePositionZ()
                Angle1 = -15
            else
                offset = R2I(GetCameraField(CAMERA_FIELD_ZOFFSET)) + 300 + GetUnitFlyHeight(Units[i]) + Z2 - R2I(GetCameraEyePositionZ())
                Angle1 = -50
            end
            Angle2 = GetUnitFacing(Units[i])
            if (Down[i]) and ( not Up[i]) and (Around[i]) then
                Angle2 = Angle2 + 180
            end
            if (GetLocalPlayer() == Player(i)) then
                SetCameraField(CAMERA_FIELD_ZOFFSET, offset, 0.25)
                SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, 150, 0.25)
                SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, Angle1, 0.25)
                SetCameraField(CAMERA_FIELD_FIELD_OF_VIEW, 100, 0.1)
                SetCameraField(CAMERA_FIELD_FARZ, bj_CAMERA_DEFAULT_FARZ, 0.25)
                SetCameraField(CAMERA_FIELD_ROTATION, Angle2, 0.25)
            end
        end
        i = i + 1
    end
end
function init()
    l = Location(0, 0)
    local Count = 0
    -- create triggers
    local UpDown = CreateTrigger()
    local UpUp = CreateTrigger()
    local DownDown = CreateTrigger()
    local DownUp = CreateTrigger()
    local RightDown = CreateTrigger()
    local RightUp = CreateTrigger()
    local LeftDown = CreateTrigger()
    local LeftUp = CreateTrigger()
    while true do
        if Count >= COUNT_OF_PLAYERS then break end
        BlzTriggerRegisterPlayerKeyEvent(UpDown, Player(Count), OSKEY_W, 0, true)
        BlzTriggerRegisterPlayerKeyEvent(UpUp, Player(Count), OSKEY_W, 0, false)
        BlzTriggerRegisterPlayerKeyEvent(DownDown, Player(Count), OSKEY_S, 0, true)
        BlzTriggerRegisterPlayerKeyEvent(DownUp, Player(Count), OSKEY_S, 0, false)
        BlzTriggerRegisterPlayerKeyEvent(LeftDown, Player(Count), OSKEY_A, 0, true)
        BlzTriggerRegisterPlayerKeyEvent(LeftUp, Player(Count), OSKEY_A, 0, false)
        BlzTriggerRegisterPlayerKeyEvent(RightDown, Player(Count), OSKEY_D, 0, true)
        BlzTriggerRegisterPlayerKeyEvent(RightUp, Player(Count), OSKEY_D, 0, false)
        Count = Count + 1
    end
 
    -- create actions
    TriggerAddAction(UpDown, UpDown_Actions)
    TriggerAddAction(UpUp, UpUp_Actions)
    TriggerAddAction(DownDown, DownDown_Actions)
    TriggerAddAction(DownUp, DownUp_Actions)
    TriggerAddAction(RightDown, RightDown_Actions)
    TriggerAddAction(RightUp, RightUp_Actions)
    TriggerAddAction(LeftDown, LeftDown_Actions)
    TriggerAddAction(LeftUp, LeftUp_Actions)
 
 
    TimerStart(CreateTimer(), 0.01, true, Move_Actions)
    TimerStart(CreateTimer(), 0.2, true, Animation_Actions)
    TimerStart(CreateTimer(), 0.01, true, Camera_Actions)
end
Note that I manually call the init() function in my example map.
 

Attachments

  • WASD lua.w3m
    20.7 KB · Views: 16
Last edited:
Level 2
Joined
Jun 8, 2020
Messages
11
Upd:had more questions but found a solution for each of them
 
Last edited:
Status
Not open for further replies.
Top