Bienvenido a Open Games Community

Únete ahora para acceder a todas nuestras funciones. Una vez que te registres e inicies sesión, podrás crear temas, responder a hilos existentes, valorar la reputación de otros miembros y muchísimo más. Además, es rápido y totalmente gratuito; ¿a qué esperas?

Script I'm looking for an NPC

mrc1333

Miembro
Registrado
23 Mar 2023
Mensajes
10
Puntuación de reacción
2
Puntos
128
Ubicación
Polska
Witam, poszukuję kogoś, kto pomoże mi napisać NPC do Canary 13.40, który będzie miał 10 misji polegających na zabijaniu potworów i bossów oraz dostarczaniu przedmiotów. NPC, których próbuję dodać, nie liczą zabitych potworów.
 

Alex

Administrador
Dev
Registrado
1 Sep 2021
Mensajes
2.354
Puntuación de reacción
28.294
Puntos
707
Witam, poszukuję kogoś, kto pomoże mi napisać NPC do Canary 13.40, który będzie miał 10 misji polegających na zabijaniu potworów i bossów oraz dostarczaniu przedmiotów. NPC, których próbuję dodać, nie liczą zabitych potworów.

Hello an example of Npc, i just copied Benjamin.lua:


Código Lua:
local internalNpcName = "Benjamin"
local npcType = Game.createNpcType(internalNpcName)
local npcConfig = {}

npcConfig.name = internalNpcName
npcConfig.description = internalNpcName

npcConfig.health = 100
npcConfig.maxHealth = npcConfig.health
npcConfig.walkInterval = 2000
npcConfig.walkRadius = 2

npcConfig.outfit = {
    lookType = 128,
    lookHead = 116,
    lookBody = 79,
    lookLegs = 117,
    lookFeet = 76,
    lookAddons = 0
}

npcConfig.flags = {
    floorchange = false
}

npcConfig.voices = {
    interval = 15000,
    chance = 50,
    { text = 'Welcome to the post office!' },
    { text = 'If you need help with letters or parcels, just ask me. I can explain everything.' },
    { text = 'Hey, send a letter to your friend now and then. Keep in touch, you know.' }
}

local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)

npcType.onThink = function(npc, interval)
    npcHandler:onThink(npc, interval)
end

npcType.onAppear = function(npc, creature)
    npcHandler:onAppear(npc, creature)
end

npcType.onDisappear = function(npc, creature)
    npcHandler:onDisappear(npc, creature)
end

npcType.onMove = function(npc, creature, fromPosition, toPosition)
    npcHandler:onMove(npc, creature, fromPosition, toPosition)
end

npcType.onSay = function(npc, creature, type, message)
    npcHandler:onSay(npc, creature, type, message)
end

npcType.onCloseChannel = function(npc, creature)
    npcHandler:onCloseChannel(npc, creature)
end

local function creatureSayCallback(npc, creature, type, message)
    local player = Player(creature)
    local playerId = player:getId()

    if not npcHandler:checkInteraction(npc, creature) then
        return false
    end

    if MsgContains(message, "measurements") then
        local player = Player(creature)
        if player:getStorageValue(Storage.Postman.Mission07) >= 1 and    player:getStorageValue(Storage.Postman.MeasurementsBenjamin) ~= 1 then
            npcHandler:say("Oh they don't change that much since in the old days as... <tells a boring and confusing story about a cake, a parcel, himself and two squirrels, at least he tells you his measurements in the end> ", npc, creature)
            player:setStorageValue(Storage.Postman.Mission07, player:getStorageValue(Storage.Postman.Mission07) + 1)
            player:setStorageValue(Storage.Postman.MeasurementsBenjamin, 1)
            npcHandler:setTopic(playerId, 0)
        else
            npcHandler:say("...", npc, creature)
            npcHandler:setTopic(playerId, 0)
        end
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, "Hello. How may I help you |PLAYERNAME|? Ask me for a {trade} if you want to buy something. I can also explain the {mail} system.")
npcHandler:setMessage(MESSAGE_FAREWELL, "It was a pleasure to help you.")

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

npcHandler:addModule(FocusModule:new(), npcConfig.name, true, true, true)

npcConfig.shop = {
    { itemName = "label", clientId = 3507, buy = 1 },
    { itemName = "letter", clientId = 3505, buy = 8 },
    { itemName = "parcel", clientId = 3503, buy = 15 }
}
-- On buy npc shop message
npcType.onBuyItem = function(npc, player, itemId, subType, amount, ignore, inBackpacks, totalCost)
    npc:sellItem(player, itemId, amount, subType, 0, ignore, inBackpacks)
end
-- On sell npc shop message
npcType.onSellItem = function(npc, player, itemId, subtype, amount, ignore, name, totalCost)
    player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Sold %ix %s for %i gold.", amount, name, totalCost))
end
-- On check npc shop message (look item)
npcType.onCheckItem = function(npc, player, clientId, subType)
end

npcType:register(npcConfig)

Now try modify it like this it will ask you to kill 10 demons:


Código Lua:
local internalNpcName = "Missionary"
local npcType = Game.createNpcType(internalNpcName)
local npcConfig = {}

npcConfig.name = internalNpcName
npcConfig.description = internalNpcName

npcConfig.health = 100
npcConfig.maxHealth = npcConfig.health
npcConfig.walkInterval = 2000
npcConfig.walkRadius = 2

npcConfig.outfit = {
    lookType = 128,
    lookHead = 116,
    lookBody = 79,
    lookLegs = 117,
    lookFeet = 76,
    lookAddons = 0
}

npcConfig.flags = {
    floorchange = false
}

npcConfig.voices = {
    interval = 15000,
    chance = 50,
    { text = 'New Missions!' },
}

local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)

npcType.onThink = function(npc, interval)
    npcHandler:onThink(npc, interval)
end

npcType.onAppear = function(npc, creature)
    npcHandler:onAppear(npc, creature)
end

npcType.onDisappear = function(npc, creature)
    npcHandler:onDisappear(npc, creature)
end

npcType.onMove = function(npc, creature, fromPosition, toPosition)
    npcHandler:onMove(npc, creature, fromPosition, toPosition)
end

npcType.onSay = function(npc, creature, type, message)
    npcHandler:onSay(npc, creature, type, message)
end

npcType.onCloseChannel = function(npc, creature)
    npcHandler:onCloseChannel(npc, creature)
end

local function creatureSayCallback(npc, creature, type, message)
    local player = Player(creature)
    local playerId = player:getId()

    if not npcHandler:checkInteraction(npc, creature) then
        return false
    end

    if MsgContains(message, "mission") then
        local player = Player(creature)
        if player:getStorageValue(250000) >= 10 then
            npcHandler:say("You have killed 10 of this monsters, here is your reward. ", npc, creature)
            player:setStorageValue(250000, 0)
            player:addItem(2160, 10)
            npcHandler:setTopic(playerId, 0)
        else
            npcHandler:say("You should kill 10 Demons.", npc, creature)
            npcHandler:setTopic(playerId, 0)
        end
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, "Hello. How may I help you |PLAYERNAME|? Ask me for a {mission} to kill monsters.")
npcHandler:setMessage(MESSAGE_FAREWELL, "It was a pleasure to help you.")

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

npcHandler:addModule(FocusModule:new(), npcConfig.name, true, true, true)

-- On buy npc shop message
npcType.onBuyItem = function(npc, player, itemId, subType, amount, ignore, inBackpacks, totalCost)
    npc:sellItem(player, itemId, amount, subType, 0, ignore, inBackpacks)
end
-- On sell npc shop message
npcType.onSellItem = function(npc, player, itemId, subtype, amount, ignore, name, totalCost)
    player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Sold %ix %s for %i gold.", amount, name, totalCost))
end
-- On check npc shop message (look item)
npcType.onCheckItem = function(npc, player, clientId, subType)
end

npcType:register(npcConfig)

Now you need to create a file as onKill function, when killing a demon it gives you storage 250000, and adds +1 each time you kill one demon.

Código Lua:
local monsters = {
    ["demon"] = {storage = 250000},
}

local monstersOnKill = CreatureEvent("MonstersOnKill")
function monstersOnKill.onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster or targetMonster:getMaster() then
        return true
    end
    local monsterConfig = monsters[targetMonster:getName():lower()]
    if not monsterConfig then
        return true
    end
    for key, value in pairs(targetMonster:getDamageMap()) do
        local attackerPlayer = Player(key)
        if attackerPlayer then
            if monsterConfig.storage then
                attackerPlayer:setStorageValue(monsterConfig.storage, 1)
            end
        end
    end
    return true
end
monstersOnKill:register()

On the last script you can add more monsters if you want.
Now on the npc you can copy many times the if statement and create like 10 or more missions, succeding it each one until the last one will be a boss.
 

mrc1333

Miembro
Registrado
23 Mar 2023
Mensajes
10
Puntuación de reacción
2
Puntos
128
Ubicación
Polska
dziękuję, umieściłem NPC w folderze NPC i umieściłem go w grze, po napisaniu cześć daje mi zadanie zabijania demonów. Umieściłem plik monsteronkill.lua w folderze ze skryptami podczas zabijania demonów i nadal ich nie liczy, ustawiłem miejsce do przechowywania. W konsoli nie mam żadnych błędów, czy muszę gdzieś resetować tego onkill jako zdarzenie czy wystarczy dodać to do skryptu?
 

mrc1333

Miembro
Registrado
23 Mar 2023
Mensajes
10
Puntuación de reacción
2
Puntos
128
Ubicación
Polska
thank you, I put the npc in the NPC folder and put it in the game, after writing hi it gives me the task to kill demons. I put monsteronkill.lua in the script folder while killing demons and it still doesn't count them, I set storage. I don't have any errors in the console, do I need to reset this onkill somewhere as an event or is it enough to add it to the script?
 

Alex

Administrador
Dev
Registrado
1 Sep 2021
Mensajes
2.354
Puntuación de reacción
28.294
Puntos
707
thank you, I put the npc in the NPC folder and put it in the game, after writing hi it gives me the task to kill demons. I put monsteronkill.lua in the script folder while killing demons and it still doesn't count them, I set storage. I don't have any errors in the console, do I need to reset this onkill somewhere as an event or is it enough to add it to the script?
Probably, not sure but;

Open demon.lua file, then search for monster.events { "MonstersOnKill" } <-- Add the event on the monster file in order to count it when you kill it.
 
shape1
shape2
shape3
shape4
shape5
shape6
Arriba