Author Topic: Need help with array/array_construct  (Read 1710 times)

Offline Magus_BGforge

  • Planewalker
  • *****
  • Posts: 75
Need help with array/array_construct
« on: April 17, 2016, 08:19:40 AM »
Can anyone please clarify why the below code fails and how to make it work?
Specifically, why bif-res-cnt_0 can't be converted to an integer, while it was declared as an integer just a few lines before?

Code: [Select]
COPY_EXISTING ~chitin.key~ ~chitin.key~
  READ_LONG 0x10 bif-off
  READ_LONG 0x8 bif-cnt
  READ_LONG 0x14 res-off
  READ_LONG 0xc res-cnt

  FOR (j=0;j<bif-cnt;j+=1) BEGIN
    bif-res-cnt_j = 0
    PATCH_PRINT ~BIF %j% has bif-res-cnt_%j%=%bif-res-cnt_j% resources~
  END

  FOR (j=0;j<res-cnt;j+=1) BEGIN
    cur-off = res-off + j*0xe
    READ_LONG (cur-off+0xa) res-locator
    bif-index = res-locator BLSR 20
    PATCH_PRINT ~bif-index=%bif-index%~
    bif-res-cnt_bif-index += 1 //record number of resources in bif
  END

  FOR (j=0;j<bif-cnt;j+=1) BEGIN
    PATCH_PRINT ~BIF %j% has bif-res-cnt_%j%=%bif-res-cnt_j% resources~
  END

Output:
Code: [Select]
Copying and patching 1 file ...
BIF 0 has bif-res-cnt_0=0 resources
BIF 1 has bif-res-cnt_1=0 resources
...
BIF 181 has bif-res-cnt_181=0 resources
bif-index=0
ERROR: cannot convert bif-res-cnt_bif-index or %bif-res-cnt_bif-index% to an integer
ERROR: [chitin.key] -> [chitin.key] Patching Failed (COPY) (Not_found)
Stopping installation because of error.
« Last Edit: April 17, 2016, 01:17:27 PM by qwertyqwerty »

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Need help with array/array_construct
« Reply #1 on: April 17, 2016, 01:54:48 PM »
Your are making evaluation errors.
Code: [Select]
bif-res-cnt_bif-index += 1 //record number of resources in bif
This fails because you are trying to use the string-literal "bif-res-cnt_bif-index" as an integer, but it can't be evaluated to an integer. bif-index is not being evaluated when used like this. Also, you may think you are setting a variable "bif-res-cnt_0" to 0, but you are not. You setting "bif-res-cnt-j" to 0.

You are not making use of the array construct in that sense. Is it intentional, or is the documentation leading you astray?

Offline Magus_BGforge

  • Planewalker
  • *****
  • Posts: 75
Re: Need help with array/array_construct
« Reply #2 on: April 17, 2016, 02:10:27 PM »
Yes, I thought I was setting bif-res-cnt_0.
Doc says
SET ~x~ = ~var_%one%_%two%~
is equivalent to
SET ~x~ = ~var_1_sbar~
which is what I was trying to use.

Offline Magus_BGforge

  • Planewalker
  • *****
  • Posts: 75
Re: Need help with array/array_construct
« Reply #3 on: April 17, 2016, 02:19:16 PM »
All right, I think I got it working with
Code: [Select]
COPY_EXISTING ~chitin.key~ ~chitin.key~
  READ_LONG 0x10 bif-off
  READ_LONG 0x8 bif-cnt
  READ_LONG 0x14 res-off
  READ_LONG 0xc res-cnt

  FOR (j=0;j<bif-cnt;j+=1) BEGIN
    SET $bif-res-cnt(~%j%~) = 0
  END

  FOR (j=0;j<res-cnt;j+=1) BEGIN
    cur-off = res-off + j*0xe
    READ_LONG (cur-off+0xa) res-locator
    bif-index = res-locator BLSR 20
    SET $bif-res-cnt(~%bif-index%~) += 1
  END

  FOR (j=0;j<bif-cnt;j+=1) BEGIN
    cnt = $bif-res-cnt(~%j%~)
    PATCH_PRINT ~BIF %j% has %cnt% resources~
  END
2 questions, though:
1) Do I really have to use SET when assigning to an array contruct? Seems to fail otherwise.
2) Is there a way to use array contruct directly in PRINT, instead of proxying through another var, like in the code above (cnt)?

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Need help with array/array_construct
« Reply #4 on: April 17, 2016, 02:54:56 PM »
Yes, I thought I was setting bif-res-cnt_0.
Doc says
SET ~x~ = ~var_%one%_%two%~
is equivalent to
SET ~x~ = ~var_1_sbar~
which is what I was trying to use.
In the example they are, but what you were doing was not equivalent (notably, the reverse is not equivalent). I'll see if I can't rewrite the doc to be more to the point.

1) Yes. The implicit notation only takes string literals on the left-hand side.
2) No. PRINT actually takes game strings and not TP2 strings. A subset of TP2 strings are syntactically identical to a subset of game strings.
« Last Edit: April 17, 2016, 03:05:32 PM by Wisp »

Offline Magus_BGforge

  • Planewalker
  • *****
  • Posts: 75
Re: Need help with array/array_construct
« Reply #5 on: April 17, 2016, 03:54:49 PM »
I see, thanks

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: