undead mage
Active Member
Hello! I was wondering if it's possible to spawn a certain npc for example every first day of the month at startup?
local globalevent_spawnNPC_onDay = GlobalEvent("globalevent_spawnNPC_onDay")
-- NPC list and which day(s) of month they should spawn
spawnNpcList = {
["Banker"] = {
["pos"] = {x=1024, y=1024, z=7},
["dayList"] = {1}
},
["Sam"] = {
["pos"] = {x=1024, y=1024, z=7},
["dayList"] = {14, 28}
}
}
-- on startup
function globalevent_spawnNPC_onDay.onStartup()
local today = tonumber(os.date("%d", os.time()))
local spawnedNPC = 0
local shouldHaveSpawned = 0
for npcName, npcInfo in pairs(spawnNpcList) do
for _, day in ipairs(npcInfo["dayList"]) do
-- If today is in the configured dayList of this NPC
if today == day then...
local globalevent_spawnNPC_firstDay = GlobalEvent("globalevent_spawnNPC_firstDay")
function globalevent_spawnNPC_firstDay.onStartup()
local day = tonumber(os.date("%d", os.time()))
-- This is not the first day of any month
if day > 1 then
return false
end
-- Where do we want to place this NPC?
local pos = Position(1024, 1024, 7)
local npc = Game.createNpc("Banker", pos)
if npc then
npc:setMasterPos(pos)
pos:sendMagicEffect(CONST_ME_MAGIC_RED)
return true
end
print("Error: Failed to spawn NPC [".. npc:getName() .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].");
return false
end
globalevent_spawnNPC_firstDay:register()
Thank you! I had one more question would it be possible to make it that multiple npcs/positions/days could be given? For example now it's banker on every first day of the month. But would it be possible to make it that I can add another npc, Sam for example to spawn every 15th of the month?RevScriptSys, place file in data/scripts/ folder, and restart server.
Lua:local globalevent_spawnNPC_firstDay = GlobalEvent("globalevent_spawnNPC_firstDay") function globalevent_spawnNPC_firstDay.onStartup() local day = tonumber(os.date("%d", os.time())) -- This is not the first day of any month if day > 1 then return false end -- Where do we want to place this NPC? local pos = Position(1024, 1024, 7) local npc = Game.createNpc("Banker", pos) if npc then npc:setMasterPos(pos) pos:sendMagicEffect(CONST_ME_MAGIC_RED) return true end print("Error: Failed to spawn NPC [".. npc:getName() .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."]."); return false end globalevent_spawnNPC_firstDay:register()
local globalevent_spawnNPC_onDay = GlobalEvent("globalevent_spawnNPC_onDay")
-- NPC list and which day(s) of month they should spawn
spawnNpcList = {
["Banker"] = {
["pos"] = {x=1024, y=1024, z=7},
["dayList"] = {1}
},
["Sam"] = {
["pos"] = {x=1024, y=1024, z=7},
["dayList"] = {14, 28}
}
}
-- on startup
function globalevent_spawnNPC_onDay.onStartup()
local today = tonumber(os.date("%d", os.time()))
local spawnedNPC = 0
local shouldHaveSpawned = 0
for npcName, npcInfo in pairs(spawnNpcList) do
for _, day in ipairs(npcInfo["dayList"]) do
-- If today is in the configured dayList of this NPC
if today == day then
shouldHaveSpawned = shouldHaveSpawned + 1
-- Where do we want to place this NPC?
local pos = Position(npcInfo.pos.x, npcInfo.pos.y, npcInfo.pos.z)
local npc = Game.createNpc(npcName, pos)
if npc then
npc:setMasterPos(pos)
pos:sendMagicEffect(CONST_ME_MAGIC_RED)
spawnedNPC = spawnedNPC + 1
else
print("Error: Failed to spawn NPC [".. npcName .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].")
end
end
end
end
print("Spawned [".. spawnedNPC .."] out of [".. shouldHaveSpawned .."] day configured NPC's on startup.")
return true
end
globalevent_spawnNPC_onDay:register()
Hey thank you! But it doesn't seem to be working. I have configured the position (triple checked it) and at dayList I have used 27 (today). But it doesn't spawn and the console also says 0 out of 0 day npcs spawnedSure:
Lua:local globalevent_spawnNPC_onDay = GlobalEvent("globalevent_spawnNPC_onDay") -- NPC list and which day(s) of month they should spawn spawnNpcList = { ["Banker"] = { ["pos"] = {x=1024, y=1024, z=7}, ["dayList"] = {1} }, ["Sam"] = { ["pos"] = {x=1024, y=1024, z=7}, ["dayList"] = {14, 28} } } -- on startup function globalevent_spawnNPC_onDay.onStartup() local today = tonumber(os.date("%d", os.time())) local spawnedNPC = 0 local shouldHaveSpawned = 0 for npcName, npcInfo in ipairs(spawnNpcList) do for _, day in ipairs(npcInfo["dayList"]) do -- If today is in the configured dayList of this NPC if today == day then shouldHaveSpawned = shouldHaveSpawned + 1 -- Where do we want to place this NPC? local pos = Position(npcInfo.pos.x, npcInfo.pos.y, npcInfo.pos.z) local npc = Game.createNpc(npcName, pos) if npc then npc:setMasterPos(pos) pos:sendMagicEffect(CONST_ME_MAGIC_RED) spawnedNPC = spawnedNPC + 1 else print("Error: Failed to spawn NPC [".. npcName .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].") end end end end print("Spawned [".. spawnedNPC .."] out of [".. shouldHaveSpawned .."] day configured NPC's on startup.") return true end globalevent_spawnNPC_onDay:register()
Hey thank you! But it doesn't seem to be working. I have configured the position (triple checked it) and at dayList I have used 27 (today). But it doesn't spawn and the console also says 0 out of 0 day npcs spawned
-- on startup
function globalevent_spawnNPC_onDay.onStartup()
print("Test1")
local today = tonumber(os.date("%d", os.time()))
local spawnedNPC = 0
local shouldHaveSpawned = 0
for npcName, npcInfo in ipairs(spawnNpcList) do
print("Test2")
for _, day in ipairs(npcInfo["dayList"]) do
print("Test3")
-- If today is in the configured dayList of this NPC
if today == day then
print("Test4")
shouldHaveSpawned = shouldHaveSpawned + 1
-- Where do we want to place this NPC?
local pos = Position(npcInfo.pos.x, npcInfo.pos.y, npcInfo.pos.z)
local npc = Game.createNpc(npcName, pos)
if npc then
print("Test5")
npc:setMasterPos(pos)
pos:sendMagicEffect(CONST_ME_MAGIC_RED)
spawnedNPC = spawnedNPC + 1
else
print("Test6")
print("Error: Failed to spawn NPC [".. npcName .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].")
end
end
end
end
print("Test7")
print("Spawned [".. spawnedNPC .."] out of [".. shouldHaveSpawned .."] day configured NPC's on startup.")
return true
end
Do I also need to changeChangeipairs(spawnNpcList)
topairs(spawnNpcList)
for _, day in ipairs(npcInfo["dayList"]) do
for _, day in pairs(npcInfo["dayList"]) do
Do I also need to change
for _, day in ipairs(npcInfo["dayList"]) do
for
for _, day in pairs(npcInfo["dayList"]) do
?
That fixed it, ty!Changeipairs(spawnNpcList)
topairs(spawnNpcList)
That's clear! Ty!Nope, here is difference explained between pairs() and ipairs():
.What is the difference of pairs() vs. ipairs() in Lua?
In a for loop, what is the difference between looping with pairs() and ipairs()? This page uses both: Lua Docs With ipairs(): a = {"one", "two", "three"} for i, v in ipairs(a) do print(i, v) endstackoverflow.com