• 🏆 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!
  • ✅ Time to vote for the top 3 models! The POLL for Hive's 6th HD Modeling Contest: Mechanical is now open! 📅 Poll close on July 16, 2024! 🔗 Cast your vote now!
  • ✅ The POLL for Hive's Texturing Contest #33 is OPEN! Vote for the TOP 3 SKINS! 🔗Click here to cast your vote!

[Solved] [Lua] Array of Units issue

Status
Not open for further replies.
Level 8
Joined
Jun 16, 2008
Messages
333
I am trying to have an array of units that I made manually but it doesn't work. You guys are my last hope!

I tried nextCirc = {"gg_unit_gcop_0039", "gg_unit_gcop_0042", "gg_unit_gcop_0044", "gg_unit_gcop_0049"}
and that without ""

Lua:
function HeroTele()
        playerTele = {}
        nextCirc  = {"gg_unit_gcop_0039", "gg_unit_gcop_0042", "gg_unit_gcop_0044", "gg_unit_gcop_0049"}
        for i=1,15 do
            playerTele[i] = 0
        end
        local coP = GetUnitsOfPlayerAndTypeId(Player(PLAYER_NEUTRAL_PASSIVE), FourCC("bcop"))
        ForGroup(coP, CircleofPower)
        DestroyGroup(coP)
 end

function CircleofPower()
        local EnumcoP = GetEnumUnit()
        local coPCollision = CreateTrigger() 
        TriggerRegisterUnitInRangeSimple(coPCollision, 86, EnumcoP)
        TriggerAddAction(coPCollision, function() coPTele(EnumcoP) end)
end

function coPTele(a)
        local trigUnit =GetOwningPlayer(GetTriggerUnit()) 
        local playerId = GetPlayerId(trigUnit) + 1
        if trigUnit ~= Player(15) then
            SetUnitOwner(a, trigUnit, TRUE)
            local checkCircs = GetUnitsOfPlayerAndTypeId(trigUnit, FourCC("bcop"))
            if BlzGroupGetSize(checkCircs) >= 2 then
                ForGroup(checkCircs, copOwnerChange)
                DestroyGroup(checkCircs)
                if playerTele[playerId] == 0 then
                    SetUnitPositionLocFacingBJ(GetTriggerUnit(), GetRectCenter(GetUnitLoc(_G[string.format(nextCirc[playerId])]), 75))
                end
            end
        end
end

function copOwnerChange()
    SetUnitOwner(GetEnumUnit(), Player(PLAYER_NEUTRAL_PASSIVE), TRUE)
end

I tried to create the the array "nextCirc" without the quotes around the units and ran it without the _g[string.format and it didn't work.


Edit: I added GetRectCenter() infront of GetUnitLoc() but it sends me way offset
 
Last edited:
Level 9
Joined
Mar 26, 2017
Messages
376
You need to remove the parantheses from gg_unit as these are unit variables.

Lua:
_G[string.format(nextCirc[playerId])]
This part is not correct. What you are trying to do is find an object called 'nextCirc[1]' etc, which doesn't exist.
The statement nextCirc = {...} creates one list object called nextCirc.

You can find element in the nextCirc list like this:
Lua:
nextCirc[playerId]

_G["object name"..Id] will only be useful when iterating over several named variables.


Also, you cannot pass a location to GetRectCenter.
 
Level 8
Joined
Jun 16, 2008
Messages
333
You need to remove the parantheses from gg_unit as these are unit variables.

Lua:
_G[string.format(nextCirc[playerId])]
This part is not correct. What you are trying to do is find an object called 'nextCirc[1]' etc, which doesn't exist.
The statement nextCirc = {...} creates one list object called nextCirc.

You can find element in the nextCirc list like this:
Lua:
nextCirc[playerId]

_G["object name"..Id] will only be useful when iterating over several named variables.


Also, you cannot pass a location to GetRectCenter.
I made the changes but for nextCirc[playerId] I keep getting nil if I print it out or print out
GetUnitLoc(nextCirc[playerId])

and what do you mean by nextCirc[1] doesn't exist?
 

Uncle

Warcraft Moderator
Level 67
Joined
Aug 10, 2018
Messages
6,889
playerTele = {} can be changed to playerTele = __jarray(0)

This gives it the default value of 0.

And you probably want to create the Array like this:
nextCirc = {}
nextCirc[1] = gg_unit_gcop_0039
nextCirc[2] = gg_unit_gcop_0042
nextCirc[3] = gg_unit_gcop_0044
nextCirc[4] = gg_unit_gcop_0049

What's the goal of this function anyway? I don't really get what you're trying to do.

And I THINK you need to replace TRUE with true:
Lua:
SetUnitOwner(a, trigUnit, TRUE)
--That should be:
SetUnitOwner(a, trigUnit, true)
 
Level 8
Joined
Jun 16, 2008
Messages
333
playerTele = {} can be changed to playerTele = __jarray(0)

This gives it the default value of 0.

And you probably want to create the Array like this:
nextCirc = {}
nextCirc[1] = gg_unit_gcop_0039
nextCirc[2] = gg_unit_gcop_0042
nextCirc[3] = gg_unit_gcop_0044
nextCirc[4] = gg_unit_gcop_0049

What's the goal of this function anyway? I don't really get what you're trying to do.

And I THINK you need to replace TRUE with true:
Lua:
SetUnitOwner(a, trigUnit, TRUE)
--That should be:
SetUnitOwner(a, trigUnit, true)

Are you sure the unit variables gg_unit_gcop_0039 etc are valid?

I think Pr114 is right uncle, it is still nil.

But I am trying to get it so a unit has to enter range with 2 circle of powers and once both are touched, the unit teleport to the next respected one. But if you want to try my map be my guest. You basically helped me with a lot of triggers so you are going to get credit on my map. Don't hate tho because it is a maze... This is my first map since 2010-2011 and I'm trying to learn LUA as I go.
 
Level 8
Joined
Jun 16, 2008
Messages
333
Last edited:
You ask if gg's get created if one trigger without an event is created? I'm not sure I understand, can you elaborate?
But the answer is no, I guess, as long as there's no GUI reference, see the link above.

or is there another way?
I linked to w3x tools, which you could use to generate those globals.
 
Level 8
Joined
Jun 16, 2008
Messages
333
You ask if gg's get created if one trigger without an event is created? I'm not sure I understand, can you elaborate?
But the answer is no, I guess, as long as there's no GUI reference, see the link above.


I linked to w3x tools, which you could use to generate those globals.
So I did some testing, GUI trigger needs to be triggered and if you delete the GUI trigger, it no longer exist.

Also, W3X doesn't have a Lua option so ;(
 
Also, W3X doesn't have a Lua option so ;(
Plain text is created, it's not JASS specific. It works depending on input. If you tell me what output code you need exactly, I could help.

So I did some testing, GUI trigger needs to be triggered and if you delete the GUI trigger, it no longer exist.
So what I tested in the linked thread is wrong, that reference is kept if it's being used in code at same time?
 
Last edited:
Level 9
Joined
Mar 26, 2017
Messages
376
A preplaced unit is only bound to a 'gg_unit' variable' if it is referenced in one of your world editor Unit variables.

Oops, saw this mentioned earlier in this topic.
 
Last edited:
Level 8
Joined
Jun 16, 2008
Messages
333
What's the goal of this function anyway? I don't really get what you're trying to do.
this is what I am trying to accomplish...
test.gif


well what I accomplished.... but unit doesn't turn instantly if you know any way around that issue?

My Code
Lua:
function HeroTele()
        playerTele = {}
        for i=1,15 do
            playerTele[i] = 1
        end
        local coP = GetUnitsOfPlayerAndTypeId(Player(PLAYER_NEUTRAL_PASSIVE), FourCC("bcop"))
        ForGroup(coP, CircleofPower)
        DestroyGroup(coP)
end

function CircleofPower()
        local EnumcoP = GetEnumUnit()
        local coPCollision = CreateTrigger()
        TriggerRegisterUnitInRangeSimple(coPCollision, 86, EnumcoP)
        TriggerAddAction(coPCollision, function() coPTele(EnumcoP) end)
end

function coPTele(a)
        local trigUnit =GetOwningPlayer(GetTriggerUnit())
        local playerId = GetPlayerId(trigUnit) + 1
        local trigUnitTele = playerTele[playerId]
        if trigUnit ~= Player(15) then
            print(GetUnitLoc(udg_TeleCOP[playerId]))
            SetUnitOwner(a, trigUnit, TRUE)
            local checkCircs = GetUnitsOfPlayerAndTypeId(trigUnit, FourCC("bcop"))
            if BlzGroupGetSize(checkCircs) >= 2 then
                ForGroup(checkCircs, copOwnerChange)
                DestroyGroup(checkCircs)
             
                if playerTele[playerId] <=5 then
                    SetUnitFacingTimed(GetTriggerUnit(), 270, 0.00)
                    SetCameraPositionLocForPlayer(trigUnit, GetUnitLoc(udg_TeleCOP[trigUnitTele]))
                    SetUnitPositionLoc(GetTriggerUnit(), GetUnitLoc(udg_TeleCOP[trigUnitTele]))
                    ReviveHeroLoc(GetTriggerUnit(), GetUnitLoc(udg_TeleCOP[trigUnitTele]), false)
                    playerTele[playerId] = playerTele[playerId] + 1
                end
            end
        end
end

function copOwnerChange()
    SetUnitOwner(GetEnumUnit(), Player(PLAYER_NEUTRAL_PASSIVE), TRUE)
end
and TRUE works just so you know and also false and FALSE. I don't think Lua cares

playerTele = __jarray(0)
I didn't try this as well because I would have to change the whole +1 and array[1] to array[0]
 
Last edited:

Uncle

Warcraft Moderator
Level 67
Joined
Aug 10, 2018
Messages
6,889
I believe they added a make unit face angle instantly function.

And i'm not sure what you mean about changing array[1] to array[0]. All __jarray(0) does is create a table (just like using Var = {}) with the added benefit of setting the default value of all Elements in that table to your listed value. You can set it to any value you want meaning it doesn't have to be an Integer either.

Example:
SomeBoolean = __jarray(false)

Will result in:
SomeBoolean[1] = false
SomeBoolean[2] = false
SomeBoolean[3] = false
etc...

Otherwise, their default values would be Nil.
 
Level 8
Joined
Jun 16, 2008
Messages
333
I believe they added a make unit face angle instantly function.

And i'm not sure what you mean about changing array[1] to array[0]. All __jarray(0) does is create a table (just like using Var = {}) with the added benefit of setting the default value of all Elements in that table to your listed value. You can set it to any value you want meaning it doesn't have to be an Integer either.

Example:
SomeBoolean = __jarray(false)

Will result in:
SomeBoolean[1] = false
SomeBoolean[2] = false
SomeBoolean[3] = false
etc...

Otherwise, their default values would be Nil.
okay that good to know I can put any value in it.... I just thought it was 0 but that makes it much easier and faster game wise

ps: im talking out my ass

BlzSetUnitFacingEx(GetTriggerUnit(), 270)
is the trigger
 
Last edited:
Status
Not open for further replies.
Top