Hallo,
ich wollte folgendes Petsystem einbauen:
Bitte melden Sie sich an, um diesen Link zu sehen.
Klappt prinzipiell auch, aber wenn ich einen Namen eingebe und danach das Pet rufen möchte, kann ich wieder einen neuen Namen eingeben und das in einem Loop.
Wenn ich das benennen deaktiviere klappt das alles soweit, aber das würde ich auch gerne noch zum laufen bekommen.
Er meckert auch nicht, wie in der Quest geschrieben, wenn man Sonderzeichen eingibt. Wenn der Name zulang ist meckert er.
Die Pet Quest:
LUA: petQuest.lua
- quest petsystem begin
- state start begin
- when login begin
- callPetsystemCacheSave()
- end
- when 53001.use begin
- local sealVnum = item.get_vnum()
- local pid = pc.get_player_id()
- local srcPet = getPlayerPetByPIDAndSealVnum(pid, sealVnum)
- if not srcPet then
- local name = nil
- local spawnFromFar = nil
- if PET_SETTINGS.ALLOW_NAMECHANGE then
- local availableCharacters = 24 - string.len(pc.get_name()) - string.len(" - ")
- say_title(item_name(sealVnum) .. ":")
- say("")
- say("Da du dieses Pet zum ersten Mal rufst,")
- say("musst du dir zunächst einen Namen für es aussuchen.")
- say("")
- say_reward("Bitte gib den Namen des Pets ein:")
- say("- max. " .. availableCharacters .. " Zeichen")
- say("- nur Buchstaben und Zahlen")
- say("")
- name = input()
- if string.len(name) > availableCharacters then
- say_title(item_name(sealVnum) .. ":")
- say("")
- say("Der eingegebene Name ist zu lang.")
- say("")
- return
- elseif type(string.match(name, "^[%-%.%w_]+$")) ~= datatypes.STRING then
- say_title(item_name(sealVnum) .. ":")
- say("")
- say("Der eingegebene Name enthält nicht erlaubte Zeichen.")
- say("")
- return
- end
- end
- if PET_SETTINGS.ALLOW_SPAWNTYPECHANGE then
- say_title(item_name(sealVnum) .. ":")
- say("")
- say("Da du dieses Pet zum ersten Mal rufst,")
- say("musst du auswählen, ob das Pet beim spawnen")
- say("zu dir laufen soll oder direkt neben")
- say("dir erscheinen soll.")
- say("")
- say_reward("Das Pet soll ...")
- spawnFromFar = select("... zu mir laufen", "... neben mir spawnen") == 1
- end
- srcPet = createNewPlayerPet(pid, sealVnum, name, spawnFromFar)
- if not srcPet then
- syschat("Something went wrong ...")
- return
- end
- end
- local mobVnum = srcPet:getPetdata():getMobVnum()
- local affectList = srcPet:getPetdata():getAffectList()
- local spawnEffectPath = srcPet:getPetdata():getSpawneffectFilepath()
- local petName = srcPet:getName()
- local spawnFromFar = srcPet:isSpawnFromFar()
- if pet.is_summon(mobVnum) then
- pet.unsummon(mobVnum)
- syschat("Dein Haustier wurde zurückgeschickt.")
- return
- end
- if pet.count_summoned() >= PET_SETTINGS.MAX_SPAWN_COUNT then
- syschat("Du hast bereits die maximale Anzahl von " .. PET_SETTINGS.MAX_SPAWN_COUNT .. " Haustieren gerufen.")
- return
- end
- pet.summon(mobVnum, " - " .. petName, spawnFromFar)
- if type(spawnEffectPath) == datatypes.STRING then
- pet.spawn_effect(spawnEffectPath)
- end
- end
- end
- end
petfunctions.lua:
LUA: petFunctions.lua
- --settings
- PET_SETTINGS = {}
- PET_SETTINGS.MAX_SPAWN_COUNT = 1
- PET_SETTINGS.ALLOW_NAMECHANGE = true
- PET_SETTINGS.ALLOW_SPAWNTYPECHANGE = false
- PET_SETTINGS.CACHE_SECONDS = 300
- local pets = {
- [53001] = {
- mobVnum = 34001,
- spawneffectFilepath = nil,
- defaultName = "Feuerphönix",
- defaultSpawnFromFar = false,
- affects = nil
- }
- }
- local PETDATA_BY_SEALVNUM = {}
- local PLAYERPETS_BY_PID = {}
- local PET_SYSTEM_LAST_SAVE = get_time()
- function isInstanceOf(var, class)
- return getmetatable(var) == class
- end
- datatypes = {
- STRING = type(""),
- NUMBER = type(0),
- TABLE = type({}),
- FUNCTION = type(function() end),
- BOOLEAN = type(true),
- NIL = type(nil)
- }
- setmetatable(datatypes, {__index = function(t,i) return datatypes.NIL end})
- local PetData = {
- --metatable
- }
- function PetData:new(sealVnum, mobVnum, defaultName, defaultSpawnFromFar, spawneffectFilepath)
- local Affect = {
- --metatable
- }
- function Affect:new(affectID, amount)
- if type(affectID) ~= datatypes.NUMBER or type(amount) ~= datatypes.NUMBER then
- return false
- end
- local object = {}
- setmetatable(object, self)
- --on undefined key request
- self.__index = function(tbl, key)
- return nil
- end
- --tostring call
- self.__tostring = function()
- return object:tostring()
- end
- --private variables
- local affectID = affectID
- local amount = amount
- function object:getAffectID()
- return affectID
- end
- function object:getAmount()
- return amount
- end
- function object:tostring()
- return tostring(self:getAffectID()) .. ": " .. tostring(self:getAmount())
- end
- return object
- end
- if type(sealVnum) ~= datatypes.NUMBER or type(mobVnum) ~= datatypes.NUMBER or type(defaultName) ~= datatypes.STRING or type(defaultSpawnFromFar) ~= datatypes.BOOLEAN or (type(setSpawneffectFilepath) ~= datatypes.STRING and type(setSpawneffectFilepath) ~= datatypes.NIL) then
- return false
- end
- local object = {}
- setmetatable(object, self)
- --on undefined key request
- self.__index = function(tbl, key)
- return nil
- end
- --tostring call
- self.__tostring = function()
- return object:tostring()
- end
- --private variables
- local sealVnum = sealVnum
- local mobVnum = mobVnum
- local defaultName = defaultName
- local defaultSpawnFromFar = defaultSpawnFromFar
- local spawneffectFilepath = spawneffectFilepath or nil
- local affectList = {}
- function object:getSealVnum()
- return sealVnum
- end
- function object:getMobVnum()
- return mobVnum
- end
- function object:getDefaultName()
- return defaultName
- end
- function object:getDefaultSpawnFromFar()
- return defaultSpawnFromFar
- end
- function object:getSpawneffectFilepath()
- return spawneffectFilepath
- end
- function object:getAffectList()
- return affectList
- end
- function object:addAffect(affectID, amount)
- local affect = Affect:new(affectID, amount)
- if not affect then
- return false
- end
- table.insert(affectList, affect)
- return true
- end
- function object:tostring()
- return tostring(self:getSealVnum()) .. ": " .. tostring(self:getMobVnum())
- end
- return object
- end
- local Pet = {
- --metatable
- }
- function Pet:new(petData, name, spawnFromFar)
- if not isInstanceOf(petData, PetData) or (type(name) ~= datatypes.STRING and type(name) ~= datatypes.NIL) or (type(spawnFromFar) ~= datatypes.BOOLEAN and type(spawnFromFar) ~= datatypes.NIL) then
- return false
- end
- local object = {}
- setmetatable(object, self)
- --on undefined key request
- self.__index = function(tbl, key)
- return nil
- end
- --tostring call
- self.__tostring = function()
- return object:tostring()
- end
- --private variables
- local petData = petData
- local name = type(name) ~= datatypes.STRING and petData:getDefaultName() or name
- local spawnFromFar = type(spawnFromFar) ~= datatypes.BOOLEAN and petData:getDefaultSpawnFromFar() or spawnFromFar
- local changed = false
- function object:getPetdata()
- return petData
- end
- function object:getName()
- return name
- end
- function object:setName(newName)
- if type(newName) ~= datatypes.STRING then
- return
- end
- name = newName
- self:setChanged(true)
- end
- function object:isSpawnFromFar()
- return spawnFromFar
- end
- function object:setSpawnFromFar(var)
- if type(var) ~= datatypes.BOOLEAN then
- return
- end
- spawnFromFar = var
- self:setChanged(true)
- end
- function object:wasChanged()
- return changed
- end
- function object:setChanged(var)
- if type(var) ~= datatypes.BOOLEAN then
- return
- end
- changed = var
- end
- function object:tostring()
- return self:getName()
- end
- return object
- end
- local function fillPetdataFromTable(sourceTable, destTable)
- for k,v in pairs(sourceTable) do
- local petData = PetData:new(k, v.mobVnum, v.defaultName, v.defaultSpawnFromFar, v.spawneffectFilepath)
- if petData then
- if type(v.affects) ~= datatypes.NIL then
- for _,aff in ipairs(v.affects) do
- petData:addAffect(aff.id, aff.amount)
- end
- end
- destTable[petData:getSealVnum()] = petData
- end
- end
- end
- local function fillPlayerPetsFromMySQL()
- local savedPets = mysql_query("CALL player.pet_AllPets();") or false
- if not savedPets or table.getn(savedPets) < 1 then return end
- for _,v in ipairs(savedPets) do
- local pid = tonumber(v[1])
- local sealVnum = tonumber(v[2])
- local petName = v[3]
- local spawnFromFar = tonumber(v[4]) == 1
- createPlayerPet(PLAYERPETS_BY_PID, pid, sealVnum, petName, spawnFromFar)
- end
- end
- local function updateDBFromTable(sourceTable)
- for pid,pets in pairs(sourceTable) do
- for petData,pet in pairs(pets) do
- if pet:wasChanged() then
- local sealVnum = petData:getSealVnum()
- local name = pet:getName()
- local spawnFromFar = pet:isSpawnFromFar() and 1 or 0
- mysql_query("CALL player.pet_InsertUpdate(".. pid .. ", " .. sealVnum .. ", '" .. name .. "', " .. spawnFromFar .. ");")
- pet:setChanged(false)
- end
- end
- end
- end
- function getPETDATA_BY_SEALVNUM(sealVnum)
- if type(sealVnum) ~= datatypes.NUMBER then
- return false
- end
- return PETDATA_BY_SEALVNUM[sealVnum]
- end
- function getPlayerPetByPIDAndSealVnum(pid, sealVnum)
- if type(pid) ~= datatypes.NUMBER or type(sealVnum) ~= datatypes.NUMBER then
- return false
- end
- if type(PLAYERPETS_BY_PID[pid]) == datatypes.NIL then
- return nil
- end
- return PLAYERPETS_BY_PID[pid][getPETDATA_BY_SEALVNUM(sealVnum)]
- end
- function createPlayerPet(destTable, pid, sealVnum, petName, spawnFromFar)
- if type(pid) ~= datatypes.NUMBER then
- return false
- end
- local petData = getPETDATA_BY_SEALVNUM(sealVnum)
- local pet = Pet:new(petData, petName, spawnFromFar)--checks if values are valid, returns false if not
- if not pet then
- return false
- end
- if type(destTable[pid]) == datatypes.NIL then
- destTable[pid] = {}
- end
- destTable[pid][petData] = pet
- return getPlayerPetByPIDAndSealVnum(pid, sealVnum)
- end
- function createNewPlayerPet(pid, sealVnum, petName, spawnFromFar)
- local newPlayerPet = createPlayerPet(PLAYERPETS_BY_PID, pid, sealVnum, petName, spawnFromFar)
- if not newPlayerPet then
- return false
- end
- newPlayerPet:setChanged(true)
- return newPlayerPet
- end
- function callPetsystemCacheSave()
- if get_time() - PET_SYSTEM_LAST_SAVE >= PET_SETTINGS.CACHE_SECONDS then
- updateDBFromTable(PLAYERPETS_BY_PID)
- PET_SYSTEM_LAST_SAVE = get_time()
- end
- end
- --on startup
- fillPetdataFromTable(pets, PETDATA_BY_SEALVNUM)
- fillPlayerPetsFromMySQL()