{steamAPI Registercallresult} 1
SteamAPI Registercallresult
SteamAPI Registercallresult
SteamAPI Registercallresult is a function that allows you to register a callback handler for a specific call result type in the Steamworks API. A callback handler is a function or a method that is executed when a callback is triggered. A call result type is a unique identifier that corresponds to a specific request to the Steam server, such as SteamUserStats()->RequestCurrentStats().
{steamAPI registercallresult} 1
In this article, we will explain how to use SteamAPI Registercallresult to handle call results in Steamworks. We will also show you some examples of how to use it in different languages and frameworks.
What is SteamAPI Registercallresult?
SteamAPI Registercallresult is a function that allows you to register a callback handler for a specific call result type. The syntax of SteamAPI Registercallresult is as follows:
void SteamAPI_RegisterCallResult ( CCallbackBase *pCallback, SteamAPICall_t hAPICall );
The parameters are:
pCallback: A pointer to an instance of CCallbackBase or a subclass of it. This is the callback handler that will be executed when the call result is triggered. You need to override the Run and GetCallbackSizeBytes methods. The Run method is where you write your logic for handling the call result. The GetCallbackSizeBytes method returns the size of the callback data structure.
hAPICall: A handle to the API call that corresponds to the call result type. This is returned by the function that makes the request to the Steam server.
The function returns nothing.
How does SteamAPI Registercallresult work?
SteamAPI Registercallresult works by storing the callback handler and the API call handle in an internal map. When the Steam client receives a response from the Steam server, it checks if the response matches any of the stored API call handles. If it does, it executes the corresponding callback handler and passes it the response data as a parameter.
To use SteamAPI Registercallresult, you need to do the following steps:
Create an instance of CCallbackBase or a subclass of it. This is your callback handler.
Make a request to the Steam server using one of the functions that return an API call handle, such as SteamUserStats()->RequestCurrentStats(). Store the returned handle in a variable.
Call SteamAPI Registercallresult with your callback handler and the API call handle as parameters.
Wait for the callback to be triggered. When it does, your callback handler will be executed with the response data as a parameter.
Unregister your callback handler by calling SteamAPI_UnregisterCallResult with the same parameters as before.
Examples of using SteamAPI Registercallresult
Here are some examples of how to use SteamAPI Registercallresult in different languages and frameworks:
C++
In C++, you can use the CCallResult class template to create your callback handler. This class inherits from CCallbackBase and automatically handles registration and unregistration of callbacks. You just need to provide a function or a method pointer as a template parameter and override the OnRunCallback method. For example, if you want to handle the UserStatsReceived_t call result type, which is triggered by SteamUserStats()->RequestCurrentStats(), you can do something like this:
// Define a class that inherits from CCallResult class CMyUserStatsReceived : public CCallResult<UserStatsReceived_t> public: // Override the OnRunCallback method void OnRunCallback(UserStatsReceived_t *pParam) // Write your logic for handling the call result here // For example, print some information from pParam printf("Game ID: %llu\n", pParam->m_nGameID); printf("Result: %d\n", pParam->m_eResult); printf("User ID: %llu\n", pParam->m_steamIDUser.ConvertToUint64()); ; // Create an instance of your class CMyUserStatsReceived myUserStatsReceived; // Make a request to the Steam server SteamAPICall_t hCall = SteamUserStats()->RequestCurrentStats(); // Register your callback handler with the API call handle myUserStatsReceived.SetGameserverFlag(false); // Optional, set to true if you are a game server myUserStatsReceived.Set(hCall, &myUserStatsReceived, &CMyUserStatsReceived::OnRunCallback); // Wait for the callback to be triggered // When it does, your OnRunCallback method will be executed // Unregister your callback handler // This is done automatically by the CCallResult destructor
C#
In C#, you can use the CallResult<T> class template to create your callback handler. This class inherits from CallbackBase and automatically handles registration and unregistration of callbacks. You just need to provide a delegate as a template parameter and assign it to the Action property. For example, if you want to handle the UserStatsReceived_t call result type, which is triggered by SteamUserStats()->RequestCurrentStats(), you can do something like this:
// Define a delegate that matches the signature of the call result type public delegate void OnUserStatsReceived(UserStatsReceived_t pCallback); // Create an instance of CallResult with your delegate as a template parameter CallResult<OnUserStatsReceived> myUserStatsReceived = new CallResult<OnUserStatsReceived>(); // Assign a method or a lambda expression to the Action property myUserStatsReceived.Action += (UserStatsReceived_t pCallback) =>
// Write your logic for handling the call result here // For example, print some information from pCallback Console.WriteLine("Game ID: 0", pCallback.m_nGameID); Console.WriteLine("Result: 0", pCallback.m_eResult); Console.WriteLine("User ID: 0", pCallback.m_steamIDUser); ; // Make a request to the Steam server SteamAPICall_t hCall = SteamUserStats()->RequestCurrentStats(); // Register your callback handler with the API call handle myUserStatsReceived.SetGameserverFlag(false); // Optional, set to true if you are a game server myUserStatsReceived.Set(hCall); // Wait for the callback to be triggered // When it does, your Action method or lambda expression will be executed // Unregister your callback handler // This is done automatically by the CallResult destructor
Unity
In Unity, you can use the Steamworks.NET plugin to access the Steamworks API. This plugin provides wrapper classes for the Steamworks API functions and types. You can use the CallResult<T> class template to create your callback handler. This class inherits from CallbackBase and automatically handles registration and unregistration of callbacks. You just need to provide a delegate as a template parameter and assign it to the m_Func property. For example, if you want to handle the UserStatsReceived_t call result type, which is triggered by SteamUserStats()->RequestCurrentStats(), you can do something like this:
// Define a delegate that matches the signature of the call result type public delegate void OnUserStatsReceived(UserStatsReceived_t pCallback, bool bIOFailure); // Create an instance of CallResult with your delegate as a template parameter CallResult<OnUserStatsReceived> myUserStatsReceived = new CallResult<OnUserStatsReceived>(); // Assign a method or a lambda expression to the m_Func property myUserStatsReceived.m_Func = (UserStatsReceived_t pCallback, bool bIOFailure) =>
// Write your logic for handling the call result here // For example, print some information from pCallback Debug.Log("Game ID: " + pCallback.m_nGameID); Debug.Log("Result: " + pCallback.m_eResult); Debug.Log("User ID: " + pCallback.m_steamIDUser); ; // Make a request to the Steam server SteamAPICall_t hCall = SteamUserStats()->RequestCurrentStats(); // Register your callback handler with the API call handle myUserStatsReceived.SetGameserverFlag(false); // Optional, set to true if you are a game server myUserStatsReceived.Set(hCall); // Wait for the callback to be triggered // When it does, your m_Func method or lambda expression will be executed // Unregister your callback handler // This is done automatically by the CallResult destructor
Conclusion
SteamAPI Registercallresult is a useful function that can simplify your code and make it easier to handle callbacks in Steamworks. However, you should be careful not to use it for API calls that return multiple values or fire multiple times, such as SteamMatchmaking()->RequestLobbyList(). In In this section, we will explain why you should not use SteamAPI Registercallresult for API calls that return multiple values or fire multiple times. We will also show you some alternatives that you can use instead. Why not use SteamAPI Registercallresult for API calls that return multiple values or fire multiple times? SteamAPI Registercallresult is designed to handle API calls that return a single value and fire only once. For example, SteamUserStats()->RequestCurrentStats() returns a UserStatsReceived_t structure and fires only when the request is completed. However, some API calls return multiple values or fire multiple times. For example, SteamMatchmaking()->RequestLobbyList() returns an array of LobbyMatchList_t structures and fires every time a new lobby is found. If you use SteamAPI Registercallresult for these API calls, you will encounter some problems: - You will only receive the first value or the first firing of the callback. The subsequent values or firings will be ignored. - You will not be able to unregister your callback handler until the API call is complet