Hi,
I have my action like this :
It works fine, but it looks a bit crowded.
In 'Score Type' and 'Second Type' you can select as string or as int using enum
The lists are arrays.
if possible i would want that the user can only place an array type 'string' or 'int'
in "Score List" and in "Seccond List"
(this way there is less chance having errors)
and then in the script to check if an array type 'string' or 'int' was placed.
(this way 'Score Type' and 'Second Type' are not needed)
I don't have a lot experience with coding yet but this is my code so far :
// (c) Copyright HutongGames, LLC 2010-2016. All rights reserved.
// Made by Djaydino http://www.jinxtergames.com
// Works with http://dreamlo.com/
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/
using UnityEngine;
using System.Collections;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Web")]
[Tooltip("gets highscores from Dreamlo website and place them in arrays")]
public class DreamloGetScoreToArray : FsmStateAction
{
public enum ScoreType
{
AsInt,
AsString
}
public enum SecondType
{
AsInt,
AsString
}
public enum SortType
{
ascending,
descending
}
public enum SortBy
{
score,
seconds,
date
}
[RequiredField]
[Tooltip("Place Public Code from the Dreamlo website")]
public FsmString publicCode;
[UIHint(UIHint.Variable)]
[Tooltip("Store the value in an array.")]
public FsmArray nameList;
[UIHint(UIHint.Variable)]
[Tooltip("Store the value in an array.")]
public FsmArray scoreList;
public ScoreType scoreType;
[UIHint(UIHint.Variable)]
[Tooltip("Store the value in an array.")]
public FsmArray secondsList;
public SecondType secondType;
[UIHint(UIHint.Variable)]
[Tooltip("Store the value in an array.")]
public FsmArray textList;
[UIHint(UIHint.Variable)]
[Tooltip("Store the value in an array.")]
public FsmArray dateAndTimeList;
[Tooltip("here you can choose the size of your list for example 10 will give the top 10 scores, leave to none or 0 to get all scores")]
public FsmInt ListSize;
public SortType sortType;
public SortBy sortBy;
public FsmEvent isError;
[Tooltip("Where any errors thrown will be stored. Set this to a variable, or leave it blank.")]
public FsmString errorMessage = "";
private WWW www;
private Coroutine routine;
private string webUrl;
private string setSaveType;
public override void Reset()
{
scoreType = ScoreType.AsInt;
secondType = SecondType.AsInt;
sortType = SortType.descending;
publicCode = null;
ListSize = null;
nameList = null;
scoreList = null;
secondsList = null;
textList = null;
dateAndTimeList = null;
errorMessage = "";
}
public override void OnEnter()
{
switch (sortBy)
{
case SortBy.score:
break;
case SortBy.seconds:
setSaveType = setSaveType + "-seconds";
break;
case SortBy.date:
setSaveType = setSaveType + "-date";
break;
}
switch (sortType)
{
case SortType.descending:
break;
case SortType.ascending:
setSaveType = setSaveType + "-asc";
break;
}
setSaveType = "pipe" + setSaveType;
if (ListSize == null || ListSize.Value == 0)
{
webUrl = "http://dreamlo.com/lb/" + publicCode.Value + "/" + setSaveType;
}
else
{
webUrl = "http://dreamlo.com/lb/" + publicCode.Value + "/" + setSaveType + "/" + ListSize;
}
routine = StartCoroutine(UploadNewHighscore());
}
private IEnumerator UploadNewHighscore()
{
www = new WWW(webUrl);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
FormatHighscores(www.text);
}
else
{
errorMessage = www.error;
Fsm.Event(isError);
}
}
void FormatHighscores(string textStream)
{
string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i <entries.Length; i ++)
{
string[] entryInfo = entries[i].Split(new char[] {'|'});
string username = entryInfo[0];
nameList.Resize(nameList.Length+1);
nameList.Set(nameList.Length-1, username);
if (scoreType == ScoreType.AsInt)
{
int scoreInt = int.Parse(entryInfo[1]);
scoreList.Resize(scoreList.Length+1);
scoreList.Set(scoreList.Length-1, scoreInt);
}
else
{
string score = entryInfo[1];
scoreList.Resize(scoreList.Length+1);
scoreList.Set(scoreList.Length-1, score);
}
if (secondType == SecondType.AsInt)
{
int secondsInt = int.Parse(entryInfo[2]);
secondsList.Resize(secondsList.Length+1);
secondsList.Set(secondsList.Length-1, secondsInt);
}
else
{
string seconds = entryInfo[2];
secondsList.Resize(secondsList.Length+1);
secondsList.Set(secondsList.Length-1, seconds);
}
string text = entryInfo[3];
textList.Resize(textList.Length+1);
textList.Set(textList.Length-1, text);
string dateAndTime = entryInfo[4];
dateAndTimeList.Resize(dateAndTimeList.Length+1);
dateAndTimeList.Set(dateAndTimeList.Length-1, dateAndTime);
}
Finish();
}
public override void OnExit()
{
StopCoroutine(routine);
}
}
}