UE 4 C++ Programming Syntax on Custom Classes

#pragma once
#include “WhateverClassName.generated.h”

        USTRUCT()
        struct FFruit
        {
            GENERATED_BODY()
    
            UPROPERTY(EditAnywhere, Category = Infos)
            int32 taste;
    
            UPROPERTY(EditAnywhere, Category = Infos)
            float weight;
         };

        UCLASS()
            class UWhateverClassName : public UObject
            {
            	GENERATED_BODY()
            
            public:
                    UWhateverClassName();
            
            	UPROPERTY(EditAnywhere, Category = Infos)
            	FFruit Banana;
            };

and your .cpp

#include "YourProject.h"
#include "WhateverClass.h"

UWhateverClassName::UWhateverClassName()
{
   Banana.taste = 10;
   Banana.weight = 30.5;
}

This should be a good start if you want a basic object UE4 compliant

Good Day!

I’m starting new on C++ programming on UE4 but I am no stranger to programming. I’ve had experience coding with Java and Swift before and I know the basics about Object oriented programming.

In Java, we can create classes such as:
Class Fruit {
int taste;
double weight;

}

And we can create objects for these such as:

Class main{

  Fruit banana;
  banana.taste = 10;
  banana.weight = 30.5;

}

Note that the Fruit is a custom object and want to implement such a principle in UE4. I tried doing it this way in C++ syntax inside VS2015 in a UE4 project but it doesnt work/build.
What is the correct way of doing it??

Thank you!

woah.
Thank you so much!
Just a follow up question, if I were to create this in the Unreal Engine 4 editor, I’m gonna have to:

  1. Add C++ Class
  2. Then set it to None
    Is that right?
    And can I reference this class in other C++ classes?
    For example, in MyActor C++ class, can I declare and use the Fruit object inside it?

Thank you!

That depends if you want a class or a struct. But I highly recommend you to read some C++ guides if you’re not familiar with those concepts. This is beyond UE4 and UE4 requires at least a basic understanding of c++

Nevermind, solved it!
So you basically create objects by declaring a USTRUCT in your header files.
Is that right?