Announcement

Collapse
No announcement yet.

Blueprint rules for references to other objects

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

    Blueprint rules for references to other objects

    TLDR: Local reference types don't seem to actually keep references.

    In my GameMode class, I have a Map, TradeSkills.
    I also have a function library my other blueprints use for handling tradeskills. In one of the functions, I needed to operate on the GameMode TradeSkills map a lot, so I set a local variable to reference it and work off of the local map.

    At this point, I would expect if I add entries to the local map, it would actually add it to the tradeskills map. This doesn't seem to be happening.

    This function is being called from the GameMode itself (for initialization), so I know it's happening on the server.

    Are there not two types of variables in blueprints, reference types and value types? I would expect a MAP to be a reference type. Or, is there some aspect of GameMode I am missing?

    #2
    I might be misunderstanding what your trying to do but local variables deletes itself once the function is done. It doesn't retain any data.

    Also if the variables can hook up then it works. Unless it shows an error. The other thing since you said its being called on initialization is that the All trade variable is being set before the game mode initialize. So it'll return as null. Just add a delay 0.1 or something.

    If you already check these just ignore me.
    Checkout Project "TwinBlaster" my current game in progress.
    [FREE] - 2D ROPE! Find out more here!
    Forgive my username... It's an old game name and wrote it down as a habit.

    Comment


      #3
      I would expect local variables to be removed after the function is done, it's a scope thing.
      But, let's say I have an array defined in my GameMode. I have a local array, that I set to equal the gamemode array.
      I would expect this to basically be a pointer to the array, and if I add elements to the local array they should actually be added to the GameMode array.

      Unless...blueprint local variables are always COPIES of the gamemode array, as if they were value types.

      In this case, is it a pointer to the original array, or a copy? If it's the latter, color me shocked.

      Comment


        #4
        Originally posted by Strombonni View Post
        But, let's say I have an array defined in my GameMode. I have a local array, that I set to equal the gamemode array.
        I would expect this to basically be a pointer to the array, and if I add elements to the local array they should actually be added to the GameMode array.
        I would personally use the game instance, but make your own custom game instance first and name it something easy to distinguish from the regular GI. The reason for this is because nothing else survives level changes.

        You would need the following workflow to make that work:
        GI has some populated array in it
        BP has some function that gets a reference to the GI, reads it's array and set's a variable, within the BP, to equal the GI's array
        BP does some stuff to that copy of the array and now wants to push the updated information back to the GI
        Right click in BP, get current game instance, cast to the custom game instance that you made(the name will match so make sure you name it something you will recognize)
        From there, you can access the GI's variables and set that array equal to your array within the BP
        Last edited by IronicParadox; 04-30-2018, 09:19 PM.

        Comment


          #5
          Originally posted by Strombonni View Post
          I would expect local variables to be removed after the function is done, it's a scope thing.
          But, let's say I have an array defined in my GameMode. I have a local array, that I set to equal the gamemode array.
          I would expect this to basically be a pointer to the array, and if I add elements to the local array they should actually be added to the GameMode array.

          Unless...blueprint local variables are always COPIES of the gamemode array, as if they were value types.

          In this case, is it a pointer to the original array, or a copy? If it's the latter, color me shocked.
          Yeah it's just a copy and any changes made to the copy won't "replicated" to the original. Also Gamemode is only server side. Trying to call from client well always be null.

          Any reason you can't just change the Gamemode All trade?
          Checkout Project "TwinBlaster" my current game in progress.
          [FREE] - 2D ROPE! Find out more here!
          Forgive my username... It's an old game name and wrote it down as a habit.

          Comment


            #6
            Well, this just blows my mind. I'm from the C# world, where reference types like arrays and other objects are never copied like this, you get a pointer/reference to the original object. I mean..what if the map is huge, that's incredibly inefficient.

            I gotta get confirmation on this.

            As far changing the gamemode...well, yes, but every time I wanted to reference it I'd have to get a reference to my gamemode, then from that access the tradeskills map. It's...clunky.

            Oh, I'm using GameMode because this is all happening on the dedicated server.


            Comment


              #7
              Nobody is stopping you from making it as efficient as possible by leveraging C++. Blueprint can do a lot of things but it is a shell of what C++ is so it has many limitations still.
              You mention that you use GameMode to make sure it happens on the server. There is many ways you can ensure things happen on the server from any actor that live on the server. The "IsServer" function that returns a bool is one of them. "HasAuthority" is the most common way of asking a replicated actor if it is server-side or client-side.

              Comment


                #8
                There's a " pass by reference" checkbox in there.
                | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

                Comment


                  #9


                  Originally posted by BrUnO XaVIeR View Post
                  There's a " pass by reference" checkbox in there.

                  I guess I'm blind...I don't see that on any of the nodes. I have a node to get the game mode, a node off of THAT to access its TradeSkills property, and a node from there to set a local variable to the GameMode.Tradeskills property. On none of them to do I see a pass by reference.



                  Originally posted by GarnerP57 View Post
                  Nobody is stopping you from making it as efficient as possible by leveraging C++. Blueprint can do a lot of things but it is a shell of what C++ is so it has many limitations still.
                  You mention that you use GameMode to make sure it happens on the server. There is many ways you can ensure things happen on the server from any actor that live on the server. The "IsServer" function that returns a bool is one of them. "HasAuthority" is the most common way of asking a replicated actor if it is server-side or client-side.
                  Effecient? In what way? Definitely not as productive, in my opinion.

                  But, are you seriously saying that accessing a MAP object variable defined in the GameMode, and storing a reference (not a complete copy) it in the calling blueprint's local variables is out of the question, and I need to use C++ instead? I promise you, the guys in the Unity forums would have a field day with THAT, and I personally don't consider it reasonable, especially for a potentially data heavy object like a map. A value type, sure. I would expect a copy. I mean...it's fine, I can work around it, but still I find it hard to believe that Blueprints would behave this way.

                  Also, I didn't say I use GameMode to make sure it happens on the server, I think you misunderstood what I said. What I said was, I'm using the GameMode to store the map contents because I know this is happening on the server. And I'm pretty sure the GameMode is a recommended place to store this kind of data that I want to persist in memory, or at least from what I've read.

                  Comment


                    #10
                    Originally posted by Strombonni View Post
                    <<<There's a " pass by reference" checkbox in there>>>. I guess I'm blind...I don't see that on any of the nodes. I have a node to get the game mode, a node off of THAT to access its TradeSkills property, and a node from there to set a local variable to the GameMode.Tradeskills property. On none of them to do I see a pass by reference.

                    In a Macro or Function definition, its part of the Details-pane 'Inputs'.
                    It may be hidden, so click on the 'expand icon' to view the dropdown.
                    Then you should see a Tickbox marked 'Pass-By-Reference' etc etc...

                    Comment


                      #11
                      Ok, I thought that might be what Bruno was talking about. Sadly, in this case it's not a function or macro input.

                      Comment


                        #12
                        Originally posted by Strombonni View Post
                        Ok, I thought that might be what Bruno was talking about. Sadly, in this case it's not a function or macro input.
                        No worries, just work-off direct references... For example, store your major byRef's in Gamemode / Game-Instance or whatever, then access them through UE4's built-in helper-functions. To skip casting, store a one-time reference created during Begin-Play, or add your own helper-funcs using a BP Function Library etc etc.

                        Comment


                          #13
                          I've got some more clarification on another thread, and this person stated that in general, objects actors and such are passed by reference, even when referencing them in another object. Maps, arrays, and sets are NOT when you are just referencing them as a property on another object, instead they are copied. This is surprising to me, it seems very ineffecient as these types are meant to store data, and often lots of it.

                          Regardless, it may be better for me to have a dedicated Object in GameMode, which is responsible for storing these types, and I then pass that around.
                          Again, the issue is you don't always get these passed in via an input in a function or macro, sometimes you just access them as a variable in another object.


                          I do wish Epic could document/clarify this.

                          Comment

                          Working...
                          X