RPTools.net

Discussion and Support

Skip to content

It is currently Sun Nov 19, 2017 7:20 am 




Forum rules


PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)



Reply to topic  [ 6 posts ] 

Previous topic | Next topic 

  Print view

Author Message
 Offline
Dragon
 
Joined: Tue Apr 20, 2010 7:22 pm
Posts: 600
 Post subject: Evaluate if field is number or string
PostPosted: Wed Apr 26, 2017 10:19 am 
Is there a syntax that would tell me if a field is a number, a string expression that returns a number, or a just a regular string?

For example, the field could contain...

7
1d6+3
This is not a number

...and I'd like an expression that would return "7", eval(1d6+3), or "This is not a number".

I know that if I try "eval" on the 3rd line, it will throw an error, so that's out. IsNumber would work on the 1st, but not the second.

Any ideas?


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7947
Location: Bay Area
 Post subject: Re: Evaluate if field is number or string
PostPosted: Wed Apr 26, 2017 11:28 am 
Here's a summary of something I do.

Code:
[H: abort(input("expression||Enter an Expression|TEXT"))]
[H, if(json.isEmpty(expression)): value = 0; value = expression]
[H, if(! isNumber(value)): value = eval(value)]
[R: strformat("%{expression} = %{value}")]


So, as long as the expression evaluates to a number it should work fine. For example, if you enter "corn" and corn isn't defined it will ask you for the value.

Here's a snippet of an example I use with my attack macro where players can enter in their own attack string.

Code:
<!-- Temp Attack -->
[
H, if(json.isEmpty(tempATK)): tempATK 0]
[
HtempATK.total 0]
[
H, if(isNumber(tempATK)), code: {
   [
H, if(tempATK): attackTipOutput json.set(attackTipOutput,"Temp Attack",tempATK)]
   [
HtempATK.total tempATK]
   [
HtempATK.json tempATK]
};{
   [
HtempATK.json json.fromList(tempATK)]
   [
H, foreach(atk,tempATK.json), code: {
      [
Hindex roll.count +1]
      [
Hatk.name "Temp Attack "+index]
      [
H, if(isNumber(atk)): atk.value atkatk.value = eval(atk)]
      [
H, if(atk.value && isNumber(atk)): attackTipOutput json.set(attackTipOutput,atk.name,atk)]
      [
H, if(atk.value && ! isNumber(atk)): attackTipOutput json.set(attackTipOutput,atk.name,strformat("%{atk} = %{atk.value}"))]
      [
HtempATK.total tempATK.total atk.value]
   }]
}] 
 

_________________
Downloads:


Top
 Profile  
 
 Offline
Dragon
 
Joined: Tue Apr 20, 2010 7:22 pm
Posts: 600
 Post subject: Re: Evaluate if field is number or string
PostPosted: Wed Apr 26, 2017 3:46 pm 
Hmmm, that doesn't quite work for what I need.

if you enter "Corn", that's what I want to be outputted. Not another prompt for what that means...

In my "spell setup" section, there's a field for Damage. Works fine if they enter in 10 or 2d6, you get the right result broadcasted when the spell is cast. But I was thinking it would be nice if they players could use that field for non numeric stuff, so they could put in "Increases saves for 3 rounds" or something like that. There's a lot of "cast the spell, then go try to remember what that spell actually does". This was my idea as a workaround for that, without adding a new field to the json object for spells.


Top
 Profile  
 
 Offline
Dragon
 
Joined: Tue Apr 20, 2010 7:22 pm
Posts: 600
 Post subject: Re: Evaluate if field is number or string
PostPosted: Wed Apr 26, 2017 4:21 pm 
Got it

Code:
[H: abort(input("expression||Enter an Expression|TEXT"))]

[h : firstChar = substring(expression, 0, 1)]

[h : endResult = ""]

[h, if(isNumber(firstChar)), code : {
   [h : endResult = eval(string(expression))]
};
{
   [h : endResult = expression]
}]

[broadcast("end = " + endResult)]


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7947
Location: Bay Area
 Post subject: Re: Evaluate if field is number or string
PostPosted: Wed Apr 26, 2017 5:08 pm 
I think it will give an error if blank though for substring also if the first char is a number like for 0junk for an entry. If you're parsing another source it's possible stuff like that could happen.

_________________
Downloads:


Top
 Profile  
 
 Offline
Dragon
 
Joined: Tue Apr 20, 2010 7:22 pm
Posts: 600
 Post subject: Re: Evaluate if field is number or string
PostPosted: Wed Apr 26, 2017 6:59 pm 
It should never be blank, in the full macro, it turns a blank into a "0".

And yes someone could enter in "0haha i messed up your code"...at which point I will simply say, "garbage in, garbage out". :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 6 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:

Who is online

In total there are 4 users online :: 2 registered, 0 hidden and 2 guests (based on users active over the past 5 minutes)
Most users ever online was 243 on Sun Nov 04, 2012 6:14 am

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Style based on Andreas08 by Andreas Viklund

Style by Elizabeth Shulman