playMaker

Author Topic: Append Int to a String  (Read 5867 times)

markinjapan

  • Full Member
  • ***
  • Posts: 103
Append Int to a String
« on: September 10, 2011, 08:13:54 PM »
So that you can rename instantiated gameobjects in order to be able to target them individually.

ie.

Bullet_01
Bullet_02
Bullet_03
etc

Thanks

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Append Int to a String
« Reply #1 on: September 11, 2011, 08:33:52 AM »
Hi,

 Yes, I too sometimes need that. Will see if I have time to come up with something. The problem with this is it has to be slightly more advanced then simply adding an int, we need to have the ability to use String.Format() system for true flexibilty.

http://answers.unity3d.com/questions/52411/float-to-string-c-unity-script-android-csharp.html

 bye,

 Jean

Satrio

  • Junior Playmaker
  • **
  • Posts: 67
Re: Append Int to a String
« Reply #2 on: September 11, 2011, 09:37:53 AM »
convert the INT to a string, use the the buildString action.. I use it to construct names and to add + and - to some strings to show damage..

or make "hard" duplicates of the Gameobjects and group under a null/gameobject called "bullets". then deactivate the group root. then activate them as needed?


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Append Int to a String
« Reply #3 on: September 14, 2011, 07:50:45 AM »
Hi,

 Ok, here we go:

I have put together a simple custom action using string.Format so that you can compose a string with an int in a very powerful way.

some good examples of what can be achieved:
http://www.csharp-examples.net/string-format-int/

Please find the code below

If you need this for floats, strings, etc, just shout or simply duplicate this action and change the Fsm type to match your need. A simple and good exercices if you want to start making custom actions.


Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.String)]
[Tooltip("Builds a String using Format() method for an int.")]
public class StringFormat : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.FsmString)]
public FsmString definition;

[UIHint(UIHint.FsmInt)]
public FsmInt theValue;

[UIHint(UIHint.Variable)]
public FsmString storeResult;

public bool everyFrame;

public override void Reset()
{
definition = null;
theValue = null;
storeResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoStringFormat();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoStringFormat();
}

void DoStringFormat()
{
if (storeResult == null) return;

storeResult.Value = string.Format(definition.Value,theValue.Value);
}

}
}


Bye,

 Jean