Custom tech.. Spawn monsters, items etc in multiplayer

Discussion in 'Starbound Modding' started by blahblahwhatever, Mar 7, 2014.

Thread Status:
Not open for further replies.
  1. blahblahwhatever

    blahblahwhatever Intergalactic Tourist

    If you go here https://gist.github.com/jordoh/7864154 you can see a (partial) list of functions that are available to us through LUA scripting. You can do things like spawnMonsters, spawnItems and much more all from within the game. It's all very interesting.. but how can you actually call these functions and have them work serverside when in multiplayer? If that interests you then keep reading..

    We'll begin by creating a new mod (set it all up, I presume you know how) and then copy over the entire "/tech/" folder from your unpacked assets folder. Now find and open up the freshly copied over "blink.lua" in your text editor.

    Within the "blink.lua" code locate this:

    Code:
    function input(args)
      if args.moves["special"] == 1 then
        return "blink"
      end
    
      return nil
    end
    ..And change it to this:

    Code:
    function input(args)
      local currentPosition = tech.position() -- Gets your chars position that we can use later
      if args.moves["special"] == 1 then
        return "blink"
      end
      if args.moves["special"] == 2 then
        -- your code here
      end
      if args.moves["special"] == 3 then
        -- more of your code here
      end
      return nil
    end
    Now, special 1,2 and 3 all correspond to particular keybindings from within the game, 1 is "F", 2 is "G" and 3 is "H" by default. So literally any time you press one of these letters AND you have the tech active (blink in this case) your custom code will run in the game. You can of course change all of these keybindings if you want by locating and then editing the "starbound.config" file which resides within the main starbound directory.

    The reason I chose for you to edit the blink.lua file first is because it has no secondary or tertiary specials so you won't be messing anything up it's just completely additonal however I suspect that you can just create a brand new custom tech and apply this same principle, I'd expect it to work although I haven't personally tried it.

    Okay well. That is it. Now you know how to call functions from within the game. All that is left is to show you some examples. These are very simple examples, you can expand upon them and come up with even more, this really is only a tiny proportion of what you can do and there are many functions and options I'm still yet to discover..

    Spawn a monster and apply some options (find more monster options within .monstertype files)
    Code:
    world.spawnMonster("glitchknight", { currentPosition[1] - 15, currentPosition[2] + 10 }, { level = 1, scale = 2 } )
    Spawn random guns / swords
    Code:
    world.spawnItem("generatedgun", { currentPosition[1] - 10, currentPosition[2] + 10 }, 1, { level = 5} )
    world.spawnItem("generatedsword", { currentPosition[1] - 10, currentPosition[2] + 10 }, 1, { level = 5} )
    Rain pixels
    Code:
    world.spawnItem("money", { currentPosition[1] - 5, currentPosition[2] + 10 }, 12345)
    world.spawnItem("money", { currentPosition[1] + 0, currentPosition[2] + 10 }, 12345)
    world.spawnItem("money", { currentPosition[1] + 5, currentPosition[2] + 10 }, 12345)
    Place a single block somewhere (whats cool about this is that you could potentially draw stuff by placing many of them in a coherent pattern using different materials and what not)
    Code:
    world.placeMaterial({ currentPosition[1], currentPosition[2] + 10 }, "foreground", "crystalblock")
    world.placeMaterial({ currentPosition[1], currentPosition[2] + 11 }, "background", "rainbowwoodblock")
    Warp from your current position or to any absolute position
    Code:
    tech.setPosition({ currentPosition[1], currentPosition[2] + 3 })
    tech.setPosition({ 123, 123 })
    Warp in the direction you are walking (press up/down/left/right and then tap F, G or H depending upon where you put the code)
    Code:
    if args.moves["up"] then
      tech.setPosition({ currentPosition[1], currentPosition[2] + 3 })
    elseif args.moves["down"] then
      tech.setPosition({ currentPosition[1], currentPosition[2] - 3 })
    elseif args.moves["left"] then
      tech.setPosition({ currentPosition[1] - 3, currentPosition[2] })
    elseif args.moves["right"] then
      tech.setPosition({ currentPosition[1] + 3, currentPosition[2] })
    end
    Spawn an NPC
    Code:
    world.spawnNpc({ currentPosition[1], currentPosition[2] + 10 }, "human", "villager", 1, 0, {})
    Find the closest npc to you, make it talk and then jump (find more npc functions in the scripts/api/npchooks.lua file)
    Code:
    local npcId = world.npcQuery(tech.position(), 100)
    world.callScriptedEntity(npcId[1], "entity.say", "Test 123.. Jump!")
    world.callScriptedEntity(npcId[1], "entity.jump", "")
    As a final example this is what you could put within blink.lua:

    Code:
    function input(args)
      local currentPosition = tech.position()
      if args.moves["special"] == 1 then
        return "blink"
      end
      if args.moves["special"] == 2 then
        world.spawnItem("diamonddrill", { currentPosition[1] - 10, currentPosition[2] + 10 }, 1)
      end
      if args.moves["special"] == 3 then
        world.spawnNpc({ currentPosition[1], currentPosition[2] + 10 }, "floran", "bandit", 6, 0, { damageTeam = 2 })
      end
      return nil
    end
    Note: If you find that it's not working remember that well administered servers can and do disable users from calling some of these functions on their home planet so try them off world or locally first.
     
    Norway174 and danks_ like this.
  2. lornlynx

    lornlynx Cosmic Narwhal

    Hmm, does that mean that tech is ever connected with player? So when you get the position from tech it is the same position as from player, and that moving one also moves the other?
     
  3. blahblahwhatever

    blahblahwhatever Intergalactic Tourist

    Yes AFAIK that's correct. I've tested blink, booster, skyrail and morphball. All of their tech positions are exactly equal to the players position. However tech is different to player, if you run the code
    Code:
    tech.setVisible(false)
    it will only make the worn tech item (if applicable) and any of the effects dissapear, it won't effect the player.
     
  4. SpectralGhost

    SpectralGhost Master Chief

    How would one go about applying status effects to monsters spawned? I'm trying to make mobs that deal certain damagetypes when they spawn, but it isn't working.
    Code:
      if args.moves["special"] == 3 then
        world.spawnMonster("smallbiped", { currentPosition[1] - 15, currentPosition[2] + 10 }, { level = 1, scale = 1 }, {statusEffects = "testburning" } )
      end
     
  5. Lucifer_§oul

    Lucifer_§oul Starship Captain

    could a file be uploaded like it all there ready to go?
     
  6. LittleR

    LittleR Void-Bound Voyager

    hehe!
     
  7. renojonathanr

    renojonathanr Scruffy Nerf-Herder

    Ahem. One does not simply put all kinds of crap in input(). If you want to have cleaner code, try something like this:

    Code:
    function input(args)
      -- You do NOT need to store the current position in an unnecessary variable. tech.position() checks for your position in the world at the exact moment it's called.
      if args.moves["special"] == 1 then
        return "blink"
      end
      if args.moves["special"] == 2 then
        return "spawnDiamondDrill"
      end
      if args.moves["special"] == 3 then
        return "spawnNpc"
      end
      -- nil is automatically returned in any Lua function without return lines at the end
    end
    
    And there's your input() function.

    Now for update(). Put your actual code after blink's code in the function, like this:

    Code:
    function update(args)
      if args.actions["blink"] then
        -- run whatever code runs when you blink
      end
      if args.actions["spawnDiamondDrill"] then
        world.spawnItem("diamonddrill", { currentPosition[1] - 10, currentPosition[2] + 10 }, 1)
      end
      if args.actions["spawnNpc"] then
        world.spawnNpc({ currentPosition[1], currentPosition[2] + 10 }, "floran", "bandit", 6, 0, { damageTeam = 2 })
      end
    end
    
    And there you have it!
     
  8. LittleR

    LittleR Void-Bound Voyager

    what do i use for the lua coding?

    plz reply

    ty
     
  9. AstralGhost

    AstralGhost Pangalactic Porcupine

    You mean like... What do you open the files with? Any old text editing program works just fine. Notepad++ works well for most. It is an advanced text editor. It can color-code and stuff like that.
    You could also find a LUA-specific editor, if you want to. It might help with some LUA-specific functions/utilities that a text editor could never do.

    However, you don't need any specific tools to write LUA code. You can just as easily write them in a regular Notepad document or anything else.

    It's actually good habit to do this anyway.
    There are two reasons:
    1. Better transitioning into other programming languages that do not automatically return values for you.
    2. You don't always want to return nil on an end-case, so never using it may lead you to forget it when you actually want it.
     
  10. LittleR

    LittleR Void-Bound Voyager

  11. LittleR

    LittleR Void-Bound Voyager

    how do we set up a mod?[DOUBLEPOST=1420692825][/DOUBLEPOST]are there any non-download alternatives to notepad++ that work like notepad++
     
    Last edited: Jan 8, 2015
  12. The | Suit

    The | Suit Agent S. Forum Moderator

    Like online notepads?
    I don't see why you would - you would have open the file in windows notepad and then copy it to an online version.
    You could use kl1p <code> ( right side option )
    http://kl1p.com/

    Just keep in mind Notepad++ can search all the files for text matches that online alternatives will not have.

    Notepad ++ has a non installer version if that's your issue - just download the zip package instead
    http://notepad-plus-plus.org/download/v6.7.3.html
     
  13. ThirdEyeGuercio

    ThirdEyeGuercio Void-Bound Voyager

  14. joy410

    joy410 Big Damn Hero

    Not helpful at all.
     
  15. The | Suit

    The | Suit Agent S. Forum Moderator

    Since parts of the tutorial are out of date, and the author has been MIA for almost a year
    Locking
    ==
    If the Tutorial Author wishes to update the tutorial. Feel free to message any moderator to unlock it for you.
     
Thread Status:
Not open for further replies.

Share This Page