Jump to content

[SOLVED] Problem with addon_basecreator


rapgod1

Recommended Posts

Hello everyone. I have a problem with this resource: click

I place object, destroy it and after restart resource this object again created (why?).

function newObject(model,x,y,z,rx,ry,rz,health,id) 
    if model and x and y and z and rx and ry and rz then 
        local ob = createObject(model, x, y, z, rx, ry, rz) 
        local acName = getAccountName(getPlayerAccount(client)) 
        setElementData(ob,"bc.creator",acName) 
        setElementData(ob,"object.health",health) 
        setElementData(ob,"bc.ID",id) 
        triggerClientEvent("setTheObjectUnbreakable",root,ob) 
        local encampment = getElementData(client,"Gang") 
        if model == 3093 or model == 3029 then 
            setupDoor(ob,encampment) 
        end 
        if ob then 
            local x,y,z = getElementPosition(ob) 
            local rx,ry,rz = getElementRotation(ob) 
            local model = getElementModel(ob) 
            dbExec(db, "INSERT INTO baseobjects VALUES (?,?,?,?,?,?,?,?,?,?,?)", count, model, acName, x, y, z, rx, ry, rz, health, tostring(encampment)) 
            count = count+1          
        end 
    end 
end 
addEvent("addon.basecreator:newObject", true) 
addEventHandler("addon.basecreator:newObject", root, newObject) 

function onObjectDamage(object,health,id) 
    if object then 
        dbExec(db,'UPDATE baseobjects SET health=? WHERE id=?',health,id) 
    end 
end 
addEvent("onObjectDamage",true) 
addEventHandler("onObjectDamage",root,onObjectDamage) 
  
function onObjectDestroy(object,id) 
    if object then 
        if getElementData(object,"parent") then 
            destroyElement(getElementData(object,"parent")) 
        end 
        destroyElement(object) 
        dbExec(db,"DELETE FROM baseobjects WHERE id=?",id) 
        count = count-1 
    end 
end 
addEvent("onObjectDestroy",true) 
addEventHandler("onObjectDestroy",root,onObjectDestroy) 

local db 
local count = 0 
local DoorsTable = {} 
function onStart() 
    if not fileExists("bases.db") then 
        local h = fileCreate("bases.db") 
        if h then 
        fileClose(h) 
        outputDebugString("[DayZ] Bases database not found, creating...") 
        end 
        db = dbConnect( "sqlite", "bases.db" ) 
        outputDebugString("[DayZ] Inserting tables into database...") 
        dbExec(db, "CREATE TABLE IF NOT EXISTS baseobjects(id INT AUTO_INCREMENT, model INT, owner VARCHAR, x FLOAT, y FLOAT, z FLOAT, rx FLOAT, ry FLOAT, rz FLOAT, health FLOAT, encampment VARCHAR)") 
        outputDebugString("[DayZ] Tables inserted.") 
    else 
        db = dbConnect( "sqlite", "bases.db" ) 
        local qh = dbQuery( db, "SELECT * FROM baseobjects" ) 
        local result = dbPoll( qh, 10000 ) 
            for i, ob in ipairs(result) do 
                local tOb = createObject(ob['model'], ob['x'], ob['y'], ob['z'], ob['rx'], ob['ry'], ob['rz']) 
                setTimer(function() 
                    if ob['health'] > 0 then 
                        triggerClientEvent("setTheObjectUnbreakable",root,tOb) 
                    end 
                end,1000,1,tOb) 
                setElementData(tOb, "bc.creator", ob['owner']) 
                setElementData(tOb, "bc.ID", ob['id']) 
                if ob['model'] == 3093 or ob['model'] == 3029 then 
                    setupDoor(tOb,ob['encampment']) 
                end 
                setElementData(tOb,"object.health",ob['health']) 
                if getElementData(tOb,"object.health") <= 0 then 
                    if getElementData(tOb,"parent") then 
                        destroyElement(getElementData(tOb,"parent")) 
                    end 
                    destroyElement(tOb) 
                end 
                count = count + 1 
            end 
            outputDebugString("[DayZ] Base objects loaded. TOTAL: "..tostring(count)) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, onStart) 

Edited by Guest
Link to comment

Hi, I was in MTADayZ team as translator and (besides translating) I previously remaked this resource and it works fine. I also added some new features to it, but unfortunately for you, I'll not share that script, because I'm not original author of the resource.

I will try to help you fix your problem anyway.

in client.lua

on line 321 change this

if hitElement and getElementType(hitElement) == "object" then 

to

if hitElement and getElementType(hitElement) == "object" and getElementData(hitElement,"object.health") then 

in server.lua

add line between lines 79 and 80 including (this shouldn't be necessary, but try it too, if other things will not help you)

if(health <= 0)then setTimer(onObjectDestroy, 500, 1, object, id) end 

for extra check of already bugged objects also change line 26 from

local qh = dbQuery( db, "SELECT * FROM base_objects" ) 

to

local qh = dbQuery( db, "SELECT * FROM base_objects WHERE health > 0" ) 

or edit your SQL file and remove objects with health 0

I'm not sure about this, I didn't test it, so try this and tell me, if it helped. :)

Edited by Guest
Link to comment

I fix it. But I have any problem with ID's in database.

For example:

I place 3 objects

2e6db247b77d4fe49b9ee6a4cb7483b3.png

And when I remove/destory object with ID 2, and again place 2 object. In database will be this:

75823496373248bd80e8e076ac661664.png

How to make, that there was no repetition in ID's?

Link to comment
Set ID to 'primary' and 'auto increment' in your table, also remove manual ID adding in the INSERT function.

Btw.. I added you on skype, accept it

This is basically what I meant, thanks for detailing it :P

I already set 'auto increment'. Now I try to set 'primary'.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...