How do I use game methods? or destroy?
How do I use game methods? or destroy?
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.
Re: How do I use game methods? or destroy?
Its game.setalwaysday(true); (or false of course). The command is used in level 1 of the demo. there is also other useful stuff like game.getdaytime() etc.
and to use destroy just type entity.destroy()
I used the function in my treefarm mod to "harvest" the trees. maybe it is more cleraly when you see it in connection with some context
Anyway, ask again if anything is unclear.
and to use destroy just type entity.destroy()
I used the function in my treefarm mod to "harvest" the trees. maybe it is more cleraly when you see it in connection with some context
Anyway, ask again if anything is unclear.
Re: How do I use game methods? or destroy?
Oh. right.. there was no night in the first level.. I didn't even notice 

Re: How do I use game methods? or destroy?
I used this:
But tgis doesnt work.
Also, if I dont specify name, all entities there will be removed? And if I run it on a place where there is no entity?
Code: Select all
if event.name=="onplayerdied" then
spawn={}
spawn.position=game.getplayer().position
spawn.name="Spawn"
entity.remove(Spawn)
end
Also, if I dont specify name, all entities there will be removed? And if I run it on a place where there is no entity?
Re: How do I use game methods? or destroy?
First of all, are you sure that an event "onplayerdied" exists?
Also entity.remove(X) wont work unless you defined "entity" somewhere and defined a function "remove" somewhere in it.
I only know table.remove(X). This will delete the first element of X, where X is a table. So if you replace entity.remove(Spawn) with table.remove(Spawn) it will delete the first element of "Spawn", which ist "position".
And did you follow my advice to read my freeplay.lua from the treefarmmod? There is, like i said, a working example on how to use destroy().
Also entity.remove(X) wont work unless you defined "entity" somewhere and defined a function "remove" somewhere in it.
I only know table.remove(X). This will delete the first element of X, where X is a table. So if you replace entity.remove(Spawn) with table.remove(Spawn) it will delete the first element of "Spawn", which ist "position".
And did you follow my advice to read my freeplay.lua from the treefarmmod? There is, like i said, a working example on how to use destroy().
Re: How do I use game methods? or destroy?
Sorry, I wanted to say destroy, not remove, and I havent been able to see ur code yet, my mobile doesnt let me download .zip files.
Re: How do I use game methods? or destroy?
Ah ok, Here it is:
This is the whole "harvestpart" of the mod. I copied it completly so you can see where the specific tables /variables come from.
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
Re: How do I use game methods? or destroy?
I cant see things betwin [cod e][/co de], but anyways, I figures a way to download it, now I think I know how to do it.
Sorry, im mainly a java programmer, and I havent used lua much.
Sorry, im mainly a java programmer, and I havent used lua much.
Re: How do I use game methods? or destroy?
So in short. You have to find the specific entity somehow. At the moment "findentities" and "event.createdentity" come to my mind only.
For example the following code will destroy a placed entity instantly:
For example the following code will destroy a placed entity instantly:
Code: Select all
if event.name == "onbuiltentity" then
event.createdentity.destroy()
end
Re: How do I use game methods? or destroy?
the entity is at the player position when it dies (is a player port), or one tick later, idk sure, so I have the cords ( player cords) and the name (Spawn), so now I think I know how to do it.
Also the things that need to be into the entity value are;
entity.position.x
entity.position.y
entity.type
Or I need something more?
And having that I can just do entity.remove()?
Also the things that need to be into the entity value are;
entity.position.x
entity.position.y
entity.type
Or I need something more?
And having that I can just do entity.remove()?
Re: How do I use game methods? or destroy?
remove without position will pop out last element from table.
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela
Re: How do I use game methods? or destroy?
I think this should work:
local entities = game.findentities{ topleft =
{x = game.getplayer().position.x-0.5,
y = game.getplayer().position.y-0.5},
bottomright =
{x = game.getplayer().position.x + 0.5 ,
y = game.getplayer().position.y + 0.5}}
for i,entity in ipairs(entities) do
if (entity.name == "spawn") then
[ADDITIONAL CODE HERE]
entity.destroy()
[OR HERE]
(maybe a "break;" here, but not sure)
end
end
local entities = game.findentities{ topleft =
{x = game.getplayer().position.x-0.5,
y = game.getplayer().position.y-0.5},
bottomright =
{x = game.getplayer().position.x + 0.5 ,
y = game.getplayer().position.y + 0.5}}
for i,entity in ipairs(entities) do
if (entity.name == "spawn") then
[ADDITIONAL CODE HERE]
entity.destroy()
[OR HERE]
(maybe a "break;" here, but not sure)
end
end
Re: How do I use game methods? or destroy?
I cant specify the exact cordinates? I need to use the find function?
Re: How do I use game methods? or destroy?
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?
If you want to destroy all entities on some position, you really need to use find entities first to destroy it, and you can easily make function for that.
If you describe some simple example of what you want to do, i can try to give you code example.
Re: How do I use game methods? or destroy?
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
Would that work?
Re: How do I use game methods? or destroy?
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?
We could make special event for that (oncharacterrespawned), in that case it could be done this way:
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
findfirstentityonposition(event.character.position, "player-port")
Re: How do I use game methods? or destroy?
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
Re: How do I use game methods? or destroy?
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
Re: How do I use game methods? or destroy?
so if I do this
This should work?
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
Re: How do I use game methods? or destroy?
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