Action If Character Has Item In Inventory

  • #1, by snowMonday, 04. June 2018, 11:32 7 years ago
    Hello

    There is the option, "If character has item" then perform action

    I would like to say If character has three items then perform the action.

    Eg.   If Character has, the hammer, nails, and wood  perform action

    How would I do this ?

    Kind Regards

    Newbie

    23 Posts


  • #2, by riffmasterMonday, 04. June 2018, 12:13 7 years ago
    Hi,

    I am rather sure that you will have to use a combined condition.

    You can achieve this by creating three conditions anywhere, just like

    has_item1, has_item2, has_item3

    which you initialize with false,

    and adding a combined condition has_allitems, which is true if all these three are true. You will have to add these conditions to the has_allitems one for this.

    The has_allitems condition you can use in your action sequence.

    EDIT: of course you will have to set the corresponding condition to true when the player picks up one of the items smile

    Newbie

    26 Posts

  • #3, by afrlmeMonday, 04. June 2018, 12:39 7 years ago
    Just create 3 if character has item queries with the action part then add the action parts you want, then 3 end if action parts on the end.


    Alternatively you could use the if lua result query action part...

    local result = 0
    local items = game.CurrentCharacter.Items
    
    for i = 1, #items do
     if items[i]:getName() == "hammer" or items[i]:getName() == "nails" or items[i]:getName() == "wood" then
      result = result + 1
      if result == 3 then return true end
     end
    end
    return false

    Not 100% sure it's correct as it's written out from my head without testing. The Lua result action part requires you to return true or false. In the example we are iterating through the different items the character has & listening out for specific ones by name & if it finds one then it adds + 1 to the result variable we created. if the variable ends up with a value of 3 at any point then it returns true. if the for loop ends then it returns false.

    I recommend using the action part method in the screenshot I posted rather than the Lua approach in this case as it's much simpler & far faster.

    Imperator

    7285 Posts

  • #4, by riffmasterMonday, 04. June 2018, 14:52 7 years ago
    Oh, sorry, didn't think about AFRLme's solution, which of course is much easier and smarter and obvious.

    Newbie

    26 Posts

  • #5, by snowTuesday, 05. June 2018, 03:56 7 years ago
    Thank you both for the excellent suggestions.

    I did try AFRLme's solution, but there seems to be a issue.

    This code works if you have the items in inventory, but the else tag at the end if you don't have the items, does not seem to be working.

    eg.
    If I click on the object and don't have those items, my display text message does not work.

    see screenshot:

    Kind Regards

    Newbie

    23 Posts

  • #6, by esmeraldaTuesday, 05. June 2018, 08:37 7 years ago
    You set up your display text to show when the player has got the gun-with-bullet, but not the money-bag.
    If you want the display text to show when the player hasn't the gun, you need to move the else down under the first end if.

    Key Killer

    533 Posts

  • #7, by sebastianTuesday, 05. June 2018, 08:54 7 years ago
    You set up your display text to show when the player has got the gun-with-bullet, but not the money-bag.
    If you want the display text to show when the player hasn't the gun, you need to move the else down under the first end if.
    ... which would then shift the issue to "if have not the gun, but the money bag". So effectively you would need to have an else for each if statement. 

    Because this seems to be a bit cumbersome
    to create 3 "i dont have All items" messages, you could do it by just have ONE if lua result action part, which checks if 3 conditions are true:

    __________

    if lua result:
    return Conditions["item1_found" ].Value and Conditions["item2_found" ].Value and 
    Conditions["item3_found" ].Value

    else

    display text: "i still need to find my things" 

    end if

    _________

    therefore you need to create these 3 conditions in the first place and also set them to true when you add them into the inventory. 

    ~Sebastian 


    Thread Captain

    2346 Posts

  • #8, by snowTuesday, 05. June 2018, 09:54 7 years ago
    Hello, thank you all for the replys.

    Moving the else under the endif fixed the issue I was having.

    Kind regards

    Newbie

    23 Posts

  • #9, by esmeraldaTuesday, 05. June 2018, 09:58 7 years ago
    You set up your display text to show when the player has got the gun-with-bullet, but not the money-bag.
    If you want the display text to show when the player hasn't the gun, you need to move the else down under the first end if.
    ... which would then shift the issue to "if have not the gun, but the money bag". So effectively you would need to have an else for each if statement. 

    Yep, that is true. If you move the else to the first if-query the text would display wether the player has the money bag or not. The engine would only check if the player has got the gun or not. 

    Instead of three conditions you could use a value and increase the value +1 when the player picks up one of the items. 




    Key Killer

    533 Posts

  • #10, by snowTuesday, 19. June 2018, 04:52 7 years ago
    Hello

    I have a small issue with an if statement.

    The first line works, if I have the three needed items.
    However the following else if, if I don't have the correct items does not work.

    I am most likely missing an end if tag or something simple.

    Any solutions would be appreciated

    Newbie

    23 Posts

  • #11, by esmeraldaTuesday, 19. June 2018, 10:06 7 years ago
    I don't know if you want to cover every possibility and give a different output for every item combination.

    You have to consider which items the character can have at that moment.
    One possibility to do it is this:

    Key Killer

    533 Posts