About LuaObj.valid property

Place to get help with not working mods / modding interface.
kendoctor
Inserter
Inserter
Posts: 34
Joined: Wed Aug 05, 2020 6:43 pm
Contact:

About LuaObj.valid property

Post by kendoctor »

I'm still confused for this valid property.

How, when, what trigger this property to be false and luobj is still not nil ?

As i discovered, when deleted a surface in one event, the surface will be actually destroyed in next tick. It seems this will happen. (I did not test it)
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: About LuaObj.valid property

Post by DaveMcW »

Code: Select all

/c
global.entity = game.player.selected
game.print(global.entity.valid)  -- true
game.player.selected.destroy()
game.print(global.entity.valid)  -- false
If you store anything in global, you must always check .valid because something else destroy it.
kendoctor
Inserter
Inserter
Posts: 34
Joined: Wed Aug 05, 2020 6:43 pm
Contact:

Re: About LuaObj.valid property

Post by kendoctor »

DaveMcW wrote: Thu Aug 20, 2020 10:00 am

Code: Select all

/c
global.entity = game.player.selected
game.print(global.entity.valid)  -- true
game.player.selected.destroy()
game.print(global.entity.valid)  -- false
If you store anything in global, you must always check .valid because something else destroy it.
thx your reply.
should i consider like this .
In one tick lifecycle.
1. the system give u a valid entity
2. if you destory it using script, the entity is still not nil, but valid is false.
3. when this tick ended, in next tick, this entity will be nil

Another scenario.
1. in one event, destroy the entity and listen its destroy event
2. in destroy event , this entity is nil or invalid ?
3. previous event with the destroy event is in the same tick ?

Facto is using determisitic strategry. I think all results should be predicted.
Can i consider, Any system event will never give u invalid entity?
Only u manully destroyed it in one tick and access the entity in the same tick, that will happen, not nil but invalid.
kendoctor
Inserter
Inserter
Posts: 34
Joined: Wed Aug 05, 2020 6:43 pm
Contact:

Re: About LuaObj.valid property

Post by kendoctor »

Code: Select all

--- Clone carriage from the old.
-- @tparam class<Carriage> old_carriage
function Carriage:clone(old_carriage)
    local restore = function(e)
        -- @todo more accurate and flexiable is to check unit_number in CarriageDoorManager
        if e.source.name ~= "player-port" then return end
        local old_doors = old_carriage.doors
        for k, door in pairs(old_doors) do 
            if door.factoobj == e.source then self:createDoorByClone(door, { factoobj = e.destination, carriage = self }) end 
        end         
    end 
    Event.on(defines.events.on_entity_cloned, restore)
    self:cloneArea(old_carriage)    
    Event.remove(defines.events.on_entity_cloned, restore)
end 
Until now, i tested this script is allright for main logics.
But i still wonder, all on_entity_cloned events will be triggered in this tick or some will be triggered in next tick ?
Post Reply

Return to “Modding help”