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.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Question about String.Mid() in ZScript

Post by Nash »

Code: Select all

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...
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: Question about String.Mid() in ZScript

Post by _mental_ »

The first argument of Mid() is "LADItem".Length() which has integer type.
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Question about String.Mid() in ZScript

Post by Nash »

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
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Question about String.Mid() in ZScript

Post by Graf Zahl »

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
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Question about String.Mid() in ZScript

Post by Nash »

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
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Question about String.Mid() in ZScript

Post by Graf Zahl »

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.
Post Reply

Return to “Scripting”