Jump to content

meeting two conditions


The_Real_Al

Recommended Posts

That's what thought. But i should probably post the specific case.

Following the tutorial i've done a script thats adds an item when you press a key. Then i added another key to remove it:

packmule = {};function packmule(_key)    local inv = getSpecificPlayer(0):getInventory()        if         _key == 38 then        inv:AddItem("bbag.BottomlessBag")    elseif    _key == 25 then        inv:Remove("BottomlessBag");    endendEvents.OnKeyPressed.Add(packmule);

 

Now i just want to use one key, press it once to add and again to remove. That's what i came up with:

packmule = {};function packmule(_key)    local inv = getSpecificPlayer(0):getInventory();        if         _key == 38 and inv:contains("bbag.Bottomlessbag") then                    inv:Remove("BottomlessBag");    else                    inv:AddItem("bbag.BottomlessBag");    end    endEvents.OnKeyPressed.Add(packmule);

 

Now everytime i press any key it spawns the item.

Link to comment
Share on other sites

The else will happen if *either* of the parts of the and aren't true, so it will always do the else when any key on the keyboard except 38 is pressed (not key == 38)

 

Like saying...

 

if the room is hot AND the window is closed THEN

   open window

else

   stick head out window

end

 

The else here will just as likely result in a bad head when you bash it against the closed window when the room you're in is cold anyway. You assume the 'else' applies to the window being closed, and thus 'else' meaning its open and thus to cool down you need to stick your head out. But it's not. The condition is if the room is hot AND window is closed. So the else will happen if the room is cold, OR if the window is open.

 

Me thinks you mean to do this:

if key == 38 then     if inv:contains("bbag.Bottomlessbag") then            inv:Remove("bbag.BottomlessBag");     else            inv:AddItem("bbag.BottomlessBag");     endend

(another thought that occurs to me is that having a key bind that destroys the bag with all your loot in it as soon as its pressed (intentionally or no) seems a dangerous thing to do :D)

Link to comment
Share on other sites

"You ugly child of Hades! The entire inventory will drown! Everywhere i look, i see bags, bags spawning down."

 

I had a growing suspicion that this 'else' was causing the spawn but no clue why pressing L wouldn't remove them. No worries about the loot though. This is more of an exercise learning lua. Next lesson: How to 'not' 'and' 'or'.

 

However it appears this doesn't quite work. This is the code in the file:

packmule = {}; function packmule(_key)local inv = getSpecificPlayer(0):getInventory();if _key == 38 then    if inv:contains("Base.Axe") then            inv:Remove("Base.Axe");    else            inv:AddItem("Base.Axe");    endendendEvents.OnKeyPressed.Add(packmule);

 

Only spawns the item on key. Am i right to assume that the position of 'local inv = ...' is correct?

Link to comment
Share on other sites

It doesn't remove the item because the Remove() function expects an InventoryItem as a parameter (IIRC) and you passed it a String "Base.Axe". Try RemoveOneOf(String) instead ... it should work.

 

Also you made an oopsie naming your function. Packmule is the global table of your script and all global variables and functions should be in there. Your function should be named "Packmule.addItems" or something like that (I always use uppercase for packages / global tables).

 

Not and or are pretty easy ... not reverses the condition so false = true and true becomes false. It can be used to make simple switches like this

 

-- reverse conditionflag = not flag;

 

or can be used like and. The whole test will be true once one of the conditions is true. Read it as "if condition1 is true or condition 2 is true do awesome stuff".

 

I highly suggest that you read this:

http://www.lua.org/pil/3.3.html

Link to comment
Share on other sites

Oh boy, it hit me.

 


Remove() function expects an InventoryItem as a parameter (IIRC) and you passed it a String "Base.Axe"

Actually a string is fine. I used it in the two-keys version already. But the comment in javadocs also states "no need" for the module. That should be "must not" and immediately pasted into 'contains()'. Makes sense  - sort of - since both reference an item in the inventory.

The code is thus:

packmule = {}function packmule.addItems(_key)    local inv = getSpecificPlayer(0):getInventory()        if _key == 38 then            if     inv:contains("Axe") then                inv:Remove("Axe")            else                inv:AddItem("Base.Axe");            end        endendEvents.OnKeyPressed.Add(packmule.addItems)

 

Now look at it. My very first mod. No more than a modling yet.

Thanks for the help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...