How to lua?

Discussion in 'Rust Development' started by Prouxx, Dec 11, 2014.

  1. Hi guys,

    I've been running Rust servers since about January time and have spent a lot of time looking at various plugins and trying to figure out what makes them tick. I'd still say I'm very much a noob when it comes to coding, but I think it's time to take the next step. Basically, I'd really like to learn to write my own plugins. I figured the best way to start doing this would be to play with existing plugins and see if I can learn enough from that. The problem I have is that every time I try to do something like this, I end up with errors that I can't trace. For example, yesterday I was trying to add some new features to the teleportation plugin and got errors like this:
    Code:
    8:28 PM [Error] m-Teleportation: A .NET exception occured in user-code
      at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in <filename unknown>:0 
      at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in <filename unknown>:0 
      at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
      at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
    I have no idea what was causing this and it's kind of annoying because I really want to be able to do this stuff. So, aside from looking at other people's plugins, how would you guys recommend that I go about trying to learn how to write them? I feel that I'm lacking some fundamental knowledge about coding this sort of thing :D

    Thanks :D
     
  2. Yes i started having my servers on fébruary and oxide at that moment. I've learned everything by looking at codes.
    Oxide 1 was very easy to start with as the error would always tell you where to look at.
    On oxide 2, it's not the case, so when you have errors you Will have to read everything you did before finding it.
    What î recommand would be to go Stép by step, look at plugin codes, take from it what you need, see how they work, and write then test your code step by step to see if errors pops up.
    We are always welcoming new coders and Will help you if you post your errors / codes on the forum.
    If you need help with lua part, just Google it, you Will always get your answers ^^. If you need help with the rust part, Well you can ask :)
     
  3. Super, prepare yourself to be inundated with ridiculous posts over the next few weeks ;) Thanks though, it's nice to see that you guys are so willing to help :D
     
  4. Its easier to learn by starting your own plugin from scratch than trying to edit another plugin because if you do it all yourself you always know what you've done and what its intended to do.
    If you want to learn by editing the teleportation plugin, take a function from it, try to understand what it does and try to replicate it with your own code. This way you learn how everything is working and what each part of the code exactly does.
    I always found that way to be the best to learn something new.
     
  5. I've just recently gotten into it myself. I'm not new to programming, but I've never really worked with Lua or C Sharp. I'm a Java guy myself. One thing that really helps is to download a decompiler (I use ILSpy, free and open-source) and decompile the Assembly-CSharp.dll and Facepunch.dll. These contain all the hooks and all the classes that are referenced by the plugins. Plugins are automatically called when they contain a function that is a hook in Oxide. Plugins can also call methods contained in these classes on their own, either in a hook function or even through chat commands etc. I started out just debugging issues with plugins that stopped functioning on the dev versions (Missing variables in the new DLL's, etc.). I've actually started adding my own hooks to Oxide with the patcher and writing a plugin to use those. Its a lot of fun once you get the idea of whats going on and it get easier as you go. The decompiler also helps when manually patching Oxide. You can use the old hooks and indexes, then decompile the dll's to make sure that the hooks are still in the proper place. If not, adjust the index in the patcher to get the hook back where it needs to be by comparing the old Assembly-CSharp and Facepunch dll's contained in the most recent Oxide version with the one you just patched. A lot of crap that you probably didn't need to know right off, but that's how I got started and I hope at least some of this helps ya. :) Last thing I would recommend too is to download the Oxide 2 Patcher from github. This will show you all the current hook names and you can take a closer look at how all that stuff works.
     
  6. Thanks for your advice guys. I've started working on a new factions plugin and, honestly, my mind is a bit boggled. I'm new to programming so I kind of expected this. What I'm trying to do is make it so that an admin can create a faction and then players can join it. After that's done I'll take a look at adding various perks for factions, making it fully configurable etc.

    Right now, what I'm struggling with is making the tables for the plugin. I can't quite seem to get my head around making a table that creates a group name and then the group name holds PlayerIDs. I don't know, it's hard to explain why I'm struggling, but I'll post what I've done so far (don't laugh ;) ) and hopefully, if you're feeling kind, you might be able to help me out a bit.

    I'm not sure if I should have just requested the plugin, but it feels like actually trying to make it is a much better thing to do. Correct me if I'm wrong though :D
     

    Attached Files:

  7. rust.UserIDFromPlayer
    not rustUserIDFromPlayer ;)
     
  8. Yeah, I'm trying to learn as well, and am hitting a wall as I don't know where to look for all available API calls... parameters etc....

    eg.: player - what methods can I call on it like player.inventory, what parameters does it take?

    Where can I find info on all that info ?

    Thanks,
    TheDoc
     
  9. Download http://oxidemod.org/downloads/oxide-2-for-rust-experimental.714/
    Get a .Net decompiler as there are a bunch of free ones out there. I'd suggest http://www.telerik.com/products/decompiler.aspx

    Open up the Assembly-CSharp.dll file in the decompiler and browse around to find the methods used in rust.
    I've attached the BasePlayer.cs file so you can get an idea of the available fields and methods.
     

    Attached Files:

  10. Thank you, I will look into that.
     
    Last edited by a moderator: May 17, 2015