Having two parameters

008

New Member
Reaction score
0
hi everyone,
At the moment, i have a command which is -gold x y
I want to be able to get the parameters x and y
Here is how i get x
function Trig_giveGold takes nothing returns nothing
local integer i
local integer a
loop
exitwhen ( "" = substring(GetEventPlayerChatString(), 7, 1)
a = a + substring(GetEventPlayerChatString(), 7, 1)

end loop
call DisplayTextToForce( GetPlayersAll(), a )
end function

Im new in jass

Thx.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
First you need to be sure that the player entered "-gold " and not some klaatu barada nikto/kakaduki stuff.

So you can do:

JASS:
local string cs = GetEventPlayerChatString()

//  012345
if "-gold " == SubString(cs, 0, 6) then
    // do the parsing of the arguments
    
    // if you fix/know the length of x and y then it won't be a problem
    //  012345678901
    // "-gold xx yy" // for x and y with length 2
    x = S2I(SubString(cs, 6, 8)
    y = S2I(SubString(cs, 9, 11)  

   // else you need to find the next space after the start position x
   // lets call it x_end
   x = S2I(SubString(cs, 6, x_end))

   // and then y would be just the rest of the string
   y = S2I(SubString(cs, x_end + 1, StringLength(cs)))
else
    // do nothing the player didn't write "-gold "
endif


Here are a few little string functions I like to use:
JASS:
function strlen takes string s returns integer
    return StringLength(s)
endfunction

function substr takes string s, integer start, integer end returns string
    return SubString(s, start, end)
endfunction

function strcase takes string s, boolean upper returns string
    if upper then
        return StringCase(s, true)
    else
        return StringCase(s, false)
    endif
endfunction

function strfind takes string source, string target, integer offset, boolean case_sens returns integer
    local integer i
    local integer source_len
    local integer target_len
    
    if not case_sens then
        set source = strcase(source, false)
        set target = strcase(target, false)
    endif
    
    set i = offset
    set source_len = strlen(source)
    set target_len = strlen(target)
    loop
        exitwhen i + target_len > source_len
        
        if substr(source, i, i + target_len) == target then
            return i
        endif
        
        set i = i + 1
    endloop

    return -1
endfunction

function strrep takes string source, string target, string replacement, boolean global returns string
    local integer target_start = 0
    local integer target_end = 0
    local string temp = ""
    local string result = ""

    if global then
        set target_end = strfind(source, target, 0, true)
        loop
            exitwhen target_end == -1
            
            set temp = substr(source, target_start, target_end)
            set result = result + temp + replacement
            
            set target_start = target_end + strlen(target)
            set target_end = strfind(source, target, target_start, true)
        endloop
        set result = result + substr(source, target_start, strlen(source))

    else
        set target_end = strfind(source, target, 0, true)
        
        if target_end != - 1 then
            set result = substr(source, 0, target_end) + replacement + substr(source, target_end + strlen(target), strlen(source))
        endif
    endif
        
    return result
endfunction


JASS:
The strfind function can be used to find the end of x(x_end) space after "-gold xxxxxxxxxx yyyyyyyyyy"
                                                                               this space ^ (the index of the " " character in cs) is x_end
int x_end = strfind(cs, " ", /*x_start*/ 6, false)
x = strsub(cs, 6, x_end)

this should work for any length of x

and then again y would be
y = strsub(cs, x_end + 1, strlen(cs))
 

Bribe

vJass errors are legion
Reaction score
67
JASS:
function strcase takes string s, boolean upper returns string
    if upper then
        return StringCase(s, true)
    else
        return StringCase(s, false)
    endif
endfunction


1. why are you giving these functions bj's

2. Why the above trash intead of [ljass]return StringCase(s, upper)[/ljass]
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
1. because... lower case function names are always nice ? =)
2. very good point +1 xD
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
If these weren't my libraries, I would still be linking to them:

stringFind

stringFilter

The documentation should explain it, but if you don't know the length of each string, you can basically use functions like FindFirstOf with the string and a space.

I'm not sure exactly what you're doing with the -gold command, but if you're intending to translate a player's name/number/color into an actual [ljass]player[/ljass], then you could try using my stringPlayer library as well, which is excellent for it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top