Adding two integers together

This is going to sound super basic, I realize, but I have been having this issue in several scripts I’ve written recently and I want to think it’s just me being ignorant of something fundamental… But for the life of me I don’t know what it is.

I have a very simple line adding two integers: SetLocalInt(oPC, “”+sResearchType+"", iCurrentSkill+iXPGain);

This is what they look like.
int iXPGain = GetLocalInt(oItem, “XP Gain”)*iStatModifier;
int iCurrentSkill = GetLocalInt(oPC, “”+sResearchType+"");

It adds these numbers and writes them as the new skill level, but it does so in such a way requiring me to run the script again to ‘finalize’ the results. If I’m adding 1 and 1, I’ll get 0, then 2 on a second iteration of the script.

Example:

Am I missing something, or is there some better way to formulate these sorts of operations?

have you ever tried the following?

int iXPGain = GetLocalInt(oItem, “XP Gain”)*iStatModifier;
int iCurrentSkill = GetLocalInt(oPC, sResearchType);
SetLocalInt(oPC, sResearchType, iCurrentSkill+iXPGain);

I have, yes. In a different context, but the same concept. It still gives me that delay.

In general: You don’t need to add " around a string variable
With my simple script the variable sResearchType^ should raise on every usage of the book (I guess, it’s executed by the usage of the book)
Delay?? the usage of an item is restricted to once per turn. Actually the usage of potions should follow the same rule.

It would help if you showed the code in context rather than 2 snippets which obviously are not in the right order. There is no delay in setting/using local vars, nor any need to “finalize” anything. iStatModifier and sResearchType initialization too could help. There’s nothing visible with what we have that will make 1 and 1 = 0.

As Mmat says you don’t need the “”+s around the variable name variable. But that should not make a difference, just probably cost a few extra instructions (I don’t think the compiler is smart enough to optimize that away) .

So here is the code. The reason I was using +sResearchType+ was that I’m trying to make this into a template system that gets the variables from the items instead of having to have each research type coded into its own bespoke script. I’m still undecided on which ones will be made at this point.

I’m aware that writing variables to a PC in a multiplayer environment isn’t a good idea, this is still a prototyping script and it’ll be pointing at a database item on the PC in the future. This script is called very rarely.

void main()
{
object oPC = GetItemActivator();
object oItem = GetItemActivated();

string sResearchType = GetLocalString(oItem, "ResearchType");
string sStationBlueprint = GetLocalString(oItem, "StationBlueprint");
string sBlueprintDescription = GetLocalString(oItem, "BlueprintDescription");

int iActiveResearch = GetLocalInt(oPC, "ActiveResearch");
int iBasicResearchComplete = GetLocalInt(oPC, "BasicResearch"+sResearchType+"Complete");

int iStatModifier = GetAbilityModifier(ABILITY_INTELLIGENCE, oPC)+GetAbilityModifier(ABILITY_WISDOM, oPC)/2;
int iXPGain = GetLocalInt(oItem, "XP Gain")*iStatModifier;
int iXPRequired = GetLocalInt(oItem, "XP Required");
int iCurrentSkill = GetLocalInt(oPC, ""+sResearchType+"");

//Check to make sure we only have the one research project going.
if (iActiveResearch != 0 && sResearchType != GetLocalString(oPC, "ActiveResearchType"))
    {
    SendMessageToPC(oPC, "You are currently researching " +sResearchType+" and will have to complete the basics to start a new field of study.");
    return;
    }

    //I would like a more elborate message here.
    SendMessageToPC(oPC, "Your XP mod is: " +IntToString(iStatModifier)+".");
    SendMessageToPC(oPC, "XP gain from this item is: " +IntToString(iXPGain)+".");
    SendMessageToPC(oPC, "DEBUGGING: Your current XP level is: " +IntToString(iCurrentSkill)+".");

//Once we begin reading the book we set variables and things.
switch (GetLocalInt(oPC, "ActiveResearch"))
    {
    case 0:
    //Perform first time use actions.
    SetLocalInt(oPC, "ActiveResearch", 1);
    SetLocalString(oPC, "ActiveResearchType", ""+sResearchType+"");
    SetLocalInt(oPC, ""+sResearchType+"", iXPGain);

    SendMessageToPC(oPC, "You begin to study the basics of " +sResearchType+".");
    break;

    case 1:
    SendMessageToPC(oPC, "You continue to study the basics of " +sResearchType+".");
    SetLocalInt(oPC, ""+sResearchType+"", iCurrentSkill+iXPGain);

        if (iCurrentSkill >= iXPRequired)
        {
        SendMessageToPC(oPC, ""+sBlueprintDescription+"");
        CreateItemOnObject(""+sStationBlueprint+"", oPC, 1, "research_station_"+sResearchType+"");
        SetLocalInt(oPC, "BasicResearch"+sResearchType+"Complete", 1);
        SetLocalInt(oPC, "ActiveResearch", 0);
        SetLocalString(oPC, "ActiveResearchType", "");
        return;
        }
    break;
    }
}

I can’t read what’s happening in the video.

But from the script a couple of things I notice. First, and this is not really an issue, is that
you have switch (GetLocalInt(oPC, "ActiveResearch")) when you’ve already stored that in iActiveResearch so you should just do switch (iActiveResearch)

Second, in the case 1: section you are setting “”+sResearchType+"" to the new value (iCurrentSkill + iXPGain) but then checking if the old value is >= iXPRequired. Is that on purpose?

I’m not seeing anything still that would make that 1 + 1 = 0 unless it is that comparison. When you go through it again you will get the new value you stored the first time and then iCurrentSkill might be >= iXPRequired.

Something like

case 1:
SendMessageToPC(oPC, "You continue to study the basics of " +sResearchType+".");
iCurrentSkill += iXPGain;
SetLocalInt(oPC, ""+sResearchType+"", iCurrentSkill);
...

would have it check the new skill value against required.

Edit: fixed “iCurrentSkil” to “iCurrentSkill”.

1 Like

Ah! I probably left that in the switch before I added it as its own integer. Oops. Thanks!

As for case 1, my goal was to have it produce the end result (a blueprint object in this case) after you’d reached the ‘RequiredSkill’ threshold (a variable on the book itself). So you hit the required skill, then you get the blueprint without having to read the book again. Let me see here. . .

Okay, so that definitely did fix the issue of the check not firing when you got to the RequiredSkill. And by pushing the debugging feedback to the bottom on the script made it update correctly. This was an issue of my making, but the += thing was where I tripped up.

Such a simple thing. Thanks so much, @meaglyn !

For anyone coming across this in the future,

iCurrentSkil += iXPGain;
SetLocalInt(oPC, ""+sResearchType+"", iCurrentSkill);

This updated the script total count before running the ‘if’ code block, allowing the code block to get the correct iCurrentSkill number.

Cool! And just to be clear, x += y is just shorthand for x = x + y.

One thing that still bothers me about this is why on earth are you adding an empty string both before and after another string as the result will only be this latter string anyway? I am talking about -

SetLocalInt(oPC, ""+sResearchType+"", iCurrentSkill);

Specifically the “”+sResearchType+"" part.

TR

1 Like

I did wind up trimming my script a bit, cutting out stuff like ‘activeresearch’. But for context, what this meant to do is write sResearchType to the PC database (and later the module one) as a way of allowing me to add future research types without having to create a new set of scripts for every one.

The Research Type is stored on a placeable as a string and the items you can use on that placeable also have those variables stored on them; iron would be ‘fabrication’, a magic crystal would be ‘magic’, for example. All meant to be used on an appropriate bench. But currently I’m not decided on which research types/skills we’re going to use, so I wanted to make this extensible. So when the script is run, it makes sure the ResearchType is the same between table and item, and then checks the skill of the PC attempting to do this research.

Like I said, I did trim it down quite a bit once I got it working, and got rid of quite a bit of cruft. >_>;

Does that make sense?

I think the point is that you can just do:

SetLocalInt(oPC, sResearchType, iCurrentSkill);

The extra “”+ and +"" are not doing anything except looking odd and possibly costing a couple of extra instructions. Same point I was making when I asked about it earlier. This is not about using variables on the item to specify the research type. That all makes sense to us :slight_smile:

Huh… For some reason I’ve always been under the impression that you needed to always use “”+XYZ+"" to specify strings within a command.

Now I’m not even sure where I came up with that. Agh.

Thanks again. Here’s to better coding habits. :beers:

2 Likes