Jump to content

Tutorial: NVSE4+ part 3: String variables


Recommended Posts

What is the context?

 

You could use string matching, so someone types in a request, like "9mm Pistol", and you search through the names of all items until you get a match, then add it.

 

ref rItem

string_var request

 

foreach entry <- possible_Items

    let rItem := *entry

    if eval (sv_find "request", $rItem) > -1 ; if we find the request in the name of rItem, add it

         AddItem rItem, 1

 

---

 

Need to talk to jaam about that bracket issue.

Link to comment

it seems a good solution, however this time I'm using lists and not arrays (which is so strange) so I have troubles to understand how to adapt that script. I think it's better if I PM you the specific example so you understand better.

Link to comment

Wrapping the call in parens is also helpful

 

if iInt == (GetType rSomeForm)

 

I think it really struggles sometimes without them to determine if the == is another param to the function in code like this:

if GetType rSomeForm == iInt
That shouldn't be a problem with straight operators but I think it is sometimes, or might be.
Link to comment

Sometimes it also struggles with them, giving you 'mismatching brackets' warnings, but dropping in an eval can help.

 

For the other question, there's no way I know of yet to construct editorID strings and make them refer to forms. Would be sweet. I think you can place formIDs between quotation marks and use those as forms, but that obviously invites load order headaches with non-vanilla forms.

Link to comment

You're talking about turning something like a hex string into a ref, right, the way the console does for you? All we need to do that is a str2int(). Ideally it would work on hex strings or ints, but even if it only works on ints, that would be 'ok'.

 

If that doesn't work directly when being assigned to a ref var, you could just use it with buildref.

Link to comment

Here's a completely untested strtoint. It may have bugs, but if so, they will be easy to fix.

 

 

scn StrToInt

string_var str
string_var char
array_var arParts
array_var element
int iMult
int ret

Begin Function{str}
  let iMult := 1

  ; does this work?  We want to split the string into individual chars
  let arParts := sv_split str ""
  
  foreach element <- arParts
    let ret := ret * iMult

    let char := element["value"]
    if char == "1"
      let ret := ret + 1
    elseif char == "2"
      let ret := ret + 2
    elseif char == "3"
      let ret := ret + 3
    elseif char == "4"
      let ret := ret + 4
    elseif char == "5"
      let ret := ret + 5
    elseif char == "6"
      let ret := ret + 6
    elseif char == "7"
      let ret := ret + 7
    elseif char == "8"
      let ret := ret + 8
    elseif char == "9"
      let ret := ret + 9
    endif
    
    let iMult := 10
  loop

  SetFunctionValue ret
End

 

 

Only works on normal decimal (base-10) values, but a hex version would not be difficult. This should allow something like:

 

let myInt := call StrToInt "123456"
let myMod := GetModIndex "somemod.esp"
let myRef := BuildRef MyMod MyInt
Link to comment

; does this work? We want to split the string into individual chars

let arParts := sv_split str ""

Not sure, but you can walk a string with foreach, the same as with an array, so we can skip parking the characters in an array first

 

foreach sv_character <- sv_originalstring

; etc

loop

 

I'm fairly certain I've seen Oblivion modders use "formID".dosomething in scripts before though, instead of using an editorID, although obviously your solution solves the load order problem. Would be nice though to be able to dynamically construct editorIDs and retrieve a form from them.

Link to comment

Oh I completely misinterpreted what you meant.. constructing an editorid string and turning that into a formid, yes, that would be very helpful indeed. What we need for that is basically some kind of "GetFormByName" function. I don't think this would be too difficult for NX or NVSE to do.

Link to comment
  • 2 months later...

In NVSE, an empty string_var evals to True, unlike most programming languages (Ruby can fuck off :P).

 

This is what Ar_Find returns (aka Ar_BadStringIndex) to signify "Not Found", if you store it in a string_var:

 

let str := ""

if eval str

    ; Empty string is true!

 

But, if you check the Ar_Find return directly, it seems to be False (urrggh).

 

if eval (Ar_Find "Not there", SomeArray)

    ; But, without an intermediary string_var, it is false

 

 

(editted to add second part)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. For more information, see our Privacy Policy & Terms of Use