Zcraks Posted March 8, 2019 Share Posted March 8, 2019 I have problem. This script destroys only the first object. How to delete all objects? Quote wind = createObject(3892,-2072.5,285.5,34.3, 0,0,0) if timehour <= 19 and timehour >= 6 then if wind and isElement(wind) then destroyElement(wind) wind = nil outputChatBox ("Object destroyed") end elseif not wind or not isElement(wind) then wind = createObject(3892,-2072.5,285.5,34.3, 0,0,0) wind = createObject(3895,-2035.6,263.39999,34.3, 0,0,0) wind = createObject(3896,-2096.5,146.5,34.3, 0,0,0) wind = createObject(3897,-2056.1001,214.8,34.3, 0,0,0) outputChatBox ("Object created") Link to comment
Moderators IIYAMA Posted March 8, 2019 Moderators Share Posted March 8, 2019 do local windContainer = createElement("windContainer") -- parent function createWind (...) local wind = createObject(...) if wind then setElementParent(wind, windContainer) -- make wind a child of windContainer return wind end return false end function destroyWind () if doesWindExist () then destroyElement(windContainer) windContainer = createElement("windContainer") return true end return false end function doesWindExist () return getElementChildrenCount ( windContainer ) > 0 end end This can be done by using propagation on the element tree. This means that if you use an element based function(Example: destroyElement) on a specific PARENT(Example: windContainer) element, it's CHILDREN(Example: wind, wind, wind) elements will also apply this function. Might be hard to understand, but it is easier than doing it with tables. if timehour <= 19 and timehour >= 6 then destroyWind() elseif not doesWindExist () then wind = createWind(3892,-2072.5,285.5,34.3, 0,0,0) wind = createWind(3895,-2035.6,263.39999,34.3, 0,0,0) wind = createWind(3896,-2096.5,146.5,34.3, 0,0,0) wind = createWind(3897,-2056.1001,214.8,34.3, 0,0,0) outputChatBox ("Object created") The element tree of MTA looks a bit like HTML if you have ever worked with that. https://wiki.multitheftauto.com/wiki/Element_tree Link to comment
Zcraks Posted March 9, 2019 Author Share Posted March 9, 2019 11 hours ago, IIYAMA said: Это можно сделать с помощью распространения в дереве элементов. Это означает, что если вы используете элементную функцию (Пример: destroyElement ) для определенного элемента PARENT (Пример: windContainer ) , то ее ДЕТСКИЕ (Пример: ветер, ветер, ветер ) элементы также будут применять эту функцию. Это может быть трудно понять, но это проще, чем делать это с таблицами. Дерево элементов MTA выглядит немного как HTML, если вы когда-либо работали с этим. https://wiki.multitheftauto.com/wiki/Element_tree function chekTime () local timehour, timeminute = getTime(soucre) local windContainer = createElement("windContainer") if not wind or not isElement(windContainer) then wind = createObject(3892,-2072.5,285.5,34.3, 0,0,0) wind = createObject(3892,-2072.5,285.5,34.3, 0,0,0) wind = createObject(3895,-2035.6,263.39999,34.3, 0,0,0) wind = createObject(3896,-2096.5,146.5,34.3, 0,0,0) wind = createObject(3897,-2056.1001,214.8,34.3, 0,0,0) outputChatBox ("Object created") if wind then setElementParent(wind, windContainer) return wind end return false end elseif timehour <= 19 and timehour >= 6 then if doesWindExist () then destroyElement(windContainer) outputChatBox ("Object destroyed") windContainer = createElement("windContainer") return true return false return getElementChildrenCount ( windContainer ) > 0 end end addEventHandler( "onClientRender", getRootElement( ),chekTime) It is difficult to understand. What are my mistakes? Link to comment
Ab-47 Posted March 9, 2019 Share Posted March 9, 2019 (edited) I think you wanted something a bit more basic. What you've done is overlapped the same element transfering the variable to a new object where the previous object is left undefined. In order to have multiple objects, you must have multiple variables or use an iterating table. In the most basic cases you can use variables: wind1 = createObject() wind2 = createObject() wind3 = createObject() and so on... For tables, you have a few options but the best scenario would be to add all the data into a table and unpack or use a loop to retrieve the data. Just an example of sorts, haven't tested this and am very sleepy cause it's 8am and haven't slept but have a look below you should get the idea of what you could do, especially by naming the objects according to the iterating index: local windTable = { {3892,-2072.5,285.5,34.3, 0,0,0}, {3895,-2035.6,263.39999,34.3, 0,0,0}, {3896,-2096.5,146.5,34.3, 0,0,0}, {3897,-2056.1001,214.8,34.3, 0,0,0}, } local wind = {} function loadWind() if timehour <= 19 and timehour >= 6 then for i=1, #wind or 1 do if wind[i] and isElement(wind[i]) then destroyElement(wind[i]) outputChatBox ("Object destroyed") elseif not wind[i] or not isElement(wind[i]) then for k, v in pairs(windTable) do wind[k] = createObject(v[1], v[2], v[3], v[4], v[5], v[6], v[7]) end break end end end end Edited March 9, 2019 by Ab-47 Link to comment
Moderators IIYAMA Posted March 9, 2019 Moderators Share Posted March 9, 2019 (edited) 2 hours ago, Zcraks said: function chekTime () local timehour, timeminute = getTime(soucre) local windContainer = createElement("windContainer") if not wind or not isElement(windContainer) then wind = createObject(3892,-2072.5,285.5,34.3, 0,0,0) wind = createObject(3892,-2072.5,285.5,34.3, 0,0,0) wind = createObject(3895,-2035.6,263.39999,34.3, 0,0,0) wind = createObject(3896,-2096.5,146.5,34.3, 0,0,0) wind = createObject(3897,-2056.1001,214.8,34.3, 0,0,0) outputChatBox ("Object created") if wind then setElementParent(wind, windContainer) return wind end return false end elseif timehour <= 19 and timehour >= 6 then if doesWindExist () then destroyElement(windContainer) outputChatBox ("Object destroyed") windContainer = createElement("windContainer") return true return false return getElementChildrenCount ( windContainer ) > 0 end end addEventHandler( "onClientRender", getRootElement( ),chekTime) It is difficult to understand. What are my mistakes? Keep both codes separated like I did. 0. Restore the code before you applied the changes from my code. 1. Copy the first code 100% in yours. (On top of your script file?) 2. The second code is an edited version of your code. Check which changes I made in compare to yours. For example: createObject is now replaced with createWind. Which also creates an object and rerurns it. Not that you have to use the returned value in anyway. I have more or less already done 90% for you... so no stress, just observe and apply. Edited March 9, 2019 by IIYAMA Link to comment
Zcraks Posted March 9, 2019 Author Share Posted March 9, 2019 2 hours ago, IIYAMA said: Держите оба кода отдельно, как я. 0. Восстановите код до того, как вы применили изменения из моего кода. 1. Скопируйте первый код на 100% в свой. (Поверх вашего файла скрипта?) 2. Второй код - это отредактированная версия вашего кода. Проверьте, какие изменения я сделал по сравнению с вашими. Например: createObject теперь заменен на createWind. Который также создает объект и возвращает его. Не то, чтобы вы в любом случае использовали возвращаемое значение. Я более или менее уже сделал 90% для вас ... так что без стресса, просто наблюдайте и применяйте. do local windContainer = createElement("windContainer") function createWind () local wind = createObject() if wind then setElementParent(wind, windContainer) return wind end return false end function destroyWind () if doesWindExist () then destroyElement(windContainer) windContainer = createElement("windContainer") return true end return false end function doesWindExist () return getElementChildrenCount ( windContainer ) > 0 end function chekTime () local timehour, timeminute = getTime(soucre) if timehour <= 19 and timehour >= 6 then if wind and isElement(windContainer) () then destroyWind() outputChatBox ("Object destroyed") end elseif not doesWindExist () then wind = createWind(3892,-2072.5,285.5,34.3, 0,0,0) wind = createWind(3895,-2035.6,263.39999,34.3, 0,0,0) wind = createWind(3896,-2096.5,146.5,34.3, 0,0,0) wind = createWind(3897,-2056.1001,214.8,34.3, 0,0,0) outputChatBox ("Object created") end end end Sry but i didn't succeed Link to comment
Moderators IIYAMA Posted March 9, 2019 Moderators Share Posted March 9, 2019 @Zcraks Don't use the two codes in each other... ! The variable windContainer doesn't have to leave it's environment. It will only be used where it is needed! if wind and isElement(windContainer) () then The existence of wind has nothing to do with the existence of windContainer. This parent/container exists always. BUT the existence of wind is related to the children count of the windContainer. So if there are no children, then there is no wind to destroy. Just read the function names, those explain a lot. createWind = Creating a wind element. destroyWind = Destroying all wind elements. (by destroying it's parent) doesWindExist = Check if there are any wind elements. (based on children count of the parent) do local windContainer = createElement("windContainer") -- parent function createWind (...) local wind = createObject(...) if wind then setElementParent(wind, windContainer) -- make wind a child of windContainer return wind end return false end function destroyWind () if doesWindExist () then destroyElement(windContainer) windContainer = createElement("windContainer") return true end return false end function doesWindExist () return getElementChildrenCount ( windContainer ) > 0 end end function checkTime () local timehour, timeminute = getTime() if timehour <= 19 and timehour >= 6 then destroyWind() elseif not doesWindExist () then createWind(3892,-2072.5,285.5,34.3, 0,0,0) createWind(3895,-2035.6,263.39999,34.3, 0,0,0) createWind(3896,-2096.5,146.5,34.3, 0,0,0) createWind(3897,-2056.1001,214.8,34.3, 0,0,0) outputChatBox ("Object created") end end addEventHandler( "onClientRender", getRootElement( ),chekTime) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now