Jump to content

variables in array names


subtledoctor

Recommended Posts

Before I lose hours and days to writing and testing and parse erroring, does anyone know if something like this will work:

COPY_EXISTING ~kitlist.2da~ ~override~
    COUNT_2DA_ROWS ~9~ "rows"
    FOR ( index = 2 ; index < rows ; index = index + 1 ) BEGIN
        READ_2DA_ENTRY %index% 5 9 kit_clab
        READ_2DA_ENTRY %index% 1 9 kit_code
        READ_2DA_ENTRY %index% 3 9 kit_name
        READ_2DA_ENTRY %index% 4 9 kit_desc
        READ_2DA_ENTRY %index% 0 9 kit_num
        READ_2DA_ENTRY %index% 8 9 class
        DEFINE_ASSOCIATIVE_ARRAY d5_deity_kits BEGIN "%kit_clab%" , "%kit_code%" , "%kit_name%" , "%kit_desc%" , "%kit_num%" => "%class%" END
    END
BUT_ONLY
ACTION_DEFINE_ASSOCIATIVE_ARRAY class_menu_3 BEGIN
    K_C_D , dwa => 3
    K_C_E , elf => 3
    K_C_G , gno => 3
    K_C_H , hum => 3
    K_C_HE , hum => 3
    K_C_HL , hal => 3
    K_C_HO , orc => 3
END
ACTION_DEFINE_ASSOCIATIVE_ARRAY class_menu_11 BEGIN
    K_D_D , dwa => 11
    K_D_E , elf => 11
    K_D_G , gno => 11
    K_D_H , hum => 11
    K_D_HE , hum => 11
    K_D_HL , hal => 11
    K_D_HO , orc => 11
END
ACTION_PHP_EACH d5_deity_kits AS hop => pop BEGIN
  ACTION_IF (%pop% = 3) OR (%pop% = 11) BEGIN
    ACTION_PHP_EACH class_menu_%pop% AS menu => class BEGIN
      ACTION_IF FILE_EXISTS_IN_GAME ~%menu%.2da~ BEGIN
        ACTION_IF (FILE_CONTAINS_EVALUATED (~%menu%.2da~ ~[ %TAB%]%hop_4%~)) BEGIN
          SPRINT $%hop_4%_races(~%menu_1%~) ~race~
        END
      END
    END
  END
  ...

Note, there are two things there:

 

1) ACTION_PHP_EACH class_menu_%pop% AS menu => class BEGIN

Using the val from iterations in the outer PHP_EACH routine to define which array should be checked in the inner routine.

 

2) "SPRINT $%hop_4%_races(~%menu_1%~) ~race~"

After evaluating the keys/values in the inner array, create a new array with names and keys taken from previously defined variables.

 

Basically this is meant to

1) Create a list of every kit in the game, identifying the class it belongs to and the row # in kitlist.2da, among other information;

2) Go through each such kit, see if it exists in the relevant K_x_y.2da file, and create a little sub-array with all valid races for that kit;

3) Then do various other things, which ultimately spits out a dialogue that will give the character a choice of kits to choose from that are valid for his/her race, class, and alignment.

 

It used to only check for class and alignment and that much works great; now I'm trying to add the race check with the K_x_y.2da files.

Link to comment

1) ACTION_PHP_EACH class_menu_%pop% AS menu => class BEGIN

Using the val from iterations in the outer PHP_EACH routine to define which array should be checked in the inner routine.

2) "SPRINT $%hop_4%_races(~%menu_1%~) ~race~"

After evaluating the keys/values in the inner array, create a new array with names and keys taken from previously defined variables.

 

Yes and yes.

The second construct may need an EVAL at rather unexpected place to evaluate the array name https://github.com/Gibberlings3/ItemRevisions/blob/master/item_rev/lib/store_rev_macros.tpa#L239

Link to comment

#1 seems to be failing with a parse error... I'll try to work around it and test #2.

 

It's giving me a parse error "near text: SPRINT." You have that SPRINT $ EVAL ~%etc.%~ language working in IR, and this is with Weidu v240, so I feel like maybe the parse error is somewhere before that line...?

 

EDIT - duh, it's an action, needs to be OUTER_SPRINT...

Link to comment

 

Is there any way to print an array?

You mean SPRINT ~variablename~ ?

 

No. I mean PRINT ~[array]~.

SPRINT $blah(~bleh~) ~blerg~
creates an array, but it doesn't show it to me. I want to SEE the contents, like this:

	C => 3
	D => 11
	P => 6
	R => 12
	CM => 14
	CR => 18
	CT => 15
	FC => 8
	FD => 16
	FMC => 17
Link to comment

No, it's just for troubleshooting. Just need to see if the various arrays I'm making are actually being populated correctly, to find the point of failure.. Thx.

Yeah, the reason why DOESN*T matter one bit, you still need to do the same cheese even if you just want to just "look".

Link to comment

For posterity's sake: I ended up going a bit of a different way, relying less on smashing multiple arrays against each other:

COPY_EXISTING ~kitlist.2da~ ~override~
    COUNT_2DA_ROWS ~9~ "rows"
    FOR ( index = 2 ; index < rows ; index = index + 1 ) BEGIN
        READ_2DA_ENTRY %index% 5 9 kit_clab
        READ_2DA_ENTRY %index% 1 9 kit_code
        READ_2DA_ENTRY %index% 3 9 kit_name
        READ_2DA_ENTRY %index% 4 9 kit_desc
        READ_2DA_ENTRY %index% 0 9 kit_num
        READ_2DA_ENTRY %index% 8 9 class
        DEFINE_ASSOCIATIVE_ARRAY d5_deity_kits BEGIN "%kit_clab%" , "%kit_code%" , "%kit_name%" , "%kit_desc%" , "%kit_num%" => "%class%" END
    END
BUT_ONLY

ACTION_PHP_EACH d5_deity_kits AS hop => pop BEGIN
  ACTION_IF (%pop% = 3) BEGIN
    OUTER_TEXT_SPRINT class_letter ~C~
  END
  ACTION_IF (%pop% = 11) BEGIN
    OUTER_TEXT_SPRINT class_letter ~D~
  END
  ACTION_IF (%pop% = 6) BEGIN
    OUTER_TEXT_SPRINT class_letter ~P~
  END
  ACTION_IF (%pop% = 12) BEGIN
    OUTER_TEXT_SPRINT class_letter ~R~
  END
  ACTION_IF (%pop% = 14) BEGIN
    OUTER_TEXT_SPRINT class_letter ~CM~
  END
  ACTION_IF (%pop% = 18) BEGIN
    OUTER_TEXT_SPRINT class_letter ~CR~
  END
  ACTION_IF (%pop% = 15) BEGIN
    OUTER_TEXT_SPRINT class_letter ~CT~
  END
  ACTION_IF (%pop% = 8) BEGIN
    OUTER_TEXT_SPRINT class_letter ~FC~
  END
  ACTION_IF (%pop% = 16) BEGIN
    OUTER_TEXT_SPRINT class_letter ~FD~
  END
  ACTION_IF (%pop% = 17) BEGIN
    OUTER_TEXT_SPRINT class_letter ~FMC~
  END
  ACTION_FOR_EACH race_letter IN ~D~ ~E~ ~G~ ~H~ ~HE~ ~HL~ ~HO~ BEGIN
    ACTION_IF FILE_EXISTS_IN_GAME ~K_%class_letter%_%race_letter%.2da~ BEGIN
      ACTION_IF (FILE_CONTAINS_EVALUATED (~K_%class_letter%_%race_letter%.2da~ ~[ %TAB%]%hop_4%~)) BEGIN
        OUTER_TEXT_SPRINT $~d5_kit_races~(~%race_letter%~) ~race~
      END
    END
  END
  ACTION_PHP_EACH d5_kit_races AS kitrace => val BEGIN
    ...
EDIT - probably should add a VARIABLE_IS_SET check there, to make sure the function doesn't choke on kits that don't appear in any K_C_H file...
Link to comment

 

 

Is there any way to print an array?

You mean SPRINT ~variablename~ ?
No. I mean PRINT ~[array]~.

Yeah, the fact is the array you call it, is a string variable in your function... so tell me why this wouldn't work ?

ACTION_PHP_EACH d5_deity_kits AS hop => pop BEGIN

SPRINT ~pop~

 

after which you put the ...

ACTION_IF (%pop% = 3) BEGIN

OUTER_TEXT_SPRINT class_letter ~C~

END

ACTION_IF (%pop% = 11) BEGIN

...

 

And if it's before the ACTION, aka you try to check the arrays composition then just use the same darn thing with six lines of this alike: SPRINT ~kit_clab~ ...

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...