Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Last active September 22, 2022 17:19
Show Gist options
  • Save Turupawn/e2ca50e7425ada3b4ef69202b2c690f6 to your computer and use it in GitHub Desktop.
Save Turupawn/e2ca50e7425ada3b4ef69202b2c690f6 to your computer and use it in GitHub Desktop.
Steamworks Create Example
#include <iostream>
#include "steam_api.h"
//The Game Manager class is needed to handle Steamworks callbacks
class CGameManager
{
public:
void CreateItem();
bool finished = false;
AppId_t nConsumerAppID = (AppId_t)517510;
private:
void onItemCreated(CreateItemResult_t *pCallback, bool bIOFailure);
void onItemSubmitted(SubmitItemUpdateResult_t *pCallback, bool bIOFailure);
CCallResult<CGameManager, CreateItemResult_t> m_CreateItemResult;
CCallResult<CGameManager, SubmitItemUpdateResult_t> m_SubmitItemUpdateResult;
};
//We create a new Item by providing the game ID and we specify we are creating a normal mod that can be subscribed to
void CGameManager::CreateItem()
{
std::cout << "Creating item..." << std::endl;
SteamAPICall_t hSteamAPICall = SteamUGC()->CreateItem( nConsumerAppID, k_EWorkshopFileTypeCommunity);
m_CreateItemResult.Set(hSteamAPICall, this,&CGameManager::onItemCreated);
}
//Once the mod was created, we can grab the id from the callback params and then customize it with the UGCUpdateHandle_t before making another API call to SubmitItemUpdate
void CGameManager::onItemCreated(CreateItemResult_t *pCallback, bool bIOFailure)
{
if(pCallback->m_eResult == k_EResultOK && !bIOFailure)
{
std::cout << "Item created!" << std::endl;
if(pCallback->m_bUserNeedsToAcceptWorkshopLegalAgreement)
{
SteamFriends()->ActivateGameOverlayToWebPage("steam://url/CommunityFilePage/");
}
UGCUpdateHandle_t handle = SteamUGC()->StartItemUpdate(nConsumerAppID, pCallback->m_nPublishedFileId);
SteamUGC()->SetItemTitle(handle, "Title test");
SteamUGC()->SetItemDescription(handle, "Description test");
SteamUGC()->SetItemUpdateLanguage(handle, "None");
SteamUGC()->SetItemMetadata(handle, "Test metadata");
SteamUGC()->SetItemVisibility(handle, k_ERemoteStoragePublishedFileVisibilityPublic);
SteamParamStringArray_t *pTags = new SteamParamStringArray_t();
pTags->m_ppStrings = new const char*[1];
pTags->m_ppStrings[0] = "stage";
pTags->m_nNumStrings = 1;
SteamUGC()->SetItemTags(handle, pTags);
SteamUGC()->AddItemKeyValueTag(handle, "test_key", "test_value");
std::string mod_directory = "/home/turupawn/ModExample";
SteamUGC()->SetItemContent(handle, mod_directory.c_str());
std::string preview_image = "/home/turupawn/ModExample/preview.png";
SteamUGC()->SetItemPreview(handle, preview_image.c_str());
std::string pchChangeNote = "This is a changelog";
SteamAPICall_t submit_item_call = SteamUGC()->SubmitItemUpdate(handle, pchChangeNote.c_str() );
m_SubmitItemUpdateResult.Set(submit_item_call, this,&CGameManager::onItemSubmitted);
}else
{
finished = true;
}
}
void CGameManager::onItemSubmitted(SubmitItemUpdateResult_t *pCallback, bool bIOFailure)
{
if(pCallback->m_eResult == k_EResultOK && !bIOFailure)
{
std::cout << "Item update submitted created!" << std::endl;
}
finished = true;
}
int main()
{
//First, let's check if the Steam client is running
if(SteamAPI_Init())
{
CGameManager gameManager;
//Now let's trigger the item creation process
gameManager.CreateItem();
//Dont forget to run the callbacks while we wait
while(!gameManager.finished)
{
SteamAPI_RunCallbacks();
}
}
std::cout << "Process finished" << std::endl;
return 0;
}
@twisezz
Copy link

twisezz commented Mar 25, 2022

Hi!
thank you very much!

This code is very helpful to me. It makes me well understand the usage of callback.

My code does not execute the "onitemsubmitted" function, so finished is always false. If I add finished = true under line 64, the application will work normally. Do you know how to solve this problem?

Greeting.
HL.

@Turupawn
Copy link
Author

@twisezz

Happy it helped. finished is just a flag I created to signal when all is over.

  1. CreateItem is called on line 87
  2. onItemCreated is callback returns
  3. If onItemCreated failed finished=true hence program stops, if onItemCreated succeded SubmitItemUpdate is called
  4. onItemSubmitted callback returns and finished=true stops the program

Maybe there is an issue updating the Item? That's why onItemSubmitted callback is never returned in step 4

But hey, if it's working I can't argue with that 😁

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment