Announcement

Collapse
No announcement yet.

Small problem with struct

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Small problem with struct

    Hi there!
    I'm having trouble compiling a simple character I wanted with a prebuilt set of variables within a struct I could access to.
    This is my code:
    Code:
    #pragma once
    
    #include "GameFramework/Character.h"
    #include "CustomCharacter.generated.h"
    
    UCLASS()
    class ACustomCharacter : public ACharacter
    {
    	GENERATED_UCLASS_BODY()
    
    public:
    	
    	USTRUCT(Blueprintable)
    	struct FMyCommand
    	{
    		GENERATED_USTRUCT_BODY()
    
    		UPROPERTY()
    			int32 ID;
    
    		UPROPERTY()
    			int32 BaseDamage;
    
    		UPROPERTY()
    			float Playrate;
    	};
    
    	struct FMyCommand NCommand;
    };
    The compiler keeps saying "FMyCommand isn't a class name or namespace" and "Identifier not declared". What am I doing wrong? Am I supposed to name my struct in a very specific way or something?

    #2
    Structs are like Classes, in fact they work very similar, both are skeletons which can contains properties and functions. There for class and struct declaration can't be contained as property of another, so place your struct outside any class and in the class make a property using struct as a type of your object Best would be if you declere your struct in header file
    Last edited by Shadowriver; 04-02-2014, 07:14 PM.
    =========
    My Tutorials:
    Basic knowledge about Classes and UObject environment and stuff like that

    Comment


      #3
      Originally posted by Shadowriver View Post
      Structs are like Classes, in fact they work very similar, both are skeletons which can contains properties and functions. There for class and struct declaration can't be contained as property of another, so place your struct outside any class and in the class make a property using struct as a type of your object Best would be if you declere your struct in header file
      I tried that as you see here:
      Code:
      #pragma once
      
      #include "GameFramework/Character.h"
      #include "CustomCharacter.generated.h"
      
      USTRUCT(Blueprintable)
      struct FMyCommand
      {
      	GENERATED_USTRUCT_BODY()
      
      	UPROPERTY()
      	int32 ID;
      
      	UPROPERTY()
      		int32 BaseDamage;
      
      	UPROPERTY()
      		float Playrate;
      };
      
      UCLASS()
      class ACustomCharacter : public ACharacter
      {
      	GENERATED_UCLASS_BODY()
      private:
      
      public:
      	struct FMyCommand NCommand;
      };
      However, this doesn't seem to compile either.
      Struct outside the class definition and then I use FMyCommand as a type.

      Comment


        #4
        Ohhhh i missed that, change

        struct FMyCommand NCommand;

        to

        FMyCommand NCommand;

        as you already declared struct and making property of type of struct, compiler things you trying to declere struct again
        =========
        My Tutorials:
        Basic knowledge about Classes and UObject environment and stuff like that

        Comment


          #5
          Originally posted by Shadowriver View Post
          Ohhhh i missed that, change

          struct FMyCommand NCommand;

          to

          FMyCommand NCommand;

          as you already declared struct and making property of type of struct, compiler things you trying to declere struct again
          That won't help... you can define a struct variable starting with the keyword struct. This is actually how you have to declare them in C (which is why you find a lot of typedefs for structs in old code).

          Comment


            #6
            Originally posted by Shadowriver View Post
            Ohhhh i missed that, change

            struct FMyCommand NCommand;

            to

            FMyCommand NCommand;

            as you already declared struct and making property of type of struct, compiler things you trying to declere struct again
            Yeah, that doesn't seem to work either.
            Isn't there any example out there of a struct being used in a class? It's kinda embarrassing that I can't get a simple struct compiling properly.

            Comment


              #7
              For some reason (and I really don't know why) but if you change the struct to this:

              Code:
              USTRUCT(Blueprintable)
              struct FMyCommand
              {
              	GENERATED_USTRUCT_BODY()
              
              	UPROPERTY()
              	int32 Id; //Lowercase d here
              
              	UPROPERTY()
              		int32 BaseDamage;
              
              	UPROPERTY()
              		float PlayRate; //Capital R in rate
              };
              Then it will compile fine. My guess is that the generated code requires variable names to be a specific format? This could be a bug, I don't know.

              EDIT:
              By the looks of it, you still have to define it outside of the class.
              Last edited by Daganar; 04-02-2014, 07:49 PM.

              Comment


                #8
                Originally posted by Daganar View Post
                For some reason (and I really don't know why) but if you change the struct to this:

                Code:
                USTRUCT(Blueprintable)
                struct FMyCommand
                {
                	GENERATED_USTRUCT_BODY()
                
                	UPROPERTY()
                	int32 Id; //Lowercase d here
                
                	UPROPERTY()
                		int32 BaseDamage;
                
                	UPROPERTY()
                		float PlayRate; //Capital R in rate
                };
                Then it will compile fine. My guess is that the generated code requires variable names to be a specific format? This could be a bug, I don't know.

                EDIT:
                By the looks of it, you still have to define it outside of the class.
                For some weird reason that seems to compile properly. I guess that there's some rule telling the compiler not to compile variables with a certain name...
                However, even after compiling I can't access the struct from my blueprint. Is there any parameter or anything I need to add to be able to access it?

                Comment


                  #9
                  Originally posted by Daganar
                  Yeah the problem is 100% in the generated code, they way to access it would be ACustomCharacter::FMyCommand but the generated code is just doing FMyCommand... I don't know how to force the build to do this and the wiki doesn't seem to have any answers either, sorry.

                  https://docs.unrealengine.com/latest...cts/index.html

                  EDIT:
                  Do you really need to be able to access the struct from blueprint? If you just define the struct as a plain C++ struct (without USTRUCT and GENERATED_USTRUCT_BODY()) it will work fine.
                  I do need to access it from the blueprint. I want to make my character have a "equippable" attacks from a menu to use in-game, and organizing all the information for each attack in a struct would be the way to go in my opinion. However, if I use a regular struct I can't access it from the blueprint system, can I?

                  EDIT: Nevermind, I actually just got it working. I can just use the Break node from the blueprint system.
                  Last edited by Keytotruth; 04-02-2014, 08:06 PM.

                  Comment

                  Working...
                  X