How do I use game methods? or destroy?
Posted: Wed Mar 13, 2013 5:30 pm
How do I use game methods? like setalwaysday, I tried doing game.setalwaysday but that doesnt work, I started trying this because I want to destroy an entity.
Code: Select all
if event.name=="onplayerdied" then
spawn={}
spawn.position=game.getplayer().position
spawn.name="Spawn"
entity.remove(Spawn)
end
Code: Select all
local entities = game.findentities{ topleft = -- scan the actual field
{x = glob.field[i].position.x,
y = glob.field[i].position.y},
bottomright =
{x = glob.field[i].position.x + 8 ,
y = glob.field[i].position.y + 8}}
local k = 0
for _,entity in ipairs(entities) do -- and count the trees on it
if (entity.type == "tree") then
k=k+1
end
end
-- more than 4 trees spawned and space in inventory?
if k > 4 and glob.field[i].getitemcount("raw-wood") < 128 then -- Yes, then (maybe) start chopping
local rnde = math.random(1 , 10) -- 30% Chance that a tree will be chopped
local rndz = math.random(1 , k) -- of course a random tree
local rndd = 0 -- random output
if glob.field[i].getitemcount("fertilizer") > 0 then -- random wood-output (with/without fertilizer)
rndd = math.random(1 , 6)
else
rndd = math.random(1 , 4)
end
if rnde > 7 and entities[rndz].type == "tree" then -- and of course only if the entitiy is a tree and not the field
entities[rndz].destroy() -- remove tree
glob.field[i].insert{name="raw-wood", count=rndd} -- add wood to the container
end
end
Code: Select all
if event.name == "onbuiltentity" then
event.createdentity.destroy()
end
You need to "hold" the object you want to destroy, or do any other stuff with that (https://forums.factorio.com/wiki/index.php/Lua/Entity)ficolas wrote:I cant specify the exact cordinates? I need to use the find function?
Code: Select all
if event.name="onplayerdied" then
entity={}
entity.position=game.getplayer().position
entity.type=Spawn
if entity.isvalid() then
entity.remove()
end
end
That wouldn't work, because when the player is respawned, the entitydied event is not created, just when he finally dies.ficolas wrote:Would that work?
Code: Select all
if event.name="oncharacterrespawned" then
-- I have just the access to the character now, I have to find the corresponding spawner,
-- so let's look around the player
-- the findfirstentity is just utility function defined in util.lua it uses findentities but filters
-- it by the name of the entity specified
local spawner = findfirstentity({topleft = {x = event.character.position.x - 1,
y = event.character.position.y - 1},
bottomright = {x = event.character.position.x + 1,
y = event.character.position.y + 1}},
"player-port")
if spawner ~= nil then -- if the spawner was found, I destroy it
spawner.destroy()
end
end
It is instantficolas wrote:How many ticks do the player take to respawn? I can wait one tick, then check if the player have health (so he have respawned) and then run the other part
Code: Select all
if glob.playerdiedtick==true then
glob.playerdiedtick=false
spawn={position=game.getplayer().position}
spawn.type=Spawn
spawn.remove()
end
if event.name=="onplayerdied" then
glob.playerdiedtick=true
end
noficolas wrote:so if I do this This should work
Code: Select all
if glob.playerdiedtick==true then
glob.playerdiedtick=false
spawn={position=game.getplayer().position} -- so you just created variable spawn that is table, containing position
spawn.type=Spawn -- you just added property type to the table variable
spawn.remove() -- now you are trying to call method on the table, it isn't defined, so the script crashes
-- You just made a table, this has nothing to do with the entity object.
end
if event.name=="onplayerdied" then
glob.playerdiedtick=true
end