• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[General] String contains

Status
Not open for further replies.
Level 3
Joined
Sep 28, 2018
Messages
38
How to get arithmetic value out of strings with varying length?
ex:
-setHealth 500 or -setMana 200

The desire would be to capture and use the arithmetic value for both commands.

If the commands are the same length
ex: -setHth 500 and -setMna 10000
Then substring does the job but, if the strings have different lengths then substring won't work.

C# equivalent
Code:
msg.Substring(msg.IndexOf(" "), msg.Length);
This captures anything in the string after the space. How can this be done in WorldEdit?

Thanks!

P.S. Excuse the poor thread title, had in mind to ask if String.Contains() was a possibility.
 
Last edited:
Level 39
Joined
Feb 27, 2007
Messages
5,013
You need to manually parse the string character-by-character checking until you find the delimiter you're expecting (in this case a space). Then grab SubString(delimiterEndIndex+1, StringLength(theString)) JASS doesn't have an .IndexOf or .Contains analogue.
 
^that said, but you might have a look at some libraries, if they would help you:

Specified for commands:
they could be both help helpful, but work a bit differently.

There's also some general library, [Snippet] String, that has a method find (could be seen like the "contains" method).
 
Level 3
Joined
Sep 28, 2018
Messages
38
Thanks to the both of you. @Pyrogasm thanks! Was able to implement this. @IcemanBo the links are quite helpful, thank you! If I may ask another question, how do we get the currently selected unit for a Player when a chat message event is fired? Use case is when a user types a message, I want to be able to determine the unit they have selected at that time. Is this possible? The way I'm obtaining this right now is by running a separate script that stores selected units when a player selects a unit but, hoping there's a way to do this after a chat event as I figure constantly storing the currently selected unit is taxing on the system.
 
Level 39
Joined
Feb 27, 2007
Messages
5,013
how do we get the currently selected unit for a Player when a chat message event is fired?
JASS:
native GroupEnumUnitsSelected               takes group whichGroup, player whichPlayer, boolexpr filter returns nothing

//ex
local group g = CreateGroup()
call GroupEnumUnitsSelected(g, GetTriggerPlayer(), null) //putting null here actually leaks a boolexpr unless that was patched, instead you should actually use Filter(function SomeFuncThatReturnsTrue)
 
Level 3
Joined
Sep 28, 2018
Messages
38
The null boolexpr leak should be patched out in patch 1.24c:


@jvhgamer , to find out some how some things work, you can always try to find the GUI equivalent, and then convert it to jass to see how it's done! :)
@Pyrogasm Edit: Works great! @IcemanBo good to know. Switching between GUI and Jass has proved helpful to learn the syntax. My thanks to the both of you! Really appreciate the help. If I may be a bother one last time, is there a way to execute the event "Player(1)/Player(Red) types a chat message" as "A Player types a chat message"? Seems possible given we have (Triggering player) available in our actions but I'm not seeing how.

Use case being there are 24 players. It'd be very useful to have a single Player chat msg event as opposed to 24 of them.

Does this thread still hold true? Chat commands
 
Last edited:
Level 39
Joined
Feb 27, 2007
Messages
5,013
Nope, that event is player-specific. You could write your own function to add all 24 players in one line similar to TriggerRegisterAnyUnitEventBJ():

JASS:
function TriggerRegisterAnyUnitEventBJ takes trigger trig, playerunitevent whichEvent returns nothing
    local integer index

    set index = 0
    loop
        call TriggerRegisterPlayerUnitEvent(trig, Player(index), whichEvent, null)

        set index = index + 1
        exitwhen index == bj_MAX_PLAYER_SLOTS
    endloop
endfunction
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
The reason it is suggested to process chat messages 1 character at a time is that infrequently occurring unique strings can be considered a leak. Single character substrings will not produce many unique character strings and generally those it produces will reoccur frequently, and hence can not be considered a source of string leaks.
 
Status
Not open for further replies.
Top