Global variable to local?

I guess it’s kind of similar issue to forums.unrealengine.com/t/is-it-possible-to-convert-a-local-variable-to-a-global-variable-in-blueprint/519531

When I drag’n’drop variable from global to local it doesn nothing.
image

There is nothing in the context menu…
image

And replace references is not showing local variables…

Is there some fast way to make a global variable local? It would very handy when you collapse some node graph to function.

I think your question doesn’t make sense.

If you want a local variable, declare it inside a function.

Hi @Andrej730

Do you need both variables? If so then create a new LOCAL variable with a different name but of the same type. Then at the start of the function SET the local variable to the GET of the global variable.

If you need the variable to exist outside the function then you must leave it as global and setting another variable locally is just a waste of time as you have full access to the global variable anyway.

If its only needed locally and not globally then just create the variable inside the function and delete the original global. Though have you tried copy and paste? It does give you an option to paste as local or global but that maybe limited to between functions/classes copy rather than within the same blueprint.

It’s not that I don’t understand what local and global variables are.

It’s just that sometimes at the start I’m not sure wheither I should setup a function and I make a blueprint with just global variables.
Then refactoring I collapse part of the blueprint to function to make it more readable.
Then to keep it clean I want to make local all global variables that were used only in this function and it seems there is no simple way to do it - I need to create local duplicate for each variable and then go manually through each occurence and replace global variable with local :slightly_frowning_face:

I’m shocked i cannot find a way to do this either. Very much want to refactor.

You don’t need to do the set and get manually, after creating the local variable, click on the global variable in the function and you will see an option called “Replace…” you have to go one by one, Maybe you cannot chage all always, but at least it saves a lot of work.

1 Like

Not it actually makes a lot of sense if you have a ton of class-wide variables and you want to convert them to be local variables if they’re only being used in one function. It’s very sensible and really annoying when you’re talking upwards of 10 variables. Super unhelpful response.

Do you know what a local variable is?
By definition it is a variable declared inside a function.

Do you know what a local variable is?
By definition it is a variable declared inside a function.

Exactly. So if you have a ton of class-wide variables that are only being used in one function, it would make sense to convert them to be local variables then. Because as you say, by definition they would only be relevant to the function they’re being used in.

Do you know what refactoring is?

This is the question he did…

And this was my answer…

You have a parallel universe in your brain…

Don’t make assumptions and limit yourself to answering the user question.

Guys. Original Poster made a variable, then realized that he should have made it a local variable. That’s why he says he is trying to drag it down to the local variable section. He knows what local variables are. He just made a mistake and wants to easily fix it.

Answer for original poster: no, you cannot just drag the variable down to local and change it. I see no way to easily do this. You have to make a new local variable and then replace the old one everywhere with the new local one. Then delete the old one.

1 Like

That’s how it is… it is enough to delete the old variable from the global scope and create a new one inside the function…

His code in BluePrint will probably break and he’ll have to rewrite it.

That’s one of the reasons I recommend using C++ wherever possible.

Here’s the Hacker mode :mage:

  • Grab all the nodes from the function
  • Then do a Cut (Control-X)
  • Delete the variables in the my blueprint tab
  • Paste all the nodes again
  • The global variables you deleted now appear grey in the function
  • Hover over it and use the context menu to press “create local variable” and magically all the variables become active again and the local variable is created.
4 Likes

Good to know this!!
Thank you for the trick!!

That’s very clever, I like it!

Found a downside of this method - it’s just referring to the variables by their names, so when variables will be recreated as locals they will lose all their metadata (e.g. description, default value, value ranges, etc). Which is probably okay in the most cases since local variable by definition won’t be exposed to the UI and most of this metadata won’t be needed.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.