Hi all,
here’s my situation: I’m working on a simple adventure/puzzle game concept. I’m trying to create a mechanic where a user can use an item from their inventory on an item in the game world (let’s call that action ‘combine’). Like this: upon finding an interactive object in the world (that has its own Combine element), the player presses the combine button (_combineAction) that would allow them to select an item from the inventory (using arrow buttons for example), upon which they would confirm the choice of item (with _selectItemAction) and then the game would pass the selected inventory item to combine function from the particular interactive object.
My issue is this: after calling the player’s combine action (currently done from within the player’s update function) the game doesn’t recognize the subsequent inputs… like selecting the items and confirming. I’ve tried different variations of loops, and also a coroutine but no luck so far. I understand that this is probably caused by the fact that update is called every frame but how to implement something like this, then? I’m very new to game programming (though not to programming as such) so not even looking for a code fix, but rather what are the concepts I need to look into? More coroutines? Unity events? Something else completely?
My code looks sort of like this (only including the relevant parts):
private void Update()
{
// Combine
if (_combineAction.WasPressedThisFrame() && _interactableCombine != null)
{
// Start at the first inv position
int itemIndex = 0;
// Here the player would chose an item from the inventory - not implemented yet (always using the first item, index=0)
// Once you choose an item, press a button to interact
Debug.Log($"Press {GetActionLabel(_selectItemAction)} to use item");
// This is the coroutine that should wait for the selectItemAction
// _interactableCombine is the combine element of the game world object I'm trying to interact with
waitForItemSelect(itemIndex, _interactableCombine);
}
}
And this is the coroutine:
private IEnumerator waitForItemSelect(int itemIndex, Combine interactableCombine)
{
while (!_selectItemAction.WasPressedThisFrame())
{
yield return null;
}
var selectedItem = GetInvSlotItem(itemIndex);
// On success we should stop the selection process
if (interactableCombine.Combine(selectedItem)) Debug.Log("Success.");
// On failure we shouldn't stop but let player know it was a failure
else Debug.Log("Wrong item.");
}
When I run this I get the debug message prompting me to press the select key but when I do nothing happens, the coroutine is completely ignored.
Many thanks in advance.