x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Function libraries: How to store class variables

In many cases I just want to store logic in a blueprint, essentially some functions along with some variables (accessable for ALL the functions). Essentially I like them to use like a static class.

So I tried to use class blueprint derived from Actor for everything (which doesn't really make sense to me). But that's really complicated and error-prone because I have to instantiate all of my 'code containers' by hand in each scene.

I can't use macro libraries either, because there I can't use variables AT ALL (not even local variables).

alt text

I essentially just want a function container with shared variables. Does anyone know how to achieve this?

Thank you, spyro

Product Version: Not Selected
Tags:
more ▼

asked Nov 25 '14 at 06:29 PM in Blueprint Scripting

spyro gravatar image

spyro
1.4k 71 87 189

DanZaidan gravatar image DanZaidan Jan 27 '17 at 12:42 PM

Kind of old, but did you manage to find a good way to achieve this?

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

You could create a Master Actor which have all the variables you want to use. Then when you want to create a new actor you can create a Child Actor of the Master Actor instead of a generic Actor. Then all your actors will inherit the variables, function etc which are created inside the Master Actor. In theory the Master Actor will act as both function an variable library.

more ▼

answered Jan 27 '17 at 01:23 PM

Alltvin gravatar image

Alltvin
366 9 6 20

DanZaidan gravatar image DanZaidan Jan 27 '17 at 01:36 PM

That sounds good. Although I believe storing a shared value so all actors (and UMG Widgets) can access it would still be a problem. After some research, I ended up going the Game Instance approach. I think I'll work nicely.

Alltvin gravatar image Alltvin Jan 27 '17 at 01:42 PM

hm, yes for sure. I guess you can use Game Mode as well, which is both consistent during gameplay and accessible from every Blueprint (actors and UMG) by "Get Game Mode" -> "Cast to [AssetName]".

(comments are locked)
10|2000 characters needed characters left

I came up with a way by creating a function with a static variable in its scope. At a glance it actually looks very ugly to me, seeming to re-run initialization and go out of scope. But as a static variable it actually persists and doesn't re-initialize. If your function has a parameter for setting that defaults to an appropriate value, and has a return value for getting, you can use it in a Blueprint Function Library to act as a static variable. Although I don't find the solution to be pretty at its core, you can wrap it up with your own functions and have a solution that doesn't use a dummy Actor or global object like Game Mode.

FString UMyBlueprintFunctionLibrary::AccessMyString(FString MyString = TEXT("")) { static FString MyStringValue = FString();

 if (!MyString.IsEmpty())
 {
     MyStringValue = MyString;
 }
 return MyStringValue;

}

more ▼

answered Mar 20 '17 at 09:20 PM

cskeels gravatar image

cskeels
6 1 1 2

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question