Protect and serve (lua)

never noticed the check marks that will really help out thanks and thank you for attempting to help me it’s really appreciated

1 Like

I don’t know Lua, so please correct me if I’m wrong, but on line 13 you’re using table:insert with defend (an array) and friend (a string(?)).

Can you do that with an array/list or whatever those are called?

(You can tell that I’ve forgotten programming lol)

i think you’re correct how ever as a learner i don’t know the work around.

would friend.pos sort this?

Yes, I think friend.pos should work.

just to confirm it doesn’t i’ve moved on now i really don’t know how to solve that.

Oh :face_with_diagonal_mouth: sorry about that

Hopefully if you ever go back to this level we’ll be able to help you

1 Like

It’s a shame because the sample code wasn’t even translated to lua :skull:. That’s probably what made the level like 3 times more annoying.

Try this.

defend = {}
defend[1] = {x=98, y=28}
defend[2] = {x=84, y=7}

soldiers = {}
defenders = {}

local friends = hero:findFriends()
for i=1,#friends do
    local friend = friends[i]
    if friend and friend.type == "soldier" then
        hero:say("added to soldiers")
        table.insert(soldiers, friend)
    else
        hero:say("added to defend")
        table.insert(defenders, friend)
    end
end

while true do
    for i=1,#soldiers  do
        local soldier = soldiers[i]
        if soldier then 
            hero:command(soldier, "defend", defend[(i - 1) % #defend + 1])
        end
    end
    for i=1,#defenders do
        local defender = defenders[i]
        if defender then 
            hero:command(defender, "defend", defend[(i - 1) % #defend + 1])
        end
    end
end

tried this and again the hero just stands there says added to defend. i think the problem might be with the table.insert(soldiers, friend). i don’t know if its adding the soldier.type or soldier name. but i’ve tried to alter this by adding table.insert(soldiers, friend.type) etc. i’ve tried to do the code slowly adding if statements to try work out the kinks and its always a issue with table.insert. unfortunately for me i don’t know how to give up so untill this is solved i guess im gonna be trying to fix.

1 Like

Sorry, try this @Selston.

defend = {}
defend[1] = { x = 98, y = 28 }
defend[2] = { x = 84, y = 7 }

soldiers = {}

local friends = hero:findFriends()
for i = 1, #friends do
    local friend = friends[i][^1^][1]
    if friend.type == "soldier" then[^2^][2][^3^][3]
        table.insert(soldiers, friend)[^4^][4][^5^][5]
    else
        table.insert(defend, friend)[^5^][5][^6^][6]
    end
end

while true do
    for i = 1, #soldiers do[^7^][7]
        local soldier = soldiers[i][^8^][8]
        hero:command(soldier, "defend", defend[(i - 1) % #defend + 1])[^9^][9][^10^][10]
    end
    local enemy = hero:findNearestEnemy()
    if enemy then
        hero:attack(enemy)
    end
end

I am not familiar with lua, so I took your code, gave it to copilot, had him switch it to python and found the problem. There might be a couple of things you need to change, because of the switch, but other that that I hope this will help you. :smiley:

i’ll give it a go and let you know thank you. Just to clarify after the translation into python what was the issue as this might help me realise where the code or im going wrong

The defend table is initially populated with two positions at indices 1 and 2. However, in the loop where you’re adding friends, if the friend is not a soldier, you’re inserting them into the defend table. This could cause issues because you’re mixing different types of data (positions and friend objects) in the same table.

so i took on board what you said and to rectify this issue
i created a third table ally and added anything to this table
 that wasn't a "soldier" and left the defend table alone. 
local defend = {}
defend[1] = { x = 98, y = 28 }
defend[2] = { x = 84, y = 7 }

local soldiers = {}


local ally = {}

local friends = hero:findFriends()
for i=1,#friends do
    local friend = friends[i]
    if friend then
        hero:say("friend found")
        if friend.type == "soldier" then
            table.insert(soldiers, friend[i])
            hero:say("friend added to soldiers table")
        else
            table.insert(ally, friend[i])
            hero:say("added to ally table")
        end
    end
end

while true do
    for i=1,#soldiers do
        local soldier = soldiers[i]
        if soldier then
            hero:say("soldier found in table")
            hero:command(solder, "defend", ally[i])
        else
            hero:command(soldier, "move", defend[i])
        end
    end
end
so as a added side note i added a hero:say("") after most to see what was running
and what wasn't the if friend then hero:say("friend found") it does say friend found 
how ever when it gets to if friend.type == "soldier" then. it doesn't say anything nor 
does it say anthing for the else table.insert(ally, friend[i[)
I'm 8 days in now pushing 9 and loosing the will to live because i can't give up yet i feel so stupid lol.

I think I got it

You’re adding friend[i] to defend. Friend is already friends[i], so you just need to remove the [i] on the table.insert lines.

In other words, friend isn’t an array, so shouldn’t have an index.

1 Like

Never give up! Never lose hope! If you need encouragement, look up Philippians 4:13.

1 Like

I’m lost i officially give up i want to move on and learn more but can’t because of this freaking level im 16 days in now still stuck lol.

2 Likes