I want an actor to control level blueprint variables.
Actually I am making a turn-based board game, and there has to be function like this:
ActorOnClicked -> Move the actor -> End turn('turn' is a level blueprint variable)
How can I get/set level blueprint variables?
Actors could be in any levels so you can't access a specific level from an actor blueprint.
Instead the Actor should dispatch an event "EndTurn" that the level should hook up a listener to when that actor is created.
You can do this with "event dispatchers" https://docs.unrealengine.com/en-US/Engine/Blueprints/UserGuide/EventDispatcher/index.html
Related
I Have a variable that updates every time i move my cube in the level blueprint , now i want to access this variable from multiple class blueprints , what do I do , I tried casting to gamestate but didn't succeed , I am really new to ue4 if you could explain in details please
edit: sorry for not adding details ,
The var I want to access is an integer named cube_side that tells me what side the cube is on every time I move , all of this happens in the level bp , I want to access this variable to see what side the cube is on from other class blueprints ->
here are some details in a picture
I know it's not good to code everything in the level blueprint , but it's too late now , I only need to transfer the var cube_side to other class blueprints so the other object can change depending what side the cube is on.
Create an Actor Class for your logic/functionality. Then use Get all actors of class (choose your class) -> Get a copy -> get variable
Communication with the level blueprint is rather tricky in UE4, since they are not as persistent as e.g. the GameMode, and therefore shouldn't be accessed directly (Imagine older games like Final Fantasy where a new level was loaded every time you stepped outside a boundary, so relying on it could potentially break your actors or crash the game due to nullptrs).
It's a little hacky, but works:
Move the variable inside the cube-blueprint. Add an event dispatcher to the cube, if it is moved, call it and pass the variable in.
Select the cube in the editor, open the level blueprint, right-click, "add reference to selected actor" (the cube must be part of a blueprintclass, not only a static mesh dragged in, though), and bind the event dispatcher inside the Level BP.
Create a function inside every blueprint that needs access to the variable, which does whatever it should do, depending on the variable.
The custom event of the Level Bp (that was bound to the Event Dispatcher of the cube), needs references to all actors that have to work, when the variable changes and call each Actors function (you can get the references like you got the one from the cube)
Then, every time the variable changes, the Level BP is notified, the custom Event is executed and this custom event calls all the actor's functions.
Event Dispatchers explained
This is a huge wastage of functions/code, since you only need it for this one level and may never use it again. Avoid this in the future, by not relying on the level BP so much.
You can use GameStateBP to create and store all variables that you need in game, in GameModeBP create functions to get and set this variables via Get Game State function and then function Cast To GameState and then logic. After that from any blueprint access it using Get Game Mode -> Cast to you GameMode -> use your function to set or get data from GameState.
In my application, I have a class which needs to communicate with an existing actor already created in an actorSystem. How do I get a reference to that actor from this non-actor class?
If you have the reference to the actor system (https://doc.akka.io/api/akka/current/akka/actor/ActorSystem.html), then you can simply invoke actorSelection with the path of the actor. This will give you an ActorSelection, on which you can call resolveOne, and then wait until you get the reference of the actor. Once you have the actor reference, you can send messages to it.
Here is more information about addressing actors: https://doc.akka.io/docs/akka/current/general/addressing.html .
I want to make a library that is going to be used and configurable by other people, using akka actors.
I want to make a Repository actor to which a type of storage will be injected by the library user like "MemoryDatabase" or "FileDatabase" or anything that extends a defined class or actor (see question 2).
1) What is the best way for a user to specify options (including his own created ones)?
2) Should "MemoryDatabase" be an actor child of repository, or just a normal class as a field/property of the Repository actor in this case?
Thanks!
-Jojolepro
Edit:
I want to make an actor that looks like
class Repository extends Actor{
val repoType:DataStorageType = ...
def receive:Receive={}
}
What I want to know the best way for a end user of such library to specify the repoType field, considering that Repository is already in a predefined actor hierarchy.
It depends on if you want the user to put in a concrete instance or merely a type. Ignoring any kind of DI system you could use, I would merely pass the concrete instance to the actor's constructor via props when the actor is started but then that would depend on the concrete instance being thread safe because not necessarily only that actor would be managing it. If you want the actor to own the repository, have the user pass the factory instance or type to the props and thus the constructor.
I'm using akka to create a supervisor :
mySupervisor = actorSys.actorOf(Props.create(MyActor.class, MyProperties()));
class MyProperties(){
String param1;
String param2;
//param1 & param2 are set in a configuration file
}
Since creating a supervisor from the ActorSystem is expensive I'm just doing this once. I'm using the class MyProperties in order to
access various parameters that are required within the actor. I do not want to add the logic for setting the propertes to the actor itself so the actor
has little work to perform as possible and the usually these peoperties will not change. But when the properties do change how can I update
the child actors of the supervisor ? I do not think I can change the state of mySupervisor (since immutable) so does this mean I will need to create a new supervisor with the
new properties configuration ?
You should never never ever ever ever ever change the state of an actor with anything except a message sent to that actor.
If you need to change the properties of a child actor, you can:
Kill the actor and create a replacement with the new properties
Send a message to the actor with new properties
In addition, messages sent to an actor should be immutable (final String param1). If you need to access an actor's internal state, you should send that actor a message asking for it and then have the actor reply to that request with whatever (immutable) state is needed.
In need to create an Akka2 actor (derived from UntypedActor) as a child of an existing actor (also derived from UntypedActor). The only reference I have to the parent actor is an ActorRef. Is there any way to do this? I'd like to call the parent's UntypedActorContext.actorOf() method, but don't know how to get a reference to it using the Akka API. Is there a better way of accomplishing my goal?
You cannot force someone to conceive against their will. Your actor needs to receive a message to which it responds by creating a new actor and send you the ref to that actor.
Can you change the code of the parent actor? You could for example add a handler for a message of type Props in your parent and create the child there. It is not possible to get the context outside of the actor class.