Page 1 of 3

How do I use game methods? or destroy?

Posted: Wed Mar 13, 2013 5:30 pm
by ficolas
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?

Posted: Wed Mar 13, 2013 6:35 pm
by drs9999
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.

Re: How do I use game methods? or destroy?

Posted: Wed Mar 13, 2013 6:50 pm
by MF-
Oh. right.. there was no night in the first level.. I didn't even notice :)

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 10:11 pm
by ficolas
I used this:

Code: Select all

if event.name=="onplayerdied" then
spawn={}
spawn.position=game.getplayer().position
spawn.name="Spawn"
entity.remove(Spawn)
end
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?

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 10:52 pm
by drs9999
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().

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 10:58 pm
by ficolas
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?

Posted: Fri Mar 15, 2013 11:08 pm
by drs9999
Ah ok, Here it is:

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
This is the whole "harvestpart" of the mod. I copied it completly so you can see where the specific tables /variables come from.

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 11:15 pm
by ficolas
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.

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 11:18 pm
by drs9999
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:

Code: Select all

if event.name == "onbuiltentity" then
   event.createdentity.destroy()
end

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 11:21 pm
by ficolas
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()?

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 11:29 pm
by rk84
remove without position will pop out last element from table.

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 11:34 pm
by drs9999
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

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 11:45 pm
by ficolas
I cant specify the exact cordinates? I need to use the find function?

Re: How do I use game methods? or destroy?

Posted: Fri Mar 15, 2013 11:50 pm
by kovarex
ficolas wrote:I cant specify the exact cordinates? I need to use the find function?
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)
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?

Posted: Fri Mar 15, 2013 11:57 pm
by ficolas

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 is the code I need, when the player dies and there is a spawn (player port) that revives him, the player port is removed, like so is a one-use-only player port.

Would that work?

Re: How do I use game methods? or destroy?

Posted: Sat Mar 16, 2013 12:10 am
by kovarex
ficolas wrote:Would that work?
That wouldn't work, because when the player is respawned, the entitydied event is not created, just when he finally dies.
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
Function to find entities coliding with position (not just with bounding box) could be useful as well, something like
findfirstentityonposition(event.character.position, "player-port")

Re: How do I use game methods? or destroy?

Posted: Sat Mar 16, 2013 12:13 am
by ficolas
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?

Posted: Sat Mar 16, 2013 12:55 am
by kovarex
ficolas 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
It is instant

Re: How do I use game methods? or destroy?

Posted: Sat Mar 16, 2013 10:11 am
by ficolas
so if I do this

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
This should work?

Re: How do I use game methods? or destroy?

Posted: Sat Mar 16, 2013 1:17 pm
by kovarex
ficolas wrote:so if I do this This should work
no

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