Jump to content

How to convert a string-like int variable to a real int variable?


c4_angel

Recommended Posts

Posted

Hello!

 

Some int values are stored like a string in 2da files, e.g. UNSUABLE in KITLIST.2da(in another word, cannot use a SET after READ_2DA_ENTRY). Is there any way to make it work?

 

Thank you!

Posted

One of the absolute laziest ways you could work around this would be something like

OUTER_SPRINT string_thats_actually_an_int 0x40000000
<<<<<<<< ./inlined.tpa
OUTER_SET new_var_thats_an_int = %string_thats_actually_an_int%
>>>>>>>>
COPY ~./inlined.tpa~ ~./inlined.tpa~  EVALUATE_BUFFER
REINCLUDE ~./inlined.tpa~

COPY_EXISTING ~sw1h01.itm~ ~override~
  WRITE_LONG 0x08 new_var_thats_an_int

Someone not as lazy as I am could come up with a nifty macro to do it.

Posted

Or use the less known SPRINTF command:

OUTER_TEXT_SPRINT myString ~0xff~
OUTER_PATCH ~~ BEGIN
  // currently only available as patch command
  SPRINTF myDecNum ~%d~ (~%myString%~)  // hex number as string -> decimal number
  SET myDecNum = myDecNum / 2           // myDecNum is treated as number
  SPRINTF myHexNum ~%x~ (~%myDecNum%~)  // decimal number -> hex number as string
END
PRINT ~myString = %myString%, myDecNum = %myDecNum%, myHexNum = %myHexNum%~
// Output: myString = 0xff, myDecNum = 127, myHexNum = 0x7f
Posted (edited)

Jarno wins. The only distinction TP2 makes between stings and integers are which strings are not integers after coercion.

 

This works fine:

OUTER_SPRINT myvar "123"

OUTER_SET myothervar = myvar

COPY_EXISTING sw1h01.itm override
  WRITE_LONG NAME1 myvar

I hate to pick nits but... CamDawg,

%string_thats_actually_an_int% is exactly equivalent to "string_thats_actually_an_int". On their own, percentage signs are string delimiters. Inside strings, they delimit variables, but only inside strings.

Edited by Wisp
Posted

 

I hate to pick nits but... CamDawg,

%string_thats_actually_an_int% is exactly equivalent to "string_thats_actually_an_int". On their own, percentage signs are string delimiters. Inside strings, they delimit variables, but only inside strings.

No worries, always something to learn. After all Fixpack is but a collection of fixes wrapped in picked nits.
Posted (edited)

I hate to pick nits but... CamDawg,

%string_thats_actually_an_int% is exactly equivalent to "string_thats_actually_an_int". On their own, percentage signs are string delimiters. Inside strings, they delimit variables, but only inside strings.

I could swear I'v seen cases when it wouldn't evaluate properly without percentages, sometimes double percentages. Edited by Ardanis
Posted (edited)

I could swear I'v seen cases when it wouldn't evaluate properly without percentages, sometimes double percentages.

Are you sure you weren't placing a string, into another ? As in, you place a variable that can change according to what game it's modified(BGT, Tutu etc). For example all the items they have, as the Tutu has a _ -before nearly all of the .itm file names. Which happens to be %tutu_var% in code, or whatever. Edited by Jarno Mikkola
Posted (edited)

 

I hate to pick nits but... CamDawg,

%string_thats_actually_an_int% is exactly equivalent to "string_thats_actually_an_int". On their own, percentage signs are string delimiters. Inside strings, they delimit variables, but only inside strings.

I could swear I'v seen cases when it wouldn't evaluate properly without percentages, sometimes double percentages.

 

 

+1

but I can't explain why and in which cases...

Edited by Gwendolyne
Posted

 

 

I hate to pick nits but... CamDawg,

%string_thats_actually_an_int% is exactly equivalent to "string_thats_actually_an_int". On their own, percentage signs are string delimiters. Inside strings, they delimit variables, but only inside strings.

I could swear I'v seen cases when it wouldn't evaluate properly without percentages, sometimes double percentages.

 

+1

but I can't explain why and in which cases...

 

Double percentages are needed to evaluate content of evaluated variable names, e.g.

OUTER_TEXT_SPRINT myVar ~Hello World!~
OUTER_TEXT_SPRINT varName ~myVar~
OUTER_TEXT_SPRINT result EVAL ~%%varName%%~ // result = ~Hello World!~
Posted

Jarno wins. The only distinction TP2 makes between stings and integers are which strings are not integers after coercion.

This is one of the few things I knew. The only surprising thing is that the point goes to Jarno...

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...