(solved) [zscript] partial string match?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

(solved) [zscript] partial string match?

Postby Jekyll Grim Payne » Wed Jan 09, 2019 4:51 am

I wonder if it's possible to match two string values partially, i.e. if a string contains "WOOD" in it, ignoring characters before and after?
Last edited by Jekyll Grim Payne on Thu Jan 10, 2019 2:49 am, edited 1 time in total.
User avatar
Jekyll Grim Payne
 
Joined: 21 Jul 2008

Re: [zscript] partial string match?

Postby _mental_ » Wed Jan 09, 2019 5:22 am

Sure, when String.IndexOf() returns positive number or zero.
_mental_
 
 
 
Joined: 07 Aug 2011

Re: (solved) [zscript] partial string match?

Postby Jekyll Grim Payne » Thu Jan 10, 2019 2:49 am

Thanks!
User avatar
Jekyll Grim Payne
 
Joined: 21 Jul 2008

Re: (solved) [zscript] partial string match?

Postby D2JK » Thu Jan 10, 2019 7:46 am

A question related to this... in the below code, I was wondering why it's necessary to use String.Format() in front of this function, considering that GetClassName() returns a string already:
Code: Select allExpand view
if ( String.Format(GetClassName()).IndexOf("Vile") )

instead of
Code: Select allExpand view
if ( GetClassName().IndexOf("Vile") )
D2JK
 
Joined: 30 Aug 2014

Re: (solved) [zscript] partial string match?

Postby phantombeta » Thu Jan 10, 2019 8:53 am

Because the wiki is wrong, actually. GetClassName returns a name, not a string, and names are ints generated from a string with a hashing algorithm (so that different strings get different numbers).

That said, using String.Format like that is janky IMO. It's abusing the fact that the compiler will implicitly cast names to strings. Which is fine, since apparently there's no explicit casting, but there's a much simpler, better and less performance-wasting way to do it:
Code: Select allExpand view
            string className = GetClassName ();
            if (className.IndexOf ("Vile")) 
User avatar
phantombeta
In the meadow of sinful thoughts, every flower's a perfect one
 
Joined: 02 May 2013
Location: The United Soviet Socialist Dictatorship of Hueland
Discord: phantombeta#2461
Twitch ID: phantombeta_

Re: (solved) [zscript] partial string match?

Postby D2JK » Thu Jan 10, 2019 1:39 pm

Ok, that's good to know, thanks. Actually, I'm aware of using a separate string variable like you showed, I just considered the code I wrote to be more compact. Didn't know it wastes performance, though.
D2JK
 
Joined: 30 Aug 2014


Return to Scripting

Who is online

Users browsing this forum: No registered users and 2 guests