How to script like in Unity?

Hi. A newbie from unity is here. I don’t quite understand how I can change something in game world through code.

I did examine a bit Script Canvas. But I always find visual scripting time consuming. So I wonder if there a way to script like in Unity.

For e.g. How can I find an object in script like (GameObject.Find(“Name”); in unity), move it on Update(); (transform.position += Vector3.Forward); and etc.

Using C++ if possible. Lua looks not cool

Yes. I would like to exchange notes. Could you please share with me about these components?

Unfortunately I don’t really remember everything, but I can share with you everything I remember how to do. Here’s a general outline, it was certainly enough for me to figure it out, it should be possible with a bit of hunting:

General Background and Info

Making a component used to be a complicated mess from what I can tell, but now it seems relatively easy. It involves making a gem, then adding components etc. to that gem. As I understand it, gems are basically like libraries. You can include them or not into a project and use them in multiple projects to create your product.

Creating your Project to Use

The first step is, of course, making a project. I’ll include this just in case:

  1. Open up the Lumberyard Project Configurator
  2. Click “Create New”
  3. Select an Empty or Default project*
  4. Enter a name for your new project and click “Create Project”
  5. Wait. A lot. This will take a while
  6. Wait some more
  7. A little more waiting
  8. Make sure you set your new project as the default startup project by selecting it in the Project Configurator and and clicking “Set as Default” in the top right
  9. Open up a command prompt and CD over to your /dev/ folder**
  10. Run the “lmbr_waf build_win_x64_vsVersion_profile -p all --progress” command, replacing vsVersion with your visual studio version. Since I work in 2015, my command would be “lmbr_waf build_win_x64_vs2015_profile -p all --progress”.
  11. Once that is done, you’re good to go
    Making a Gem

Up next, it’s time to go about making your Gem. This is where your component will be made:

  1. In your project configurator, under your current project, click “Enable Gems”
  2. Click “Create a New Gem” and name it. Give it a summary if you’d like, and make sure “Code and Assets” is the option selected
  3. Click “Create Gem” and wait a few seconds while it completes the operation
  4. Scroll down to your newly created Gem or, alternatively, search for it and make sure to tick the box under the “Enabled” column
  5. This is where my knowledge falters slightly. I’m not sure how much rebuilding your project actually needs, but I rebuilt the heck out of it. You’ll need to run three commands:
  6. Open command prompt in your /dev/ directory and run the following command: “lmbr_waf configure”
  7. Open a file browser and navigate to your /dev/solutions/ directory. Check to see what the solution name is. Then, in a command prompt that is in your /dev/ directory, run the following: “lmbr_waf msvs --visual-studio-solution-name=yoursolutionnamehere”. This seems somewhat stupid at first but you need to remove certain things from the name. For example, my current solution name is this: “ComponentTestingProject_vc140.sln”, so my command would be this: “lmbr_waf msvs --visual-studio-solution-name=ComponentTestingProject”
  8. Run the lmbr_waf buiild command that was referenced in step 10 of the “Creating your Project to Use” section
  9. Open Visual Studio, then open the [yourProjectName]_vc[someNumber].sln solution in the /dev/solutions directory. You should now see your Gem under the “Gems” filter in the Solution Explorer
    Making the Component

The moment you’ve all been waiting for. I’ll explain this in steps, using screenshots and other methods to explain this as it is quite complicated for someone who’s just starting out. There’s a lot to it and it can be really hard to understand if you’re like me and decided that LY was the perfect engine to move to directly after about a month of Unity experience. I would like to mention that there are probably WAY more #includes in this than are needed, but as I don’t really remember it all 100%, please excuse this.

  1. Open Visual Studio and open your [projectName]_vc[someNumber].sln solution in the /dev/solutions directory
  2. In the solution explorer, under the “Gems” filter, you should see a project with same name as the Gem you created in the “Making a Gem” section. This is your Gem (go figure). There are many like it, but this one is yours.
  3. Open it up and navigate to the “source” filter.
  4. Click the “source” filter and add a new item to it. Make it a .cpp file with whatever you want the name to be. I named mine the same as my component, although as far as I can tell, this isn’t necessary
  5. Be sure to change the location of where it will be created to /dev/Gems/[gemNameHere]/Code/Source
  6. Repeat step 4, this time creating a .h file
  7. This is where we have to go a little off the rails based on what you want your component to do. I can’t explain how to do exactly what you want as I don’t know what it is and I’m 99% certain that I don’t know how either, but I can explain in the very least, the default component creation… Thingy…
    This is the base code for a Gem:

In your .h File:

	#pragma once
#include <AzCore\Component\Component.h>
#include <AzCore\Component\ComponentBus.h>
#include <AzCore\Component\EntityBus.h>
#include <ComponentTestGem\ComponentTestGemBus.h>
namespace ComponentTestGem
{
class CompTest : public AZ::Component, protected ComponentTestGemRequestBus::Handler
{
public:
~CompTest() override = default;
AZ_COMPONENT(CompTest, "UUID");
void Init() override;
void Activate() override;
void Deactivate() override;
static void Reflect(AZ::ReflectContext*);
};
}

The basics of this explanation can be found here:

Making a Component

Next up, you’ll need to register the component with your Gem. This is explained here:

Registering the Component

Once you’ve completed that, you can start writing it. You do need to reflect the component so that you can serialize it and expose it to the editor:

Reflecting your Component

Just a note on reflection- Although I’m not entirely certain of why this works, if you want to use your component in the editor, such as adding it to an entity, you’ll need these lines:

->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))

You’ll want to add them into the

	edit->Class<CompTest>()
->STUFF GOES HERE
;

lines. This allows the component to be seen by the editor (I think). How? No clue. Does it work? Yep.

Once you’ve completed all of this, you need to add the source files into a few places. Under the “WAF” filter in your Gem project in VS, you’ll find a [gemNameHere].waf_files file. Open it up, and under the source line, add them as entries. For reference, mine looks like this. As you can see, “CompTest” is my component, added into the “Source” section.


At this point, you know more or less everything I do. I hope I could be of some help. Let me know if anything doesn’t work, I think I may have forgotten a few things but I’m not entirely certain. If something does go wrong, I’m more than happy to dig through my browser history and find all the articles I looked at; maybe it will jog my memory.

Hope this helps!

-Sly

*I wasn’t entirely certain of which one of these to go with and decided it would be best not to mess with it until I knew more, so I chose default.

**Considering the differences in versions happening of late and such, I’m not sure the following steps are necessary, but I do them each time so I’m including it. Better safe then sorry unless someone more experienced and knowledgeable about these things is kind enough to correct me

I’m currently trying to learn how to do this. I can’t exactly tell you how to do what you’re looking to do, but I’m happy to share my knowledge if you’d like. I’m brand new to lumberyard, about 2 weeks in and thus far I’ve learned in the very least how to create a component, the lumberyard C++ version of adding a C# script in Unity. It’s a bit more (a lot more) complex than simply clicking “add script” but after doing it once or twice, it’s actually not too bad. Let me know if you’d like to exchange notes and such!

Thanks for such detailed answer. I will try to do this in my spare time. I did see quite similar things on one youtube tutorial. Although I’m not entirely sure how does it help. Meaning that If I made a component, what and where should I write to make changes within the game. Like changing a position of an object.

As far as I can see It’s possible to override Init() function. But I don’t know how to change anything, like position (transform.position += Vector3.Forward; in unity for e.g.).

By the way I’ve been working in Unity for more than 1,5 year.

This is where I’m getting stuck as well. I’m trying to figure out especially how to get component references so that I can use them and change values. So far, I’ve managed to create a component, and that in itself took a few weeks to figure out due to a lack of documentation. I think your best bet will be to look at the C++ API ref for LY. That may hold the answer, but as I said, I’m still working on it. If I make any progress I’ll leave it as a reply to this and let you know. The API can found here.