Search Unity

[UnityScript][SteamWorks] Adding SteamWork into your UnityScript Project!

Discussion in 'Editor & General Support' started by VisualTech48, Aug 16, 2016.

  1. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Hello! Sorry if I placed this in the wrong section but I want to help people that code IN JS and have issues with connecting their game to SteamWorks

    Current Features:
    • Get Steam Stats
    • Set Steam Stats
    • Activate Achievements

    I'm here to help anyone that is posting his game to Steam, that has been coded with UnityScript, and has no knowledge, on how to add the SteamWorks.API into the game.

    As we know, UnityScript is decreasing, however still people use it, and till its officially disbanded from Unity, I'm sticking mostly do it, while learning C# and preparing for the inevitable.

    However, before that I wanted to help people to manage SteamWorks, Stats and Achievements and implent them into the game correctly.

    Phase_1

    Firstly, you will need do download the Steam.API C# project from here and follow the instructions for a successful installation:
    http://steamworks.github.io/installation/

    After you've imported the asset and changed the SteamID, you'll need to do a few things with its scripts, that you have just imported so this all can work. Why? Because we need C# scripts that are from the API to be compiled after JS, so we can work on them and connect them through our JS scripts.

    Before we go further, make a EMPTY object, and add SteamManager.CS to it.

    What you need to have is a Standard Assets folder in your Asset folder. Transfer the folder listed below into the Standard Assets folder:
    • Steamworks.net
    After that, you'll need to create a new C# script called "Steam_Events", that will act as a "tunnel" script, that I made to be able to control the primary functions of SteamWorks for us to use with our JS Scripts, and add it as well in the Standard Assets folder.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Steamworks;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class Steam_Events : MonoBehaviour {
    8.     //Overlay checker callback
    9.     //This is needed so we can check if steam overlay is lifed/activated
    10.     protected Callback<GameOverlayActivated_t> m_GameOverlayActivated;
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (SteamManager.Initialized) {
    14.             Debug.LogWarning("Loaded SteamManager!");
    15.         }
    16.     }
    17.  
    18.     //This enables the checking if the Steam Overlay is enabled.
    19.     //And the testing part to check for players how many are playing currently
    20.     private void OnEnable() {
    21.         if (SteamManager.Initialized) {
    22.             m_GameOverlayActivated = Callback<GameOverlayActivated_t>.Create(OnGameOverlayActivated);
    23.         }
    24.         if (SteamManager.Initialized) {
    25.             m_NumberOfCurrentPlayers = CallResult<NumberOfCurrentPlayers_t>.Create(OnNumberOfCurrentPlayers);
    26.         }
    27.     }
    28.     //This function handles what will happen when the overlay is lifed. My tactic is to set the timescale to 0 to pause everything.
    29.     private void OnGameOverlayActivated(GameOverlayActivated_t pCallback) {
    30.         if(pCallback.m_bActive != 0) {
    31.             Debug.Log("Steam Overlay has been activated");
    32.             Time.timeScale = 0;
    33.         }
    34.         else {
    35.             Debug.Log("Steam Overlay has been closed");
    36.             Time.timeScale = 1;
    37.         }
    38.     }
    39.     //This is a callresult that seeks the number of players, as listen in the C# SteamWorks documentation
    40.     private CallResult<NumberOfCurrentPlayers_t> m_NumberOfCurrentPlayers;
    41.  
    42.     //Not needed, just like the callresult but its useful for checking how many players are playing.
    43.     private void Update() {
    44.         if(Input.GetKeyDown(KeyCode.Space)) {
    45.             SteamAPICall_t handle = SteamUserStats.GetNumberOfCurrentPlayers();
    46.             m_NumberOfCurrentPlayers.Set(handle);
    47.             Debug.Log("Called GetNumberOfCurrentPlayers()");
    48.         }
    49.     }
    50.     //Function for checking how many players are playing
    51.     private void OnNumberOfCurrentPlayers(NumberOfCurrentPlayers_t pCallback, bool bIOFailure) {
    52.         if (pCallback.m_bSuccess != 1 || bIOFailure) {
    53.             Debug.Log("There was an error retrieving the NumberOfCurrentPlayers.");
    54.         }
    55.         else {
    56.             Debug.Log("The number of players playing your game: " + pCallback.m_cPlayers);
    57.         }
    58.     }
    59.     // Here begings my coding fully, where I made special functions to handle different types of things to tackle
    60.     //The UnlockAchive functions, unlocks an achivment, on calling
    61.     static public void UnlockAchive(string achievement){
    62.         if (SteamManager.Initialized) {
    63.             SteamUserStats.SetAchievement (achievement);
    64.             StoreStats ();
    65.         }
    66.     }
    67.     //This function is used to set Stats of your SteamWorks. Only works for floats currently, int is broken for handling
    68.     static public void SetStats(string StatusName, float Value, bool toINT){
    69.         if (SteamManager.Initialized) {
    70.             if(toINT){
    71.                 //int intData = (int)Value;
    72.                 //SteamUserStats.SetStat (StatusName, intData);
    73.             }
    74.             Debug.Log ("Setting stats of: " + StatusName + ", To: " + Value);
    75.             SteamUserStats.SetStat (StatusName, Value);
    76.         StoreStats ();
    77.         }
    78.     }
    79.     //This function is used to Get The Stats of a Stat, in your SteamWorks.
    80.     static public float GetStats(string StatusName){
    81.         float Stats;
    82.         SteamUserStats.GetStat (StatusName, out Stats);
    83.         return Stats;
    84.     }
    85.     //This function saves the stats, after being handled by
    86.     static public void StoreStats(){
    87.         SteamUserStats.StoreStats();
    88.     }
    89.     //Function to reset all the achivements and stats for you only
    90.     //You'll be using this function a lot, in order to test properly.
    91.     static public void Reset(){
    92.         SteamUserStats.ResetAllStats (true);
    93.     }
    94. }
    95.  
    Copy this and paste it into the script you just made and save it.

    Now to the easy part to use this script as your own in JS.
    This is pretty simple, as with the added STATIC functions you don't have to paste the script everywhere and without needing to reference it each time for the scene.

    To access our script we don't even have to add variables, just call functions. That is it.
    Example of setting an achievement in JS:
    Code (JavaScript):
    1. //TUTORIAL is an achievement added in the "Achievement Tab" in SteamWorks.
    2. //This function activates the achievement at the start of the script.
    3. function Start(){
    4.     Steam_Events.UnlockAchive("TUTORIAL");
    5. }
    Changing stats:
    Code (JavaScript):
    1. #pragma strict
    2. var Points = 0;
    3. //PLAYER_POINTS is an STAT added in the "STATS Tab" in SteamWorks.
    4. //Here we first GET PLAYER_POINTS, add 100, and call a function to SAVE IT
    5. function Start(){
    6.     Points = Steam_Events.GetStats("PLAYER_POINTS") + 100;
    7.     Steam_Events.SetStats("PLAYER_POINTS", Points, false);
    8. }
    *NOTE: Keep in mind TO HAVE STEAM OPENED in order to scripts to work. EACH TIME YOU ADD A CHANGE TO STEAMWORKS, AND PUBLISH IT, YOU HAVE TO OPEN STEAM, GO TO LIBARY, CLICK ON A FEW RANDOM GAMES, AND THEN CLICK BACK TO YOURS TO SEE IF THE ACHIVEMNTS HAVE BEEN UPDATED

    If you have any questions,
    Feel free to ask me.
     
    Ignacii and MattAuroch like this.
  2. Ignacii

    Ignacii

    Joined:
    May 23, 2013
    Posts:
    107
    It's been some weeks since you posted, I know lol, but just want to say thanks for helping me to implement better control over Steam's overlay!