Modding Help Detecting what item a player has in their hand...

Discussion in 'Starbound Modding' started by Caudyr, Sep 16, 2015.

  1. Caudyr

    Caudyr Black Hole Surfer

    I actually tried to look at the API documentation for the game to see if I could find anything that would do this...but the site appears to be down saying that it was out of date and thus taken down, or something like that.

    Anyway, I'm kinda asking this for someone else...but also for myself. Does anyone know what lua method you would use to detect what item a person currently has selected for their hand?



    To give an example, I'm thinking about something I had mentioned in the Enhanced Storage mod's discussion thread that the author didn't know how to do. It is:

    The mod currently allows chests to maintain their inventories when they're broken. However, I wanted to suggest an item that the user could use to break chests and have their contents be spilled out (would be extremely useful for chests people find while exploring, so they didn't have to empty them item by item before breaking them).

    In the lua for it, he's already got a check for a chest (with items in it) within a chest, where if that's the case...when the placed chest is broken, all the chests that have items within them that are inside of it will be spilled out.



    What I want to suggest is that he simply adds a check to see if that item is currently equipped by the player when they break the chest...and if so, have it spill the contents just like the aforementioned chests do...but that would require me to know what method he could call to detect this sort of thing.

    However, I can't search the API documentation to figure out what calls I can use to do this, etc...since it's down (unless there's a new link that's not in the API documentation thread, heh). Anyone able to help with this question and/or a link to the documentation if there is one, by chance? ^^

    Thanks in advance~ :D
     
  2. LoPhatKao

    LoPhatKao Space Kumquat

    docs are down/gone :(

    depending which entity is the caller (player or the chest) 2 different ways to get hand item
    Code:
    entity.getItemSlot(slot)  --[slot = "primary" or "alt"]
    or
    world.entityHandItem(entityId, slot)  --[slot = "primary" or "alt"]
     
  3. Caudyr

    Caudyr Black Hole Surfer

    Would this also work for items that are on the hotbar but not in the player's "primary/secondary" slots for weapons, etc.? Basically...think of it like a pickaxe...you'd put it in...say...slot #2 of your hotbar, and then mouse wheel to it in order to use it there. Will either of these recognize that it's selected, even if it's not in the character's actual "primary/secondary" slots?

    EDIT: Also, when you say "entityID", does that indicate the itemName field...or something else in these files that I haven't come across yet?
     
  4. LoPhatKao

    LoPhatKao Space Kumquat

    afaik, primary will read whatever item is selected in hotbar or if holding an item (havent tried tho)

    entityId is .. the entity id number heh.. whatever # you get from calling entity.id() or world.entityQuery()
    (for example: in a single-player game, player is always entity id # -65535)
     
  5. Caudyr

    Caudyr Black Hole Surfer

    Oh, so the first example entity.getItemSlot(STUFF) would be what's mainly used for players then, I'm guessing?

    When the chest is broken, what I want to suggest is that it basically check to see if the user broke it with a specific item...and if so, then the chest's contents will be spilled...otherwise they'll function like they normally would in the ES mod (the chests already c heck to see if there are other filled chests within them when they're broken). ^^

    EDIT: Also...what about if one were to simply want to detect that the item is broken with shift+click (as in, "fine control" of the manipulator, etc.)? Is there an easy way to look for shift+click when breaking an object instead? It might be easier to do that, if so.

    I know there's a way to look for "primaryFire" and "secondaryFire" I.e. right click and left click...but what about shift+click? ^^
     
    Last edited: Sep 17, 2015
  6. LoPhatKao

    LoPhatKao Space Kumquat

    yeah, entity.get~ is more for if you are looking at entity's own hand (guard npcs use it to see what weapon they have equipped - gun / sword / shield)
    so the way you describe the scenario, the entity currently running script would be the chest that is breaking (so entity.getItemSlot would not be the right func)
    so you'd be calling something like
    Code:
    if world.entityHandItem(breaking_player_id,"primary") == "chestbreaking_itemname" then
    -- do cool stuff here
    else
    -- breaking stuff when not holding breaker item
    end
    
    ( i should unpak ES so i can see what exactly you're after.. but lazy ;))
    there might be existing code to just search in players inventory for an item, instead of requiring it to be in hand... this is where i would have gone mining the docs :(

    now for the shift+click .. i really have no idea there ;D
    i would guess (might be wrong) that game engine handles that, as all that really changes is the area size that would get sent to the C equivalent of world.damageTiles()
     
  7. Caudyr

    Caudyr Black Hole Surfer

    Hrm...yeah, I'm guessing it does too...but there's variables for detecting primaryFire and secondaryFire..so I figured there's a pretty good possibility that there are ones for their alternative fires as well (shift+click). I just...don't know the name of those variables if they exist. ^^

    Anyway, thanks for the clarification on entityHandItem and whatnot...and on the information on how to do this stuff in general! ^^
     
  8. C0bra5

    C0bra5 Oxygen Tank

    here are all the variables for the args.moves , arg beeing the variable used for the input function eg.: function input(args)
    Code:
    primaryFire : false
    special : 0  (goes from 0 to 3, 0 = none, 1=1st special, 2 = 2nd special, 3 = 3rd special)
    jump : false
    run : true
    up : false
    altFire : false
    right : false
    down : false
    left : false
    
    according to this you need to write args.moves["altFire"] but it think it's for the right click, but i noticed that the key by default for walking is shift so you could use the args.moves["run"] and args.moves["primaryFire"] to check for this alternate fire.
    i don't think that what you are searching for, but if you know in which table to search for, use this to list it's content.
    Code:
    for k,v in pairs(insertTableHere) do world.logInfo(k.." : "..tostring(v)); end
    it will print the content of a table in your log.
     
  9. Caudyr

    Caudyr Black Hole Surfer

    Ah, so you can just look for the "run" arg to see if it's false + see if primaryFire is true then, I imagine.

    If there's no direct check for that, then that's a sufficient workaround. Thanks C0bra5 and LoPhatKao for the help! :D
     
  10. Wheee321

    Wheee321 Subatomic Cosmonaut

    I know it`s an older thread, but It's about the same thing:
    I need a monster to detect if my character is holding a weapon in hand or not, but with
    Code:
    playerItem = world.entityHandItem(-65535, "primary")
    I just get a nil value
    Someone able to help? Is it even possible?
     
  11. LoPhatKao

    LoPhatKao Space Kumquat

    unpak the game assets, check the luas for mobs/npcs to make sure its still a valid function (should be)
    do world.playerQuery around mob so its for sure getting the right player id (might not default to -65535 in sp game anymore.. havent checked in a while)
    if still nil... well beats me :D sorry
     
  12. Wheee321

    Wheee321 Subatomic Cosmonaut

    Still getting a nil value...but thanks anyway
     
  13. srecinto

    srecinto Big Damn Hero

    Here is a safe way:
    -- Search for the player
    playerEntityId = world.playerQuery( mcontroller.position(), 5, { order = "nearest" } )[1]
    -- Get hand item that is in primary slot (values are either in "primary" or "alt")
    playerHandItem = world.entityHandItem(playerEntityId, "primary")

    the bad thing about this, according to the API is "Gets an item descriptor for the item held in the entity's hand" so not a direct reference to the item... I'm looking to see if I can still get a handle on the item
     
    artguk likes this.

Share This Page