Jump to content

Visual Studio Code for Leadwerks

AggrorJorn

1,701 views

I have been using Visual Studio Code for a couple of years now and it is my defacto text editor next to Notepadd++. I mainly use it however to write Lua scripts. 

 

 

Leadwerks extension

Personally I would love to see it if Visual Studio Code would be the default code editor for Leadwerks (either with 4.x or 5.x). So I got started on working on a custom Leadwerks extension.  You can download it here: https://marketplace.visualstudio.com/items?itemName=aggror.Leadwerks

Todo list:

  • Done: Leadwerks extension has a dependency on the Lua extension. So this Lua extension is automatically installed when you install the Leadwerks extension.
  • Done: Snippets. Every time you create a new script in the Leadwerks editor, you get a couple of default scripts like Function Script:Start(), UpdateWorld(), PostRender(context). Using a very simple snippet, these kind of functions can be inserted with ease.
    • prop[type] : Creates Leadwerk editor properties
    • print: Shortcut for System:Print("")
    • lescripts: Inserts all entity script functions (commented)
    • class: Creates a basic class objects with example functions
    • start: Start function of an entity script
    • updateworld: UpdateWorld function of an entity script
    • updatephysics: UpdatesPhysics function of an entity script
    • collision: Collision function of an entity script with all parameters
    • PostRender: PostRender function of an entity script with the context parameter
    • function: Creates a function for either a custom object or for the specific script
    • if
    • for
    • pair
    • ipar
  • For instance: just type 'col' followed by 1 tab and you get: 
  • function Script:Collision(entity0,entity1,position,normal,speed)
    
    end

     

  • Partially done: Supporting intellisense (sort of) with Leadwerks entities.
    • Lua is a typeless language (not a strong typed language), which makes intellisense not really possible. 
    • VS code is smart enough to recognise some functions that are available in your script, but it is not as complete as when you would work with C# or C++.
    • Done: Generate snippets for the entire Leadwerks API. 
      • Snippets are created per object and a second one without the object. For instance
        • Entity:SetPosition()
        • SetPosition()
      • TODO: Classes with parents, would require matching functions. For instance: a pivot is an entity and thus requires Pivot:SetPosition()
    • Done: parameters into placeholder slots.
    • If I can extend the intellisense so that it recognises Leadwerks entities, perhaps we could see correct variables and functions of those entities.
  • TODO: Loading in the API documentation in a split screen. 
    • The API documentation is written in an XML format which I can retrieve in VS code. I can display it in a splitscreen. 
    • I would have to play with the styling a bit but in general this should work really fast
    • API documentation can be cached if in online mode. Documentation could also be periodically fetched. Moving the API documentation to Github would help improve this process. (newer file versions etc)
  • Debugging
    • Josh stated that he is getting someone to make this for him. So fingers crossed. :)
    • The biggest issue at the moment is the lack of debugging support. Visual studio has debugging options of course, but it can't communicate with the Leadwerks editor.
    • If you have an error in your script while running from the editor, the default Lua editor is opened. :(
  • Like 9


11 Comments


Recommended Comments

Including the second API snippet that starts with “Entity:” is interesting because if you ever want to know what functions are available for that class you can just start typing “ent...”

Share this comment


Link to comment

Seeing all the API in VSCode is pretty awesome. It looks like this is your raw input from the docs:

SetRotation(number pitch, number yaw, number roll,  bool global=false)

And you will want to turn that into this:

SetRotation(&pitch, &yaw, &roll, &global)

 

Share this comment


Link to comment
7 hours ago, Josh said:

Seeing all the API in VSCode is pretty awesome. It looks like this is your raw input from the docs:


SetRotation(number pitch, number yaw, number roll,  bool global=false)

And you will want to turn that into this:


SetRotation(&pitch, &yaw, &roll, &global)

 

Yeah those parameters need some work. What do you think looks better? Your post or the thing below?

SetRotation($number_pitch, $number_yaw, $number_roll, $bool_global=false)
SetRotation($vec3_rotation, $bool_global=false)

On one hand I don't want to lose the type, but on the other hand it looks uglier. Both are just easily made so its just a matter of picking a style.

Share this comment


Link to comment

Maybe something like this would work:
 

	"Entity:SetPosition": {
		"prefix": "SetPosition",
		"body": "SetPosition($x,$y,$z,$global)",
		"description": "Entity Method\nSets the position of an entity in 3-dimensional space, using local or global coordinates.\nParameters\nx (number): X component of the position to set.\ny (number): Y component of the position to set.\nz (number): Z component of the position to set.\nglobal (boolean): uses global space if set to true, otherwise local space is used."
	},

 

Share this comment


Link to comment

The popup window can be restyled I think. I haven't tried it yet, but it is basically css so perhaps it is fairly easy to achieve a larger width.

Adding the parameter information to the description could work. This way I can also add 'cleaner' parameter names. As long as the documentation holds up to its current parameter convention, it can be easily transformed. 

type name: "number x" or "bool ignoreCollision"
type name=value: "bool global=true" or "number velocity = 1.0"

Fyi: at https://www.leadwerks.com/learn?page=API-Reference_Object_Entity_SetPosition the first syntax is missing a comma.

  • SetPosition(number x number y, number z, bool global = false)

Share this comment


Link to comment

Small update published: I have added the parameters as indexes as well. The syntax is converted from

SetRotation(number pitch, number yaw, number roll,  bool global=false)

to

SetRotation(pitch, yaw, roll, global)

The description contains the explanation of the parameters. see image below. It is not waterproof though. Sometimes the syntax parameters are missing in the description, causing the description to be empty or imcomplete. You can identify mistakes/incomplete information in the documentation easier this way.

 The biggest downside at the moment is the way Vs code scales the intellisense window. Right now that is not optimal. However there is word that they are working on that by making the styling more accessible through settings. In the image below I applied custom styling to make the width 2 x its original size.

AddForce command with 2 different versions of paramters.

image.png.fc0af2e1a73ad616c5cc8e4ae83cc962.png

image.png.5b158b75c54b8bbb534054c0ae96d8d5.png

 

  • Like 1

Share this comment


Link to comment

Why does it have ".," at the end of each line? Why does it say (Leadwerks) on the last line? :D

Share this comment


Link to comment

Because the description was written in array form I need to use comma's to specify a new line. The extension somehow included that comma. I can solve this by just cramming everything on 1 line. It is not that people would read that document anyway. Why the ' Leadwerks' is inserted is not clear to me yet. It is not comming from the code and does also not appear in the snippet file. Vs code is adding it for some reason.

Pushed a new build that fixes the comma.

Share this comment


Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Blog Entries

    • By Josh in Josh's Dev Blog 8
      An often-requested feature for terrain building commands in Leadwerks 5 is being implemented. Here is my script to create a terrain. This creates a 256 x 256 terrain with one terrain point every meter, and a maximum height of +/- 50 meters:
      --Create terrain local terrain = CreateTerrain(world,256,256) terrain:SetScale(256,100,256) Here is what it looks like:

      A single material layer is then added to the terrain.
      --Add a material layer local mtl = LoadMaterial("Materials/Dirt/dirt01.mat") local layerID = terrain:AddLayer(mtl) We don't have to do anything else to make the material appear because by default the entire terrain is set to use the first layer, if a material is available there:

      Next we will raise a few terrain points.
      --Modify terrain height for x=-5,5 do for y=-5,5 do h = (1 - (math.sqrt(x*x + y*y)) / 5) * 20 terrain:SetElevation(127 + x, 127 + y, h) end end And then we will update the normals for that whole section, all at once. Notice that we specify a larger grid for the normals update, because the terrain points next to the ones we modified will have their normals affected by the change in height of the neighboring pixel.
      --Update normals of modified and neighboring points terrain:UpdateNormals(127 - 6, 127 - 6, 13, 13) Now we have a small hill.

      Next let's add another layer and apply it to terrain points that are on the side of the hill we just created:
      --Add another layer mtl = LoadMaterial("Materials/Rough-rockface1.json") rockLayerID = terrain:AddLayer(mtl) --Apply layer to sides of hill for x=-5,5 do for y=-5,5 do slope = terrain:GetSlope(127 + x, 127 + y) alpha = math.min(slope / 15, 1.0) terrain:SetMaterial(rockLayerID, 127 + x, 127 + y, alpha) end end We could improve the appearance by giving it a more gradual change in the rock layer alpha, but it's okay for now.

      This gives you an idea of the basic terrain building API in Leadwerks 5, and it will serve as the foundation for more advanced terrain features. This will be included in the next beta.
    • By Josh in Josh's Dev Blog 1
      Documentation in Leadwerks 5 will start in the header files, where functions descriptions are being added directly like this:
      /// <summary> /// Sets the height of one terrain point. /// </summary> /// <param name="x">Horizontal position of the point to modify.</param> /// <param name="y">Vertical position of the point to modify.</param> /// <param name="height">Height to set, in the range -1.0 to +1.0.</param> virtual void SetHeight(const int x, const int y, const float height); This will make function descriptions appear automatically in Visual Studio, to help you write code faster and more easily:

      Visual Studio can also generate an XML file containing all of the project's function descriptions as part of the build process. The generated XML file will serve as the basis for the online documentation and Visual Studio Code extension for Lua. This is how I see it working:

      I am also moving all things private to private members. I found a cool trick that allows me to create read-only members. In the example below, you can access the "position" member to get an entity's local position, but you cannot modify it without using the SetPosition() method. This is important because modifying values often involves updating lots of things in the engine under the hood and syncing data with other threads. This also means that any method Visual Studio displays as you are typing is okay to use, and there won't be any undocumented / use-at-your-own risk types of commands like we had in Leadwerks 4.
      class Entity { private: Vec3 m_position; public: const Vec3& position; }; Entity::Entity() : position(m_position) {} It is even possible to make constructors private so that the programmer has to use the correct CreateTerrain() or whatever command, instead of trying to construct a new instance of the class, with unpredictable results. Interestingly, the constructor itself has to be added as a friend function for this to work.
      class Terrein { private: Terrain(); public: friend shared_ptr<World> CreateTerrain(shared_ptr<World>, int, int, int) }; The only difference is that inside the CreateTerrain function I have to do this:
      auto terrain = shared_ptr<Terrain>(new Terrain); instead of this, because make_shared() doesn't have access to the Terrain constructor. (If it did, you would be able to create a shared pointer to a new terrain, so we don't want that!)
      auto terrain = make_shared<Terrain>(); I have big expectations for Leadwerks 5, so it makes sense to pay a lot of attention to the coding experience you will have while using this. I hope you like it!
    • By Josh in Josh's Dev Blog 0
      A new update is available for beta testers.
      Terrain
      The terrain building API is now available and you can begin working with it, This allows you to construct and modify terrains in pure code. Terrain supports up to 256 materials, each with its own albedo, normal, and displacement maps. Collision and raycasting are currently not supported.
      Fast C++ Builds
      Precompiled headers have been integrated into the example project. The Debug build will compile in about 20 seconds the first run, and compile in just 2-3 seconds thereafter. An example class is included which shows how to add files to your game project for optimum compile times. Even if you edit one of your header files, your game will still compile in just a few seconds in debug mode! Integrating precompiled headers into the engine actually brought the size of the static libraries down significantly, so the download is only about 350 MB now.
      Enums Everywhere
      Integer arguments have been replaced with enum values for window styles, entity bounds, and load flags. This is nice because the C++ compiler has some error checking so you don't do something like this:
      LoadTexture("grass.dds", WINDOW_FULLSCREEN); Operators have been added to allow combining enum values as bitwise flags.
      A new LOAD_DUMP_INFO LoadFlags value has been added which will print out information about loaded files (I need this to debug the GLTF loader!).
      Early Spring Cleaning
      Almost all the pre-processor macros have been removed from the Visual Studio project, with just a couple ones left. Overall the headers and project structure have been massively cleaned up.
×
×
  • Create New...