• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Help in script

Scremo

Member
Joined
Jul 13, 2014
Messages
51
Reaction score
14
well I made this script to randomly spawn an item checking some conditions, it turns out that when it can't spawn in the checked condition it just doesn't spawn, I wanted to know how I could make it check the position and if it couldn't generate a new position and then yes spawn the item.

Lua:
function spawnItemOnArea(from, to, itemId, itemCount)    
    local spawnTilesNames = {
        "grass",
    }
   
    local randX, randY = math.random(from.x, to.x), math.random(from.y, to.y)
    local random = {x=randX, y=randY, z=7}
    local randomTile = Tile(random)
   
    local thing = randomTile:getThing()
        for _,v in ipairs(spawnTilesNames) do
            if (thing:getName() == v) and (randomTile:getThingCount() <= 1) then
                doCreateItem(itemId, itemCount, randomTile:getPosition())
            end
        end
    end
 
Solution
I need before creating the item to check if that position is authorized and only then create the item, if it is not authorized to generate a new position. Sorry for bad english.
So the tile having the name of 'grass' authorizes the tile..
If the tile is not authorized, do you want it to keep re-attempting new positions until one is found? Maybe with a cut-off of X number of attempts?

Well, that's my best guess, so here you go. xD

ezgif.com-gif-maker (1).gif

Lua:
if spawnItemOnArea(Position(240, 237, 7), Position(254, 247, 7), 2173, 1, 100) then
    print("Item successfully spawned on a random authorized tile, that did not have any other items on it.")
else
    print("No items were able to be spawned.")
end
Lua:
function spawnItemOnArea(from...
well I made this script to randomly spawn an item checking some conditions, it turns out that when it can't spawn in the checked condition it just doesn't spawn, I wanted to know how I could make it check the position and if it couldn't generate a new position and then yes spawn the item.
This just doesn't make sense.
I don't know exactly what you want to happen.

I can usually guess using context clues, but I honestly can't figure it out.

Please explain more clearly.

----------

as an aside ..

instead of this
Lua:
for _,v in ipairs(spawnTilesNames) do
    if (thing:getName() == v) and (randomTile:getThingCount() <= 1) then
        doCreateItem(itemId, itemCount, randomTile:getPosition())
    end
end
I'd suggest doing it like this.
Lua:
if table.contains(spawnTilesNames, thing:getName()) and randomTile:getThingCount() <= 1 then
    Game.createItem(itemId, itemCount, randomTile:getPosition())
end
 
I need before creating the item to check if that position is authorized and only then create the item, if it is not authorized to generate a new position. Sorry for bad english.
 
I need before creating the item to check if that position is authorized and only then create the item, if it is not authorized to generate a new position. Sorry for bad english.
So the tile having the name of 'grass' authorizes the tile..
If the tile is not authorized, do you want it to keep re-attempting new positions until one is found? Maybe with a cut-off of X number of attempts?

Well, that's my best guess, so here you go. xD

ezgif.com-gif-maker (1).gif

Lua:
if spawnItemOnArea(Position(240, 237, 7), Position(254, 247, 7), 2173, 1, 100) then
    print("Item successfully spawned on a random authorized tile, that did not have any other items on it.")
else
    print("No items were able to be spawned.")
end
Lua:
function spawnItemOnArea(from, to, itemId, itemCount, maxAttempts)    
    local spawnTilesNames = {
        "grass",
    }
    
    for i = 1, maxAttempts do
        local randX, randY = math.random(from.x, to.x), math.random(from.y, to.y)
        local randomPosition = Position(randX, randY, 7)
        local randomTile = Tile(randomPosition)
        
        local groundTile = randomTile:getGround()
        if table.contains(spawnTilesNames, groundTile:getName()) and randomTile:getThingCount() <= 1 then
            Game.createItem(itemId, itemCount, randomPosition)
            return true
        end
    end
    
    return false
end
 
Solution
Back
Top