Unreal Blueprint - passing Constructor arguments for a spawned class

Started by
6 comments, last by MarcusAseth 6 years, 8 months ago

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

Advertisement
1 minute ago, MarcusAseth said:

so I'll ask my question here as well

What's the question?

Hello to all my stalkers.

Just now, Lactose said:

What's the question?

Accidentally hit the key to post before I had finised writing, I've fixed it now :)

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.

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 :)

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;

 

Hello to all my stalkers.

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;

 

This topic is closed to new replies.

Advertisement