Announcement

Collapse
No announcement yet.

Problem keeping array of objects "alive"

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Problem keeping array of objects "alive"

    Hello i'm very new to Lua Programming and scripting for games, actually it's my first try. i 'm trying to build the basic mecanics for seeding to harvest a weed plant ( ilegal job )

    what i wanna do is : you will have seeds ( not yet implemented) , you will plant them with "H" , after they ready to harvest , you will press "E" to collect. A bag will show on the ground and then you will be able to pick it up as a weed bag

    i can plant lots of plants, i see the plants growing up, they are all added to an array and updated with the client ( i just used the atm_c.lua and atm_s.lua examples on blue montains github)

    then the player can harvest the first plant. After that i remove the plant from the array and update client with it. The harvest function ("Colher") has 2 args .. plant object and player object.

    When i hit "E" to harvest the SECOND plant.. i get that plant object and player object ( harvest function arguments) they are not valid objects. I dont understand very well how Onset works ..so i might be trying something impossible, i dont know.

    You see a way i can do it, i can change my entire code.. no problem.

    Any help is appreciated! thanks in advance!

    video on youtube showing the problem : https://www.youtube.com/watch?v=Ejt_nYvJkLk

    here is my scripts :

    ilegalmaconha_c :

    Code:
    local PlantasIds = {}
    local plantas_ = {}
    local player = GetPlayerId()
    
    AddRemoteEvent("ilegalmaconha:update", function(PlantaObjects)
    
    
            plantas_ = PlantaObjects
    
            --[[clear array]]--    
            PlantasIds = {}
    
            for _,v in pairs(plantas_) do
                if IsValidObject(v) then
                    table.insert(PlantasIds, v)
                end
            end
    
    end)
    
    local function OnKeyPress(key)
    
          --[[
           Player is on foot. he can do some stuff  
          ]]--
          if not(IsPlayerInVehicle()) then
    
            --[[
              Player will plant another Plant
              Later implementation : check inventory for seed. dont know how to do it yet
            ]]--
            if(key == 'H') then
                CallRemoteEvent("Plantar")      
            end
    
            --[[ Player will harvest Plant ]]--
            if(key == 'E') then
    
                local x, y, z  = GetPlayerLocation()
    
                for _,v in pairs(PlantasIds) do
    
                    --to get the closer plant, implementation for later
                    --local x2, y2, z2 = GetObjectLocation(v)
                    --local dist = GetDistance3D(x, y, z, x2 , y2, z2)
    
                    if IsValidObject(v) then
                        CallRemoteEvent("Colher", v, player)
                        break
                    end
                end
            end
          end
    end
    AddEvent("OnKeyPress", OnKeyPress)
    Server side :
    ilegalmaconha_s :

    Code:
    Plantas = {}
    idplantas = 0
    
    
    --[[ util ]]--
    function tablelength(T)
      local count = 0
      for _ in pairs(T) do count = count + 1 end
      return count
    end
    
    function tablefind(tab, el)
        for index, value in pairs(tab) do
            if value == el then
                return index
            end
        end
    end
    
    
    function Plantar(player)
    
        local planta;
        local TIMER_PLANTAR = 1000;
        local TIMER_PLANTAR_CRESCER = 10000;
        local TIMER_PLANTAR_FLORIR = 10000;
    
        AddPlayerChat(player,"New Plant...")    
    
    
        --Start animation
    
    
        SetPlayerAnimation(player, "PICKUP_LOWER")    
    
        --[[
            First step of Plant - little one.
        ]]--
    
          Delay(TIMER_PLANTAR, function()
    
                --[[ Get position of player to set Plant , little adjustment of Z coord.
                ]]--
    
    
                local x, y, z = GetPlayerLocation(player)
                planta = CreateObject(64, x, y, z)
    
                idplantas = idplantas + 1
    
                SetObjectPropertyValue(planta, "nome", "planta"..tostring(idplantas))
                SetObjectScale(planta,0.2,0.2,0.2)
    
                local xo, yo, zo = GetObjectLocation(planta)            
                SetObjectLocation(planta, xo, yo, zo-95.0695)
    
                --[[Second stage of plant - vegging]]--
                Delay(TIMER_PLANTAR_CRESCER, function()
                    SetObjectScale(planta,0.7,0.7,0.7)
    
                    --[[Third stage - flowering ]]--
                    Delay(TIMER_PLANTAR_FLORIR, function()
                        SetObjectScale(planta,1.3,1.3,1.3)
    
                        --Plant is ready to harvest, add to Harvestable Plants array
                        table.insert(Plantas, planta)
                        AddPlayerChatAll("Plant ready to harvest : " .. GetObjectPropertyValue(planta, "nome"))
    
                        --update client with remaining harvestable Plants
                        CallRemoteEvent(player, "ilegalmaconha:update", Plantas)
    
                    end)
    
                end)
          end)
    end
    AddRemoteEvent("Plantar", Plantar)
    
    function Colher(object, player)
    
        AddPlayerChatAll("New Harvest...")
    
        for _,v in pairs(Plantas) do
            AddPlayerChatAll("Plants to harvest - " .. GetObjectPropertyValue(v, "nome"))
        end
    
        if IsValidPlayer(player) then
          AddPlayerChat(player, "Player harvesting = " .. GetPlayerName(player))
        else
          AddPlayerChatAll("Player not found!")        
        end
    
        local nomeplanta = ""
        local x, y, z
    
        if IsValidObject(object) then
            nomeplanta = GetObjectPropertyValue(object,"nome")
            x, y, z = GetObjectLocation(object)
        end
    
        if nomeplanta == "" then
            AddPlayerChatAll("Plant not found!")
        else
            AddPlayerChatAll("Plant to be harvest : " .. nomeplanta)
        end
    
        for _,v in pairs(Plantas) do
            AddPlayerChatAll("Verifying Plant  - " .. GetObjectPropertyValue(v, "nome"))
            if GetObjectPropertyValue(v,"nome") == nomeplanta then
                AddPlayerChatAll("Plant Removed : " .. nomeplanta)
                table.remove(Plantas, tablefind(Plantas, v))    
                CallRemoteEvent(player,"ilegalmaconha:update", Plantas)
                DestroyObject(object)
                local pacotemaconha = CreateObject(619, x, y, z)
                SetObjectScale(pacotemaconha,0.5,0.5,0.5)
                break
            end
        end
    
    end
    AddRemoteEvent("Colher", Colher)
    Last edited by fferrandini; 01-18-2020, 06:32 PM.

    #2
    In your function 'Colher' your argument order is incorrect.

    This is how yours is ordered:
    Code:
     
     function Colher(object, player)
    This is how it should be ordered:
    Code:
     
     function Colher(player, object)
    When adding a remote event, the function sent as the "callback" (in this case, Colher) is sent first the player that called the remove event, then the data sent with the event.

    When you call the remove event on the client side, you are sending 'v', which is the plant object, and player, which is the player. However, the remote events add their own argument to that, player, and it is added before all other arguments.

    Comment


    • fferrandini
      fferrandini commented
      Editing a comment
      i will give it a try, thanks in advance.

    • fferrandini
      fferrandini commented
      Editing a comment
      damn i was so dumb.. it worked with Function Colher(player, object) and CallRemoteEvent("Colher", plantobject)!

      of course.. when a remote event is called on server, he always gets player as first arg .. so dumb of me... thanks man!
Working...
X