Modding Help Spawn item on hit - Is it feasible?

Discussion in 'Starbound Modding' started by Worldcrafter, Feb 22, 2014.

  1. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    Hello! Overly-ambitious and still quite noobish modder here. While working on a mod idea, my progress was halted when attempting to implement a weapon that spawns an item on hit. This led me to go hiking through the assets in search for a possible resolution; sadly, I have not come across one.

    I have come across several other commands to spawn things, such as liquids or materials upon reaping a projectile, but reaping is not my goal; I'm hoping to produce the item only if the weapon successfully strikes something that takes damage. I have tried to mesh other forms of item production with my swoosh projectile file, waltzed with various formats, and found several JSON-acceptable command strings that do nothing to alter how the weapon works.

    My hunt for the appropriate command lines took me into the world.lua, where I saw the spawnItem command. That, in itself, launched several failed attempts to incorporate it as part of the JSON-based projectile. Making no progress on successfully coercing the lua coding into JSON, I decided to peruse the inter-mo-nets (that's the technical term, you know) in order to try and alleviate my ignorance.

    This search was largely bereft, save for a few older posts with earlier versions of the game (I think the posts referred to v. Indignant Koala). As I'm still a fledgeling with JSON, I don't quite feel ready to do battle with lua; however, my ambitions drive me to at least try. Before I take the plunge, however, I want to find out if it is even feasible at this point in time of the game, or if I have spent my entire day chasing a unicorn.

    So, to those who are more knowledgeable and better experienced, I humbly beseech you to answer my question - can I potentially program a weapon to spawn an item on hit with the current Starbound version? Whether it be JSON or lua, if it is possible, I shall endeavor to do so. And if it is not, I shall put the item on the back shelf and continue my project without it.

    Thank you kindly for taking the time to read my post.
     
  2. Spawning an item is not the hard part... its spawning an item on hit.

    Its not really clear how you want to go about this... as I dont understand if you mean the player getting hit spawning an item, or the player hitting an enemy spawning an item.

    There is no command that I know of that runs when you are hit, and thats going to be the biggest roadblock for you right now. I saw some armor that implemented "Reflecting damage" but I dont remember the mod... Had something to do with tiered or augmented armor or something... You could look and see how they implemented the reflecting feature and mimic that to spawn items instead.
     
  3. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    First and foremost, thank you for taking the time to reply. My sincerest apologies for the confusion. What I meant to convey was that the item spawns when the player hits an enemy with the weapon.

    On another note, that "reflecting damage" you mention does sound quite intriguing; I may have to gander at it sometime just to see how it works.
     
  4. The | Suit

    The | Suit Agent S. Forum Moderator

    Are you talking about something similar to a bow?
    When an arrow kills something it drops meat?

    Or are you saying everytime you hit something with a projectile an item spawns?
     
    Last edited: Feb 22, 2014
  5. Daimoth

    Daimoth Scruffy Nerf-Herder

    I would like to know this, too. Can weapons have .lua's?

    I ask because the current interplay between treasurepools and monstertypes is either chewed up or none of us understand it fully, and I'm looking for a way to get around that.
     
  6. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    Thank you kindly for the reply, and I apologize once more for my lack of clarity. I am attempting to set it up so that the weapon's projectile spawns an item on every successful hit, regardless if it kills the enemy or not. I do not want the item to spawn on swinging at empty air, and I do not intend to have it affect the drop pools at this point in time.
     
  7. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    To update my situation, I am still unable to manipulate the coding to do as I desire. I have continued to explore the existing files to see what I can glean from their information, which led me to cracking open the capturepod.lua file. From a non-modding point of view, it seemed the best candidate for an on-hit effect. Delving into the file, however, I discovered that all the coding is monster-side, and is all the monster's response to getting hit, instead of the effects of the item itself.

    Noticing the mention of damage amid the various code, I dove into the various API files and rummaged around; I was hoping to find some sort of command to track damage being dealt, either numerical or a damage type. To my great disappointment, I discovered that the damage recognition commands are if the entity using the script is on the receiving end.

    After pouring over some of the lua info, I noticed that a script can call upon portions of other scripts. As I am woefully uneducated in the ways of coding and lua, I have a new question to pose to those willing to answer - is it possible to make a script for a projectile so that it takes part of an enemy's script for damage detection and compare the source ID?

    For example, a sword swoosh projectile that, on spawn, it checks local monster IDs. If it finds any, it checks whatever script portion that determines if a particular monster has been hit. It then compares the damage source ID; if the damage's source ID matches the swoosh's source ID, the swoosh performs whatever's next in its script, otherwise it ends. Is this example at all possible at this point in time?

    Thank you kindly for taking the time to read this post.
     
  8. The | Suit

    The | Suit Agent S. Forum Moderator

    You can find most of Starbound LUA functions here
    https://gist.github.com/jordoh/7864154

    But if you don't know LUA there is no point in starting there, so you might as well start here first
    http://community.playstarbound.com/index.php?threads/learning-how-to-write-script-lua.65473/

    Code:
    --- Called after the NPC has taken damage
    -- @param args Table (map) of info about the damage, structured as:
    --            {
    --              sourceId = <entity id of entity that caused the damage>,
    --              damage = <numeric amount of damage that was taken>,
    --              sourceDamage = <numeric amount of damage that was originally dealt>,
    --              sourceKind = <string kind of damage being applied, as defined in "damageKind" value in a *.projectile config>
    --            }
    --        Note that "sourceDamage" can be higher than "damage" if - for
    --        instance - some damage was blocked by a shield.
    function damage(args) end
     
  9. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    Thank you kindly for your reply. I am already familiar with the lua function list, and have spent some time pouring over it. I am also aware of the damage function, and have been studying it and its equivalent for monsters. Can that function work in a script for a projectile? Please forgive my ignorance, but from the way it reads, it seemed limited to the NPC the script is for. That was why I was wondering if I could couple that function with the callScripted Entity command.

    Code:
     --- Calls a lua function in the given entity's lua context
    --
    -- @param entityId The (int) entity id of the entity to call the function on.
    -- @param name The name of the function to call. This does not need to be a
    -- global function - i.e. "entity.say" would call the "say" function
    -- on the "entity" table (as defined in the target entity's lua
    -- context).
    -- @param ... (optional) Arguments passed to function
    --
    -- @returns The result of calling the function, or nil if the entity does not
    -- exist or does not support lua calls.
    callScriptedEntity = function(entityId, name, ...) end,
    Additionally, you have my sincerest thanks and deepest gratitude for the links to the tutorials.
     
  10. The | Suit

    The | Suit Agent S. Forum Moderator

    The projectile it self as far as I am aware is not scrip table.
    Only Entities - which consist of objects and creatures \ npc \

    Which is why you saw the capture pod - not have any script
    Also the reason why not all monsters are capturable
     
  11. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    Alas, I feared that might be the case. My shred of hope was with the Enraged Koala notes, where the engine update allows scriptable objects. I largely suspected it to be just objects, but I had noticed they tend to be a little loose with the term in the LUA commands.

    Sadly, it looks as if the only way to add this particular item idea is to make a stand alone LUA script to react to its unique damage type, then make a merge command for each monster and NPC config. A bit heft, and has no effect on other mods. I think I shall drop the idea for now, and eagerly await the time they decided to expand scripting possibilities.

    I humbly thank thank you for aiding me in this endeavor, and moreso for putting up with the frustration of dealing with an inexperienced modder.
     
  12. Wurmheart

    Wurmheart Subatomic Cosmonaut

    What kind of item spawn are you going for?
    You might be able to get a work around, though it'll probably still require lua editing.
    actionreap command "spawnmonster" for example could bring you awfully close (check the penguinufo code), if the area where the projectile hits cannot support the monster it will instantly die and leave itemdrops behind. penguinminiufo especially seems to die a lot, though you would have to turn them non hostile.
    You might even be able to edit the monsters to just have a very short lifespan anyway, and assigning items shouldn't be very hard either.
     
    The | Suit likes this.
  13. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    Thank you kindly for your inquiry and suggestion. I was trying to make a drain/vampiric as one of the weapon elements for this mod I am working on. I wasn't certain if there was a way to have the projectile to affect the user in one way, and the target another. So, I figured spawning a small healing item on hit would be a good work around - you strike an opponent, a small healing item appears, and subsequently gets absorbed. I also want to try and avoid affecting the monster's drop pool, so you could still get normal monster items from them.
     
  14. persy

    persy Master Chief

    I see you decided not to mess with onDamage() hook but I would like to point out that it is actually a better way to do vampiric weapons, than dropping an healing item on hit. This function provides you an id of damage source entity, thus you can directly entity.heal() (or whatever) the attacker. You can also calculate the amoung of healing on damaged inflicted etc. And the most important, such approach will allow your vampiric weapons work properly in NPC's hands, should it occur :p
    Actually vampiric weapons, should they ever be implemented, do require a specific damage type.
     
    severedskullz and The | Suit like this.
  15. Your way of going about this is spot on. Had Worldcrafter had stated that he was going for a vampiric weapon from the start, rather than asking how to spawn items, I could have given a suggestion like this a while ago.
     
    Daimoth and The | Suit like this.
  16. Daimoth

    Daimoth Scruffy Nerf-Herder

    Actually I'd still like to know how to spawn items. Could I spawn a monster and kill it immediately with its own script? I'd like to get away with not using a mob at all, the way treasurepools and monster classes interact is extremely counterintuitive and, I suspect, bug ridden.
     
    Last edited: Feb 24, 2014
  17. Or you could just use the spawnitem command in a projectile or what not
     
    Daimoth likes this.
  18. Daimoth

    Daimoth Scruffy Nerf-Herder

    It's worth a try.
     
  19. Worldcrafter

    Worldcrafter Subatomic Cosmonaut

    I do apologize for any confusion my inquiries had led to. I am still quite inexperienced with any kind of programming. Please keep in mind that, due to my inexperience, I was working almost solely with JSON - and even that is fairly shaky. I didn't see any way to have a projectile to have any effect on its user, let alone different effects between user and target. That is why I turned to an item alternative. And I was aware that LUA had the potential to spawn items with some effects, but not if it could do it on hit like I wanted. That is why I was asking if it was possible.

    I would also like to dispel the misconception that I had written off the on-damage command when I thanked Wurmheart for the suggestion. I had not, but was genuinely thanking another individual for their suggestion and giving me another decision to weigh. Rest assured, it was not a deciding factor one way or the other.

    severedskulls, I have been striving to find the proper command structure in order to get a projectile to use the spawn item, to no avail. I have tried "spawnitem", "item", I have tried replacing item with various specific item types, I have tried using these lines as stand alone commands, I've tried to coupled it with the "action" command, tied it in with "actiononreap", and even going so far as to try to use the output command with recipes. I've looked at other commands that work, such as spawning liquids or placing materials, and tried to figure out how their formating worked and based guesses off of them. I've compared the commands that do function to the world.lua command lists. I have become vastly intimate with the knowledge of acceptable, but non-functioning JSON code lines, but I am woefully unaware of the proper structure that Starbound wants me to use. If you know what it is, I humbly beseech you to reveal it.
     
  20. This is off the top of my head. I havent done something like this in a WHILE so this may be incorrect:
    Code:
     "actionOnReap" : [
                  {
                    "action" : "spawnitem",
                    "item" : "Youritemlol",
                    "count": 1
                  }
    ]
     

Share This Page