Jump to content
  • Advertisement
MarcusAseth

Unreal Blueprint - passing Constructor arguments for a spawned class

Recommended Posts

I didn't had much luck so far asking on the Unreal official forum (seems there is not much traffic over there :/ ), so I'll ask my question here as well in the hope that someone knows ^_^'

I'm trying to make a Worm game using Unreal mixing C++ and Blueprint, here some of the code I have:

struct TArrayBool {
	TArray<bool> BoolRow;
	inline bool& operator[](int Column) { return BoolRow[Column]; }
};
      
//Inside the WormGameMode.h

//...
      
//This multi-dimensional array holds bool to represent if a block in the grid of the game is already occupied or not
//This way I know in which grid blocks I am allowed to spawn Pickups
TArray<TArrayBool> GridSlotsOccupiedMap;
      
      
//Inside the WormGameMode.cpp
 
AWormGameMode::AWormGameMode():AGameModeBase()
{
	//Setup GridSlotsOccupiedMap
	TArrayBool Temp;
	Temp.BoolRow.Init(false, GridWidth / GridBlockSize);
	GridSlotsOccupiedMap.Init(Temp, GridHeight / GridBlockSize);
}

//This is a BlueprintNativeEvent
//It just Toggle the occupied status at a given [row][column] in the map
void AWormGameMode::ToggleOccupiedBlock_Implementation(int Row, int Column)
{
	GridSlotsOccupiedMap[Row][Column] = GridSlotsOccupiedMap[Row][Column] == true ? false : true;
}

As you see when I spawn a pickup I toggle the corresponding bool inside the "GridSlotsOccupiedMap" 2dArray inside the class and then use InitPickupID to store that same ID inside the pickup itself, so when the Worm collide with it and the pickup get destroyed I know which ID on the 2dArray should be toggled again (as allowed starting position for spawned stuff).
So my concern here is, is it possible that the SpawnActor by random chance spawns the pickup just in front of my Worm which steps on it before the InitPickupID is executed?
If that could happen I wouldn't be able to toggle the corresponding ID on the map thus that slot won't be able to spawn any more pickups.
So there is a way for me to pass those informations directly to the Pickup.h constructor so that the ID is recorded before the spawning? :/

BlueprintImageLink

QdBN8gP.png

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
Advertisement

First up, when you try to enforce a bidirectional link like this, between the occupied map and the pickup, you create the need to keep both sides in sync, forever, which is awkward. Try not to duplicate such information across 2 objects - store it directly in the object that needs to access it most often, and have the other object derive it somehow. That way, they can never get out of sync.

Secondly, in UE4, you shouldn't really be thinking about constructors, because they get called on your behalf. You can set the values in the next node in the Blueprint. I don't see that it is likely that there will somehow be a timing gap between the spawning node and the next node. Unless one of the nodes is explicitly marked as a latent one (which I think comes up with a little clock icon on it) then you can assume the whole graph will execute before any other logic takes place.

Share this post


Link to post
Share on other sites
5 minutes ago, Kylotan said:

First up, when you try to enforce a bidirectional link like this, between the occupied map and the pickup, you create the need to keep both sides in sync, forever, which is awkward. Try not to duplicate such information across 2 objects - store it directly in the object that needs to access it most often, and have the other object derive it somehow. That way, they can never get out of sync.

This seems really like a great suggestion, especially because is easy to derive that information based on the pickup position on the map, I was probably overtinking it and getting stubborn from the wrong perspective xD

I'll do that, Thanks Kylotan :)

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

Unrelated to your question (which Kylotan answered), flipping a bool can also be done like this (less error-prone and quicker to write):

 

//Somewhere.
bool myBool = false;

//Wherever you just want to flip the bool.
myBool = !myBool;

 

Share this post


Link to post
Share on other sites
43 minutes ago, Lactose said:

less error-prone and quicker to write

Interesting and true, I didn't had thought about that and judging by the code below seems also clearer to read, I'll do it this way from now on, thanks Lactose :)

 

bool& Bool = GridSlotsOccupiedMap[Row][Column];
Bool = !Bool;

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!