• 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!

Solved how use table.insert ?

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,484
Solutions
9
Reaction score
217
hello, I'm making a script to summon some monsters
Code:
local config = {
    [1] = {bossName = "boss1", bossPosition = Position(xxxxxx)},
    [2] = {bossName = "boss2", bossPosition = Position(xxxxxx)},
    [3] = {bossName = "boss3", bossPosition = Position(xxxxxx)}
}
function onThink(interval, lastExecution, thinkInterval)
    if math.random(100) > 90 then
        return true
    end
    local rand = config[math.random(3)]
    local name = rand.bossName
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..name..' boss missing map.')
        return true
    end   
    print('>> The boss: ' ..monster:getName() .. ' was summoned.')
    return true
end
I want to make it summon just one boss per server start, never summon the same boss, how make it? with table ? inserting in the table and check if is there ir not
 
table.insert(table, value)
Code:
local t = {}
local v = 100

Code:
table.insert(t, v)
is similiar to
Code:
t[#t + 1] = v
 
table.insert(table, value)
Code:
local t = {}
local v = 100

Code:
table.insert(t, v)
is equal to
Code:
t[#t + 1] = v
I'm sorry for be noobie, but how I can use it in the script ?
please tell me, later I'll understand how it work
 
I'm sorry for be noobie, but how I can use it in the script ?
please tell me, later I'll understand how it work

Wel, table.insert does nothing more but insert a value into a table.

You can make a global table, e.g:
summonedBossMonsters = { }

And when you use this script to summon a boss monster (assuming they have unique names and appear only 1 at a time since they're bosses), you can put it into this table with
table.insert(summonedBossMonsters, "Name of that boss")

Then you can use isInArray to check if the monster is in that array (meaning that it's alive), like

if isInArray(summonedBossMonsters, "Name of boss") then
-- monster exists, don't do anything
else
-- monster doesn't exist, do something
end

Make sure to make an onDeath script for your bosses that will use this system, to remove them from this table with table.remove when they die.

However, to avoid all of this complication, you could go with something simpler like:

if getCreatureByName("Name of that Boss") ~= 0 then
-- monster exists, don't do anything
else
-- monster doesn't exist, do something here
end

No tables used here, though you will probably have to loop through a single array that holds names of all possible bosses or something like that. Don't know how much about LUA you know, but there's a few different ways of doing what you want, it's up to you to pick what's the most optimal and easiest to do.
(Keep in mind this is 0.3.6 terminology, you'll probably have to use the tfs 1.0+ equivalent function of what I wrote)
 
I'm sorry for be noobie, but how I can use it in the script ?
please tell me, later I'll understand how it work
Code:
local t = {} -- create an empty table
local v = 100 -- create a variable and assign it the value of 100
When you see a # this is know as the length operator, it returns the size of the table, it can also be used on a string or string variable to return the length of the string.
Code:
local str = "this is a string"
print(#str) -- print the length of the string
-- prints 16
print(#"this is a string")
-- prints 16
local t = {} -- create an empty table
local v = 100 -- create a variable and assign it the value of 100
t[#t + 1] = v -- assign the value stored in v to the last index of t
print(#t) -- print the size of the table
-- prints 1

table.insert(table, [index/optional], value)
Code:
local t = {} -- create an empty table
local v = 100 -- create a variable and assign it the value of 100

table.insert(t, v) -- insert the value of v inside of t at its last index
-- using the optional parameter
local t = {100, 200, 300} -- t contains 3 indexes of values
table.insert(t, 3, 250) -- insert 250 at index 3 inside of t
for k, v in ipairs(t) do
    print(k, v)
end
--[[
output
1    100
2    200
3    250
4    300
]]
 
Last edited:
Wel, table.insert does nothing more but insert a value into a table.

You can make a global table, e.g:
summonedBossMonsters = { }

And when you use this script to summon a boss monster (assuming they have unique names and appear only 1 at a time since they're bosses), you can put it into this table with
table.insert(summonedBossMonsters, "Name of that boss")

Then you can use isInArray to check if the monster is in that array (meaning that it's alive), like

if isInArray(summonedBossMonsters, "Name of boss") then
-- monster exists, don't do anything
else
-- monster doesn't exist, do something
end

Make sure to make an onDeath script for your bosses that will use this system, to remove them from this table with table.remove when they die.

However, to avoid all of this complication, you could go with something simpler like:

if getCreatureByName("Name of that Boss") ~= 0 then
-- monster exists, don't do anything
else
-- monster doesn't exist, do something here
end

No tables used here, though you will probably have to loop through a single array that holds names of all possible bosses or something like that. Don't know how much about LUA you know, but there's a few different ways of doing what you want, it's up to you to pick what's the most optimal and easiest to do.
(Keep in mind this is 0.3.6 terminology, you'll probably have to use the tfs 1.0+ equivalent function of what I wrote)
oh good, thank you for the answer, about the onDeath, I'll not use in onDeath, because the system is just summon a random boss one time per startup.. I'll test your code, and report here, so much thank you
@Codex NG Thank you too, I'll take a look in your explication to try understand it, thank you again

@Shadowsong this is my code atm... i'm using tfs 1.2, not have errors in console, and anyone monster is summoned, if course in my original script have the correct names and positions
Code:
local config = {
    [1] = {bossName = "boss1", bossPosition = Position(xxxxxx)},
    [2] = {bossName = "boss2", bossPosition = Position(xxxxxx)},
    [3] = {bossName = "boss3", bossPosition = Position(xxxxxx)}
}
Bosses = {}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:getPosition():sendMagicEffect(CONST_ME_YELLOWENERGY)
    local rand = config[math.random(#config)]
    local name = rand.bossName
    if isInArray(Bosses, rand.bossName) then
        return true
    end
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    table.insert(Bosses, rand.bossName)
    if not monster then
        print('>> Failed to spawn '..name..' boss missing map.')
        return true
    end
    print('>> The boss: ' ..monster:getName() .. ' was summoned.')
    return true
end
I'm using an action item just to test more fast it
 
Last edited by a moderator:
instead of being talking about things that are not so relevant, we will try to help me figure out what might be going on that is not working correctly the script
 
@Shadowsong this is my code atm... i'm using tfs 1.2, not have errors in console, and anyone monster is summoned, if course in my original script have the correct names and positions
Code:
local config = {
    [1] = {bossName = "boss1", bossPosition = Position(xxxxxx)},
    [2] = {bossName = "boss2", bossPosition = Position(xxxxxx)},
    [3] = {bossName = "boss3", bossPosition = Position(xxxxxx)}
}
Bosses = {}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:getPosition():sendMagicEffect(CONST_ME_YELLOWENERGY)
    local rand = config[math.random(#config)]
    local name = rand.bossName
    if isInArray(Bosses, rand.bossName) then
        return true
    end
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    table.insert(Bosses, rand.bossName)
    if not monster then
        print('>> Failed to spawn '..name..' boss missing map.')
        return true
    end
    print('>> The boss: ' ..monster:getName() .. ' was summoned.')
    return true
end
I'm using an action item just to test more fast it

No errors in console, but no monster is summoned, if I understood correctly?
When faced with such a situation, it is nice to debug your script by putting more prints (print()) after each crucial function in the script to check if code (up to that line) was executed properly and eventually print out some parameters that were used by the code at that point to determine if the parameters passed to functions were actually proper or something went wrong there.
 
No errors in console, but no monster is summoned, if I understood correctly?
When faced with such a situation, it is nice to debug your script by putting more prints (print()) after each crucial function in the script to check if code (up to that line) was executed properly and eventually print out some parameters that were used by the code at that point to determine if the parameters passed to functions were actually proper or something went wrong there.
I just change the order of the lines:
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local rand = config[math.random(#config)]
    local name = rand.bossName
    if isInArray(Bosses, name) then
        return true
    end
    table.insert(Bosses, name)
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..name..' boss missing map.')
        return true
    end   
    print('>> The boss: ' ..monster:getName() .. ' was summoned')
    return true
end
and it work :O
 
I just change the order of the lines:
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local rand = config[math.random(#config)]
    local name = rand.bossName
    if isInArray(Bosses, name) then
        return true
    end
    table.insert(Bosses, name)
    local monster = Game.createMonster(rand.bossName, rand.bossPosition)
    if not monster then
        print('>> Failed to spawn '..name..' boss missing map.')
        return true
    end  
    print('>> The boss: ' ..monster:getName() .. ' was summoned')
    return true
end
and it work :O

Glad to have helped!
 
!bump I want to do something, I want to put in action item the text of the table, I'm trying with this
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local text = Bosses
    player:showTextDialog(item.itemid, text)
    return true
end
doens't work
 
Sorry if it was already answered, but if you just want 1 boss to spawn on startup it might be easier to make it spawn in globalevents>scripts>startup.lua rather than onThink.
 
Sorry if it was already answered, but if you just want 1 boss to spawn on startup it might be easier to make it spawn in globalevents>scripts>startup.lua rather than onThink.
I just put an exemple, the entire script summon one boss of 65 in every hour ... I just needed to check the name because I don't want summon the same monster two times
 
Oh I misunderstood.

You could do something like this to move the first result to the end so its not used until all bosses are cycled through.
Code:
local tableStuff = {
    {name = "thing 1", pos = {x = 1, y = 2, z = 3}},
    {name = "thing 2", pos = {x = 1, y = 2, z = 3}},
    {name = "thing 3", pos = {x = 1, y = 2, z = 3}}
}

function somethingWhatever()
    local selectedTableThing = tableStuff[1] -- select the first
    print(selectedTableThing.name) -- print for testing
    table.remove(tableStuff, 1) -- remove the first
    table.insert(tableStuff, selectedTableThing) -- add it back to the end
end

If you want to randomize it. I would suggest shuffling the table every restart.
Here is a nice little function to have:
Code:
function shuffleArray(array)
    local n, random, j = table.getn(array), math.random
    for i=1, n do
        j,k = random(n), random(n)
        array[j],array[k] = array[k],array[j]
    end
    return array
end

Example Usage:
Code:
local tableStuff = {
    {name = "thing 1", pos = {x = 1, y = 2, z = 3}},
    {name = "thing 2", pos = {x = 1, y = 2, z = 3}},
    {name = "thing 3", pos = {x = 1, y = 2, z = 3}}
}
shuffleArray(tableStuff)

function somethingWhatever()
    local selectedTableThing = tableStuff[1] -- select the first
    print(selectedTableThing.name) -- print for testing
    table.remove(tableStuff, 1) -- remove the first
    table.insert(tableStuff, selectedTableThing) -- add it back to the end
end
This will shuffle the table 1 time on server restart or on reload.
It will then take the first "boss", you can do things with it, then it will remove it from the front, and add it back again resulting in it being last. It will not appear again until all bosses have been spawned 1 time.
 
!bump I want to do something, I want to put in action item the text of the table, I'm trying with this
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local text = Bosses
    player:showTextDialog(item.itemid, text)
    return true
end
doens't work

Code:
local text = table.concat(Bosses, ', ')
 

Similar threads

Back
Top