How To Shield For X Seconds

For example, in “Storming the Farmhouse”, what if I want my hero to shield for 5 seconds when I place a black flag? I am using LUA, but Python, JavaScript, or help in any coding language would be appreciated.

In order to avoid posting a solution as is on the forum, allow me to post a specific snippet of code. I am only including the necessary parts that I believe are necessary to “shield for 5 seconds”, if such a thing is even possible at this point. (If it requires more advanced coding knowledge, please share!)

while true do
    local flag = self:findFlag()
    local green = self:findFlag("green")
    if green then
        self:pickUpFlag(green)
        self:shield(5)
    end    
end

As you can see, I attempted to pass 5 as the argument for the shield function (if I understand correctly, the shield function is predefined in the game.) In game, however, It seems like my hero only shields for 0.4 seconds (that’s how long the game says “shield” takes.)

I don’t think that there is such option. However, you can use hero.time method to control the time you want to do something.

you could do

while true do
    local flag = self:findFlag()
    local green = self:findFlag("green")
    if green then
        self:pickUpFlag(green)
        self:shield()
        self:say(wait)
        self:say(wait)
        self:say(wait)
        self:say(wait)
        self:say(wait)
    end    
end

That sounds promising, but two questions.

  1. Are you sure about self:say(wait)?
  2. Isn’t there a way to optimize that code and say something like self:wait(5) or something for wait 5 seconds? Again, I’m not sure about “wait”.

Yes, you can write self:wait(5) if you have the correct item (watch) for it.

Oh that is a way better option

@CodeOP Also you can try smth like: (sorry in Python, but you can catch the idea)

You’re trying to create a hero behavior in a game where the hero shields for a specified amount of time when a flag is placed. However, the game’s built-in functions might not directly support specifying the duration of the shield. Instead, the shield function in the game might be a simple toggle to turn the hero’s shield on or off.

If the game’s shield function doesn’t support a duration parameter, you’ll need to implement your own logic to simulate the shield for a specific duration. Here’s a modified version of your code that simulates a 5-second shield:

local isShielded = false
local shieldStartTime = 0
local shieldDuration = 5  -- The desired shield duration in seconds

while true do
    local flag = self:findFlag()
    local green = self:findFlag("green")

    if green then
        self:pickUpFlag(green)
        isShielded = true
        shieldStartTime = self:now()
    end

    if isShielded then
        local currentTime = self:now()

        -- Check if the shield duration has elapsed
        if currentTime - shieldStartTime >= shieldDuration then
            isShielded = false
        else
            self:shield()  -- Continue shielding
        end
    end
end

In this code, when the green flag is picked up, isShielded is set to true , and the shieldStartTime is recorded. Then, in the loop, it continuously checks if isShielded is true . If it is, it calculates the time that has passed since the shield was activated. If the shield has been active for 5 seconds, it sets isShielded to false . While isShielded is true , it keeps calling self:shield() to maintain the shield.

1 Like

Thanks and thank you everyone.

And darn, I really wish replies would work instead of making me have to delete and repost every single time I reply…

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.