Skip to content

Composing names of variables

Given I have several constants of the <string> type and the names range cXYZ_0 to cXYZ_10 (or any other number).
I want to have a <for> loop that returns an incrementing <int> combines the incremented number with the truncated name of my constant "cXYZ_" to return the value of that constant to another string variable. Or directly into a system function, like a message.

I tried multiple and various variations of the following, but to no avail. Any ideas?
	// or any other combination of operators I could think of, even simply +(i)
	sSkill = (cSKILL_+IntToString(i));
        SendMessageToPC(oPC, "You have a skill level of "+s+" in "+sSkill+".");

Adressing the constant directly works, btw.
	SendMessageToPC(oPC, "You have a skill level of "+s+" in "+cSKILL_1+".");

Comments

  • MelkiorMelkior Member Posts: 182
    edited December 2023
    As I understand it, NWNScript doesn't have a way to directly do what you want done.
    The best you can do is to store the variables onto an object, and then access them using GetLocalInt/Float/String since you can use a string variable to access variables stored on an object. You just can't use string variables to access other types of variables.
    So it would be more like:
    sSkill = (cSKILL_+IntToString(i));
    sSkillName=GetLocalString(GetObjectByTag("StorageThing"),sSkill);
    SendMessageToPC(oPC, "You have a skill level of "+s+" in "+sSkillName+".");
    
    That code presumes that you've previously created and tagged StorageThing and have added the appropriate variables to it.
    If you're storing the variables on the PC, that would work but variables stored on the PC are wiped by a module reload. If you want them to be permanent, you'd need to put them on the PCs skin, or better than that, on an undroppable item carried by the PC.
Sign In or Register to comment.