Question about String.Mid() in ZScript

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

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.

Question about String.Mid() in ZScript

Postby Nash » Wed Oct 09, 2019 7:48 am

Code: Select allExpand view
class<Weapon> cls = "LADWeapon" .. String.Format("%s", character.eqWeapon.GetClassName()).Mid("LADItem".Length());
 


My question is about my usage of .Mid() here. How I place a string literal in there, and then append a .Length() to the right of it.

Is this supposed to work? I mean, it gives me the result that I expect, but... is this supposed to work? I find it weird that it works because according to .Mid's signature in base.zs, the first argument is supposed to be an int...
User avatar
Nash
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Github ID: nashmuhandes

Re: Question about String.Mid() in ZScript

Postby _mental_ » Wed Oct 09, 2019 1:55 pm

The first argument of Mid() is "LADItem".Length() which has integer type.
_mental_
 
 
 
Joined: 07 Aug 2011

Re: Question about String.Mid() in ZScript

Postby Nash » Wed Oct 09, 2019 2:52 pm

I'm aware that Length() returns an int :D ... I just wasn't sure it's okay to append the method to a string literal. Maybe I'm just inexperienced because I've never come across a programming language where you can call methods directly to the right of string literals.

(I would have assumed a string would have to be placed into a variable first before a method like .Length() could be called on it)
User avatar
Nash
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Github ID: nashmuhandes

Re: Question about String.Mid() in ZScript

Postby Graf Zahl » Thu Oct 10, 2019 3:39 am

In Objective-C you can call string methods on a literal so it's not something I invented. It was more or less an accident that it works and never planned.
User avatar
Graf Zahl
Lead GZDoom Developer
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: Question about String.Mid() in ZScript

Postby Nash » Thu Oct 10, 2019 4:06 am

Alright, see that was why I asked. :) Since they weren't intended; should I continue to use it, or should I move these strings to a variable? What's the likelihood of this not working in future updates?
User avatar
Nash
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Github ID: nashmuhandes

Re: Question about String.Mid() in ZScript

Postby Graf Zahl » Thu Oct 10, 2019 8:48 am

Considering that someone else already asked about functions that do NOT work when being used like this I think we can assume that this is already out in the wild.
User avatar
Graf Zahl
Lead GZDoom Developer
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany


Return to Scripting

Who is online

Users browsing this forum: Awario [RSS] and 3 guests