how to use clear()?

Place to get help with not working mods / modding interface.
Post Reply
drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

how to use clear()?

Post by drs9999 »

Sorry for spamming the forum, but just another question:

How I can use the clear-method?

I tried: glob.chest.cleared() but I got an "invalid key"- error...

Anyway, maybe there is a better way to achieve what I want.

I want to check if a specific chest contains a specific item, if yes then his amount should be decreased by one.
What I tried so far:

Code: Select all

if glob.chest.getitemcount("coal") > 0 then
    local coal =glob.chest.getitemcount("coal")
    local wood = glob.wood.getitemcount("raw-wood")

   glob.chest.cleared()													
   coal=coal-1
						
   glob.field[i].insert{name="coal", count= coal}
   glob.field[i].insert{name="raw-wood", count=wood}
end
And:

Code: Select all

if glob.chest.getitemcount("coal") > 0 then					
   glob.field[i].insert{name="coal", count= -1}
end
FIrst version gives me the "invalid key"- error and also if i delete that line and it seem like using a variable behind the "count=" wont work very well (game crashed ;) )

Second version isnt better. After a huge lag the invetory is completly full :D

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

Re: how to use clear()?

Post by kovarex »

The function is clear, not cleared.

The addition of -1 shouldn't be possible (it tries to add 2^32 - 1 instead :) I will correct that.

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

Re: how to use clear()?

Post by ficolas »

drs9999 wrote:Sorry for spamming the forum, but just another question:
That is not spamming.

Also, there isnt any way of extracting from a container?? (removing an item) without clearing the whole container?

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

Re: how to use clear()?

Post by kovarex »

ficolas wrote:
drs9999 wrote:Sorry for spamming the forum, but just another question:
That is not spamming.
Of course not.

ficolas wrote: Also, there isnt any way of extracting from a container?? (removing an item) without clearing the whole container?
Good point, I just added this function for the next version.

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

Re: how to use clear()?

Post by drs9999 »

Ah extracting was the word I was looking for :D

Anyway I also only misstyped "cleared" in the example code. I tried both clear() and cleared() => same error

Also what is about the possibility to add a variable amount of items to a chest.

For example the player have to kill 50 creepers as fast as possible. If he kills them in under 5 min then he should receive 200 iron-plates, if it takes longer than 5 min then he only should receive 100.

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

Re: how to use clear()?

Post by ficolas »

Code: Select all

if event.name=="onunitdied" then
    if count.started==nil or false then
        if count==nil then
       glob.count={}
      glob. count.started={}
       glob. count.kills={}
    end
        glob.count.started=true
       glob.count.kills=0
    end
     glob.count.kills=count.kills+1
end

if glob.count.started==true then
    if count.time==nil then
      glob.count.time={}
      glob.count.time=0
    end
    glob.count.time=count.time+1
    if glob.count.time==120000
        if glob.count.kills>=50
            game.getplayer().insert(name=iron-plate, count=200)
            else
             game.getplayer().insert(name=iron-plate, count=100)
        end
    end
end
    


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

Re: how to use clear()?

Post by ficolas »

Sorry for the shitty code, I wrote it with my mobile phone.

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

Re: how to use clear()?

Post by drs9999 »

Thanks for the code, but that is not what i am looking for. But i noticed that I choose a bad example maybe.
That example point it out better:
Player have to kill as many creepers as possible in 5 min. after the 5 min he should receive 1 iron-plate for 1 creeper-kill

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

Re: how to use clear()?

Post by ficolas »

Code: Select all

 if event.name=="onunitdied" then
    if count.started==nil or false then
        if count==nil then
       glob.count={}
      glob. count.started={}
       glob. count.kills={}
    end
        glob.count.started=true
       glob.count.kills=0
    end
     glob.count.kills=count.kills+1
end

if glob.count.started==true and event.name=ontick then
    if glob.count.time==nil then
        glob.count.time={}
        glob.count.time=0
     end
glob.count.time=glob.count.time+1
end

if glob.count.time==6000 then
game.getplayer().insert(name=iron-plate, count=glob.count.kills)
glob.count.time=0
glob.count.kills=0
glob.count.started=false
end
 

Post Reply

Return to “Modding help”