How do I use game methods? or destroy?

Place to get help with not working mods / modding interface.
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

How do I use game methods? or destroy?

Post by ficolas » 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.

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

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

Post by drs9999 » Wed Mar 13, 2013 6:35 pm

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.

MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

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

Post by MF- » Wed Mar 13, 2013 6:50 pm

Oh. right.. there was no night in the first level.. I didn't even notice :)

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Fri Mar 15, 2013 10:11 pm

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?

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

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

Post by drs9999 » Fri Mar 15, 2013 10:52 pm

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().

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Fri Mar 15, 2013 10:58 pm

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.

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

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

Post by drs9999 » Fri Mar 15, 2013 11:08 pm

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.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Fri Mar 15, 2013 11:15 pm

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.

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

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

Post by drs9999 » Fri Mar 15, 2013 11:18 pm

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

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Fri Mar 15, 2013 11:21 pm

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()?

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

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

Post by rk84 » Fri Mar 15, 2013 11:29 pm

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

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

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

Post by drs9999 » Fri Mar 15, 2013 11:34 pm

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

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Fri Mar 15, 2013 11:45 pm

I cant specify the exact cordinates? I need to use the find function?

kovarex
Factorio Staff
Factorio Staff
Posts: 7291
Joined: Wed Feb 06, 2013 12:00 am
Contact:

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

Post by kovarex » Fri Mar 15, 2013 11:50 pm

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.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Fri Mar 15, 2013 11:57 pm

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?

kovarex
Factorio Staff
Factorio Staff
Posts: 7291
Joined: Wed Feb 06, 2013 12:00 am
Contact:

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

Post by kovarex » Sat Mar 16, 2013 12:10 am

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")

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Sat Mar 16, 2013 12:13 am

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

kovarex
Factorio Staff
Factorio Staff
Posts: 7291
Joined: Wed Feb 06, 2013 12:00 am
Contact:

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

Post by kovarex » Sat Mar 16, 2013 12:55 am

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

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas » Sat Mar 16, 2013 10:11 am

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?

kovarex
Factorio Staff
Factorio Staff
Posts: 7291
Joined: Wed Feb 06, 2013 12:00 am
Contact:

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

Post by kovarex » Sat Mar 16, 2013 1:17 pm

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

Post Reply

Return to “Modding help”

Who is online

Users browsing this forum: No registered users