Public Group

# Unreal Blueprint - passing Constructor arguments for a spawned class

This topic is 387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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?

Edited by MarcusAseth

##### Share on other sites
1 minute ago, MarcusAseth said:

so I'll ask my question here as well

What's the question?

##### Share on other sites
Just now, Lactose said:

What's the question?

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

##### Share on other sites

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

I'll do that, Thanks Kylotan

Edited by MarcusAseth

##### 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 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

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007532
• ### Who's Online (See full list)

There are no registered users currently online

×