String springs

Status
Not open for further replies.
Level 10
Joined
Jan 28, 2009
Messages
442
This is for an inventory system that I am making. It uses the default 6-slot inventory, but the two top slots are constantly occupied by two undroppable Left/Right arrows, which you use to scroll through the inventory. I made it work with dummy units, but now I'm up for a more flexible sollution.

What I came up with was using strings to store items and charges in. The functions below would (if they worked) allow you to store 3 value types, allthough I only need 2 (item type and charges).

But, as I mentioned, the functions that are supposed to interpret these strings aren't working. It is the "reading order" that is bugged.
Has anyone got a bet on what I've done wrong in these functions?

JASS:
//=====Some conditioning..........first...=======================

function RunStringOr1 takes string s , integer i returns boolean
    if ( not ( SubStringBJ( s , i , i ) == "A" ) ) then
        return false
    endif
    return true
endfunction

function RunStringOr2 takes string s , integer i returns boolean
    if ( not ( SubStringBJ( s , i , i ) == "B" ) ) then
        return false
    endif
    return true
endfunction

function RunStringOr3 takes string s , integer i returns boolean
    if ( not ( SubStringBJ( s , i , i ) == "C" ) ) then
        return false
    endif
    return true
endfunction

function RunStringC takes string s, integer i returns boolean
    if ( RunStringOr1 (s, i) ) then
         call DisplayTextToForce( bj_FORCE_PLAYER[0], ( I2S( i) ) )
//==The text messages are just to keep track of where it is reading in the string
         return true
    endif
    if ( RunStringOr2 (s, i) ) then
         call DisplayTextToForce( bj_FORCE_PLAYER[0], ( I2S( i) ) )
         return true
    endif
    if ( RunStringOr2 (s, i) ) then
         call DisplayTextToForce( bj_FORCE_PLAYER[0], ( I2S( i) ) )
         return true
    endif
    return false
endfunction


//==This function below is supposed to skip string characters until it gets to one
// of the 3 letters A, B, or C. When it does get to one of those letters, it's 
//supposed to still remember how many characters back it were when it 
//started skipping, then convert the substring from x to y into an integer, 
//and set udg_Data to that integer. Afterwards it will run some function with 
//the RunFunc function. It does that finely. The only thing I can't make it, is
//go through the entire string and react to each letter once only.

function RunString takes string s , code a , code b , code c returns nothing
    local integer x = 1
    local integer y = 1
    local integer q = StringLength( s )
    set udg_Data = 0
    loop
         exitwhen x > q
         set y = x
         loop
                exitwhen y > q or RunStringC( s, y - 1 )
                set y = y + 1
         endloop
         set udg_Data = S2I( SubStringBJ( s, x, y - 1 ) )
         if (RunStringOr1( s, y - 1)) then
               call RunFunc( a )
         else
               if (RunStringOr2( s, y - 1 )) then
                     call RunFunc( b )
               else
                     if (RunStringOr3( s, y - 1 )) then
                           call RunFunc( c )
                     endif
               endif
         endif
         set x = y
    endloop
endfunction

JASS:
//=These are some test functions, just to give RunString something "relevant" to do, and keep track of it=

function TestA takes nothing returns nothing
     call DisplayTextToForce( bj_FORCE_PLAYER[0], ( "A" ) )
endfunction

function TestB takes nothing returns nothing
     call DisplayTextToForce( bj_FORCE_PLAYER[0], ( "B" ) )
endfunction

function TestC takes nothing returns nothing
     call DisplayTextToForce( bj_FORCE_PLAYER[0], ( "C" ) )
endfunction

  • Comment - (And this is how the execution looks)
  • Set DataString = 2295A143B8888C
  • Custom script: call RunString(udg_DataString, function TestA, function TestB, function TestC)
The udg_DataString you see above is just an example of a string I might want this function to read.
The result was: 5A9B

In different attempts, which I have confused out of my mind now, the results were different. I made it go through the entire string, but it triggered all the TestFunctions several times, while it's only supposed to be triggered once each time there is an A, B, or C.
 
Level 9
Joined
Dec 12, 2007
Messages
489
JASS:
function RunStringOr1 takes string s , integer i returns boolean
    if ( not ( SubStringBJ( s , i , i ) == "A" ) ) then
        return false
    endif
    return true
endfunction
this part could be optimized into:
JASS:
function RunStringOr1 takes string s , integer i returns boolean
    return (SubString(s, i-1, i) == "A")
endfunction
and this part:
JASS:
         loop
                exitwhen y > q or RunStringC( s, y - 1 )
                set y = y + 1
         endloop
         set udg_Data = S2I( SubStringBJ( s, x, y - 1 ) )
after the loop, aren't y-1 == q?

and last,
  • Comment - (And this is how the execution looks)
  • Set DataString = 2295A143B8888C
  • Custom script: call RunString(udg_DataString, function TestA, function TestB, function TestC)
The udg_DataString you see above is just an example of a string I might want this function to read.
The result was: 5A9B
is that result is what you want? or its a wrong result?
 
Last edited:
Level 10
Joined
Jan 28, 2009
Messages
442
Whoa...!

God I lack sleep! Okay case is closed haha. I thought I had the wrong result, but the result is correct, except the letter C is missing, but that's probably because, as you said, y-1 == q after the loop, or something else. I don't know but that's a minor thing that I'll fix. Thanks for improving it, and then waking me up. I better take a look at it after I've slept.
 
Status
Not open for further replies.
Top