This is a read only copy without any forum functionality of the old Modcraft forum.
If there is anything that you would like to have removed, message me on Discord via Kaev#5208.
Big thanks to Alastor for making this copy!

Menu

Author Topic: [Question] Items that spawn temporary objects.  (Read 3036 times)

jar0fair

  • Registred Member
  • Race Changer
  • *****
  • Posts: 30
    • View Profile
Re: [Question] Items that spawn temporary objects.
« Reply #15 on: August 01, 2015, 12:34:40 am »
Quote from: "Rochet2"
Try this. I basically just added back in the GetWorldObject you removed.

Code: [Select]
-- Cauldron
local CauldronItem = 200001
local CauldronObj = 186472
-- Chair
local ChairItem = 200002
local ChairObj = 186732
-- bed
local BedItem = 200003
local BedObj = 13948
-- vendor tent
local VendortentItem = 200004
local VendortentObj = 4000668

local spawn = {}

local function SpawnObj(plr, entry, despawn_time)
    local guid = plr:GetGUID()
    if spawn[guid] then
        local spawnedGobject = plr:GetMap():GetWorldObject(spawn[guid])
        if spawnedGobject then
            spawnedGobject:RemoveFromWorld(true)
            spawn[guid] = nil
        else
            plr:SendNotification("You need to be closer to the existing object to despawn it")
        end
    else
        local x, y, z, o = plr:GetLocation()
        local gobject = plr:SummonGameObject(entry, x, y, z, o, despawn_time)
        if gobject then
            spawn[guid] = gobject:GetGUID()
        end
    end
end

local function Cauldron(event, plr, item, target)
    SpawnObj(plr, CauldronObj, 0)
end

local function Chair(event, plr, item, target)
    SpawnObj(plr, ChairObj, 0)
end

local function Bed(event, plr, item, target)
    SpawnObj(plr, BedObj, 5)
end

local function VendorTent(event, plr, item, target)
    SpawnObj(plr, VendortentObj, 0)
end

RegisterItemEvent(CauldronItem, 2, Cauldron)
RegisterItemEvent(ChairItem, 2, Chair)
RegisterItemEvent(BedItem, 2, Bed)
RegisterItemEvent(VendortentItem, 2, VendorTent)


Nope, It doesn't remove the objects, and I am not sure why, because, it really seems like would, reading the code.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Rochet2

  • Contributors
  • Polygonshifter
  • *****
  • Posts: 59
    • View Profile
    • http://rochet2.github.io/
Re: [Question] Items that spawn temporary objects.
« Reply #16 on: August 01, 2015, 12:39:42 am »
Quote from: "jar0fair"
Nope, It doesn't remove the objects, and I am not sure why, because, it really seems like would, reading the code.
Are you getting any errors?
The script does nothing after spawning one gameobject? No more spawns or any messages?
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Rochet2

  • Contributors
  • Polygonshifter
  • *****
  • Posts: 59
    • View Profile
    • http://rochet2.github.io/
Re: [Question] Items that spawn temporary objects.
« Reply #17 on: August 01, 2015, 12:51:25 am »
Alright, I see the error now.
Change
local guid = plr:GetGUID()
to
local guid = plr:GetGUIDLow()

The difference here is that GetGUID returns an uint64 number which is represented as an object.
Each GetGUID returns a different object causing the lua table to map them to different locations.
GetGUIDLow returns a normal number.
To properly make the uint64 work as table key it might need to be converted to strings, but using lowguid is easier for this and might be faster.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

jar0fair

  • Registred Member
  • Race Changer
  • *****
  • Posts: 30
    • View Profile
Re: [Question] Items that spawn temporary objects.
« Reply #18 on: August 01, 2015, 02:57:08 pm »
Quote from: "Rochet2"
Alright, I see the error now.
Change
local guid = plr:GetGUID()
to
local guid = plr:GetGUIDLow()

The difference here is that GetGUID returns an uint64 number which is represented as an object.
Each GetGUID returns a different object causing the lua table to map them to different locations.
GetGUIDLow returns a normal number.
To properly make the uint64 work as table key it might need to be converted to strings, but using lowguid is easier for this and might be faster.


Thank you so much! Both of you. It is finally working how I would like it to. Me and my entire team are very grateful.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

jar0fair

  • Registred Member
  • Race Changer
  • *****
  • Posts: 30
    • View Profile
Re: [Question] Items that spawn temporary objects.
« Reply #19 on: August 08, 2015, 03:06:29 pm »
Hia, back again.

After a lot of testing I discovered that if I log out, with one of my objects spawned, and it disappears, I can no long use the item anymore without reloading the lua engine. This is because the object is gone, and the code does not recognize it. So, I have written an additional function to nil the spawns on logout. However, it does not work. I am wondering if anyone sees any glaring mistakes in the logout function that I am somehow missing.

Code: [Select]
-- Name Here
local item = 200002
local obj = 22713

-- Code
local spawn = {}

local function SpawnObj(plr, entry, despawn_time)
    local guid = plr:GetGUIDLow()
    if spawn[guid] then
        local spawnedGobject = plr:GetMap():GetWorldObject(spawn[guid])
        if spawnedGobject then
            spawnedGobject:RemoveFromWorld(true)
            spawn[guid] = nil
        end
    else
        local x, y, z, o = plr:GetLocation()
        local gobject = plr:SummonGameObject(entry, x, y, z, o, despawn_time)
        if gobject then
            spawn[guid] = gobject:GetGUID()
        end
    end
end

local function logoff(plr, entry)
    guid = plr.GetGUIDLow()
    spawn[guid] = nil
end
-- Spawn Function
local function use(event, plr, item, target)
    SpawnObj(plr, obj, 0)
end
-- Registers
RegisterItemEvent(item, 2, use)
RegisterPlayerEvent(4, logoff)
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Rochet2

  • Contributors
  • Polygonshifter
  • *****
  • Posts: 59
    • View Profile
    • http://rochet2.github.io/
Re: [Question] Items that spawn temporary objects.
« Reply #20 on: August 08, 2015, 04:34:55 pm »
Quote
Code: [Select]
local function logoff(plr, entry)
    guid = plr.GetGUIDLow()
    spawn[guid] = nil
end
Erm .. thats ..
The reason the code doesnt work is because your parameters are wrong logoff(plr, entry).
If you look here: http://eluna.emudevs.com/Global/Registe ... Event.html it says (event, player)

also make that guid variable local.
Also you call member functions with : not with a dot.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

jar0fair

  • Registred Member
  • Race Changer
  • *****
  • Posts: 30
    • View Profile
Re: [Question] Items that spawn temporary objects.
« Reply #21 on: August 09, 2015, 03:28:40 am »
Thanks again, Rotchet, this works just how I needed it to, now. Couldn't have done it without you.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »