Announcement

Collapse
No announcement yet.

Struct not updating values?

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

    Struct not updating values?

    Hi there. I'm not sure if this is the right section, but here goes.
    I'm trying to modify some of the variables inside a struct array I made. I loop through the array with a foreach loop but it doesn't seem to do anything, even when I hide unnecessary pins.
    I thought maybe the Set Members in Struct function was not working, so I made my own functions to set members:


    Code:
    void AMainCharacter::SetRecharge(FCommand NCommand, const float Time)
    {
    	NCommand.currentRechargeTime = Time;
    }
    
    void AMainCharacter::SetTotalRecharge(FCommand NCommand, const float Time)
    {
    	NCommand.maxRechargeTime = Time;
    }

    Click image for larger version

Name:	BP.jpg
Views:	1
Size:	218.2 KB
ID:	1142903


    This is the struct I made:

    Code:
    USTRUCT(Blueprintable)
    struct FCommand
    {
    	GENERATED_USTRUCT_BODY()
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		int32 ID;
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		FText CommandName;
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		TEnumAsByte<ECommandType> Type;
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		UTexture2D* CommandIcon;
    
    	//Base damage or recovery dealt by the command.
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		int32 BaseDamage;
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		TEnumAsByte<EElement> CommandElement;
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		float ElementPercentage;
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		float currentRechargeTime;
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Commands")
    		float maxRechargeTime;
    
    	FCommand()
    	{
    		BaseDamage = 0;
    		maxRechargeTime = 1000;
    		currentRechargeTime = 0;
    	}
    };

    However, even after using those functions, the value printed on screen is still 0.2, which doesn't make much sense. Am I doing anything wrong?

    #2
    Right now you need to Break and Make the Struct. Not sure when the fix is coming, but Epic is aware of the issue.
    Map Generator 1.0
    Map Generator 2.0
    Map Generator 3.0

    Comment


      #3
      Originally posted by Zeustiak View Post
      Right now you need to Break and Make the Struct. Not sure when the fix is coming, but Epic is aware of the issue.
      Wow, that explains a lot. Been having this issue too but I could never understand what the problem was.
      Twitter: @zerofiftyone_
      Website: zerofiftyone.net - My game development blog
      Button Frenzy store page: http://store.steampowered.com/app/454630

      Comment


        #4
        That seems to work for some reason. I expect Epic to get this solved as soon as possible.
        I think this is a major issue. After all, it's one of the most basic structures you can have...

        Anyways, thanks for the help!

        Comment


          #5
          As of 4.15 this is still an issue, any word on a fix for this?

          Comment


            #6
            It is working, what issue are you having?

            Comment


              #7
              Originally posted by Everynone View Post
              It is working, what issue are you having?
              [MENTION=2637]Everynone[/MENTION], Thank you for the fast response.

              So i'm working on creating a tilegrid based game where each tile has data associated with it that I would like to store in a struct with the following properties:

              Click image for larger version

Name:	Screenshot_1.png
Views:	1
Size:	32.9 KB
ID:	1124090

              That struct then becomes an array on which I perform loops to do mass operations on. The problem however starts when I try to store the result back into the array, I have tried nearly all fixes I could think off and found around but those "darn" (due to the profanity filter I could unfortunatly not use a more harsher term) values just refuse to be set in the array.

              Click image for larger version

Name:	Screenshot_2.png
Views:	1
Size:	83.1 KB
ID:	1124091

              From this post I gathered that the array is not passed by reference and therefore ultimately won't be set, see red line in the above screenshot.


              Also this issue is still unresolved according to the issue tracker

              Any suggestions?

              EDIT: Currently trying the method proposed here by Techlord
              Last edited by Stratego99; 02-28-2017, 07:13 AM.

              Comment


                #8
                The key when setting struct array elements is to use SetMembersInStruct, see here, especially the bottom bit:

                https://forums.unrealengine.com/show...l=1#post605476

                Comment


                  #9
                  I did try that method and got the same result at a few posts down in that thread you linked:

                  https://forums.unrealengine.com/show...l=1#post605673

                  With the exact setup the element in the struct array never gets edited, which is the same in my case.

                  Comment


                    #10
                    Works as intended in my humble opinion, see if you can replicate this:

                    Click image for larger version

Name:	Untitled.png
Views:	1
Size:	321.2 KB
ID:	1124094

                    One things that is still not so great is passing struct arrays as a reference.

                    Comment


                      #11
                      ...It worked, but i'm just flabbergasted since I just finished recreating my system to a workaround and now it turns out that it was for nothing.

                      Thank you very much though!

                      Lessons learned: Don't do blueprints for more than 10+ hours a day, mistakes like these start creeping in and then you waste time backtracking.

                      Originally posted by Everynone View Post
                      One things that is still not so great is passing struct arrays as a reference.
                      I believe that I ran into this and must have misinterpreted what the error exactly was, could you expand on this in what cases the above method might fail?

                      Comment


                        #12
                        Originally posted by Stratego99 View Post
                        I believe that I ran into this and must have misinterpreted what the error exactly was, could you expand on this in what cases the above method might fail?
                        So, consider this:

                        Click image for larger version

Name:	structshenanigans1.PNG
Views:	1
Size:	180.0 KB
ID:	1124239

                        So then you're thinking, clearly looping through a struct array must be creating copies (pass by value)! Just look at them cheeky navy-blue icons:

                        Click image for larger version

Name:	loopdeloop.png
Views:	1
Size:	46.9 KB
ID:	1124241

                        All right then, lets do it without the loop:

                        Click image for larger version

Name:	whatthehell.PNG
Views:	1
Size:	174.3 KB
ID:	1124242

                        Duh, I must set it by reference, too!

                        Click image for larger version

Name:	waitwhut.PNG
Views:	1
Size:	156.9 KB
ID:	1124243



                        You can tick pass-by-ref checkbox all day long - it's a trap. You can't pass struct by reference, you can't get a pointer to a struct. As far as I know a struct is not a UOBJECT and this behaviour is not supported, not in Blueprints, that is. If I remember correctly I got it working fine in C++.



                        Workarounds exist and they're pretty straightforward, this works like a charm, for example:

                        Click image for larger version

Name:	works.png
Views:	1
Size:	141.0 KB
ID:	1124244

                        That's not even a workaround, tbh, that's how things are.

                        Hope it helps.

                        Comment


                          #13
                          uhm, it's because your c++ code is wrong.

                          Code:
                          void AMainCharacter::SetRecharge(FCommand* NCommand, const float Time)
                          {
                          	NCommand.currentRechargeTime = Time;
                          }
                          
                          void AMainCharacter::SetTotalRecharge(FCommand* NCommand, const float Time)
                          {
                          	NCommand.maxRechargeTime = Time;
                          }
                          Should work.
                          Cheers.

                          e/ Oh, 2015, nevermind.
                          @CarstenZarbock - Follow me on Twitter, thank you

                          Comment


                            #14
                            I have given up on understanding this ****.
                            A while ago I ran head first into the problem and found no way to avoid it.
                            Right now I'm doing something similar and it works like a charm. I'm not even using "set member in struct", I'm just setting array elements normally in a for-each loop.

                            I just hope it will continue to work.

                            Edit:
                            Actually, now that I think about it, I initially used a function parameter to access the array.
                            Right now I'm calling the array directly, as I'm in the messy prototyping phase. That probably circumvents the bug, even if it kinda breaks the idea of good programming.
                            Last edited by vb4; 03-02-2017, 04:14 PM.

                            Comment


                              #15
                              [MENTION=2637]Everynone[/MENTION], THANK YOU SO MUCH!!!

                              I finally get it now due to your clear tutorial and have been able to re-write my grid generation with your advice and it works perfectly now.

                              I have ended up keeping all changes made to GeneratedMapData within the parent blueprint (might move it to gamestate) and whenever I need a value I just cast to that parent blueprint and get my GeneratedMapData values from that function result.

                              Comment

                              Working...
                              X