scripting_poster

Posters

Posters classes have to be inherited from the HTSA class BasePoster. The BasePoster class just describes a generic poster item, which loads two graphic resources (thumb and full image) and displays them in game. The Item is composed of two parts, a data part structured in json format and sitting int the

C:\Users\{YOUR USERNAME}\AppData\LocalLow\RadicalFiction\HARDTIMES\Mods\{YOUR MOD NAME}_MODPKG\Posters

folder (you can modify this location using the main mod's descriptor.json file). There you will just put the json file which will reference the class to use when dealing with a poster. When you create a new poster class you will have to reference it in your posters jsons to associate it with your new posters.
For example:

{
  "posterId": "mod_poster",
  "posterType": "MYModModPoster", //THIS REFERENCES YOUR POSTER CLASS
  "posterSpriteName": "full_sized_image.png",
  "posterThumbSpritename" : "image_thumb.png",
  "extras":[
    {
		"key":"exampleExtraProperty",
		"stringValue": "Example extra property value"
    },
    {
		"key":"anotherExampleExtraProperty",
		"intValue": 10
    }
  ]
}

The BasePoster class must be extended by any custom poster class.
Extended custom poster classes are to be saved in the

C:\Users\{YOUR USERNAME}\AppData\LocalLow\RadicalFiction\HARDTIMES\Mods\{YOUR MOD NAME}_MODPKG\Scripts\Posters

folder for the game engine to load them at bootstrap.
Remember that mods loading and initialization is invoked at bootstrap time, so players or gamemanagers are not yet instantiated.
There are two methods that can be overridden in this class to give you the ability to define your custom poster class behaviour:

  • public virtual void InitPoster(BasePoster baseItemData)
  • public virtual void Prepare(GameObject posterContainer)

It is called by the Hard Times engine when initializing the poster for usage. You can use it for init activities, loading data, etc.
It's mandatory for you to call the base.InitPoster(baseItemData); method at the beginning of your code to ensure the poster will be loaded properly.
Example:

public override void InitPoster(BasePoster baseItemData)
{
    base.InitPoster(baseItemData);
    //do whatever you need to initialize your poster
}

Also this function will be called by the game engine just before showing your poster in full size. It will receive a reference to the posterContainer, which is the Game Object your poster will be showed inside. You can use it to manipulate it, spawn ui elements inside it, etc.
It's mandatory for you to call the base.Prepare(posterContainer); method at the beginning of your code to ensure the poster will be loaded properly.
Example:

public override void Prepare(GameObject posterContainer)
  {        
      base.Prepare(posterContainer);
      //get a reference to the PlayerManager
      PlayerManager playerManager = GameManager.instance.players[0];
        
      //create a custom text field to hold some data
      UnityEngine.GameObject countDownFieldGo = new UnityEngine.GameObject();
      countDownFieldGo.transform.SetParent(posterContainer.transform);
      RectTransform countdownRect = countDownFieldGo.AddComponent<RectTransform>();
      countdownRect.localScale = new Vector3(1f, 1f, 1f);
      countdownRect.anchoredPosition = new Vector3(0f,-320f,0);
      countdownRect.sizeDelta = new Vector2(500f, 75f);
      countdownRect.pivot = new Vector2(0.5f, 0.5f);

      customField = countDownFieldGo.AddComponent<Text>();
      customField.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
      customField.fontStyle = FontStyle.Normal;
      customField.fontSize = 29;
      customField.lineSpacing = 1;
      customField.supportRichText = true;
      customField.alignment = TextAnchor.MiddleCenter;
      customField.alignByGeometry = true;
      customField.color = Color.black;
      customField.raycastTarget = false;

      //fill textfield with custom data about the player
      customField.text = LocalizationManager.GLV("Hi,") + playerManager.playerName + System.Environment.NewLine;
      customField.text += LocalizationManager.GLV("Your health is: ")+playerManager.healthManager.statValue.toString();
  }

The above example will just show off a poster (defined in the posterSpriteName property of the json poster file), and then will implement a custom behaviour deined in the Prepare method of the custom poster class, which is creating a brand new text field into the poster and is filling it with data about the player.

The finished custom poster class would look like that:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MYModModPoster : BasePoster
{
    public Text customField;

    public override void InitPoster(BasePoster baseItemData)
    {
        base.InitPoster(baseItemData); 
    }


    public override void Prepare(GameObject posterContainer)
    {        
        base.Prepare(posterContainer);
        //get a reference to the PlayerManager
        PlayerManager playerManager = GameManager.instance.players[0];
        
        //create a custom text field to hold some data
        UnityEngine.GameObject countDownFieldGo = new UnityEngine.GameObject();
        countDownFieldGo.transform.SetParent(posterContainer.transform);
        RectTransform countdownRect = countDownFieldGo.AddComponent<RectTransform>();
        countdownRect.localScale = new Vector3(1f, 1f, 1f);
        countdownRect.anchoredPosition = new Vector3(0f,-320f,0);
        countdownRect.sizeDelta = new Vector2(500f, 75f);
        countdownRect.pivot = new Vector2(0.5f, 0.5f);

        customField = countDownFieldGo.AddComponent<Text>();
        customField.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
        customField.fontStyle = FontStyle.Normal;
        customField.fontSize = 29;
        customField.lineSpacing = 1;
        customField.supportRichText = true;
        customField.alignment = TextAnchor.MiddleCenter;
        customField.alignByGeometry = true;
        customField.color = Color.black;
        customField.raycastTarget = false;

        //fill textfield with custom data about the player
        customField.text = LocalizationManager.GLV("Hi,") + playerManager.playerName + System.Environment.NewLine;
        customField.text += LocalizationManager.GLV("Your health is: ")+playerManager.healthManager.statValue.toString();
    }
}


As you see it is possible to develop a custom poster class which could display any kind of informations or images, data or whatever.
This is still an experimental mode, being developed and can be subject to change.

If you have any kind of question, problem or if you find errors in this guide please get in touch at info@tempi-duri.net.
Thanks!

You could leave a comment if you were logged in.
  • scripting_poster.txt
  • Last modified: 2019/10/20 11:11
  • by thorfinio