Download

level up stat function- issues with pointers(newb)

ladies and gentlemen, im having some issues with a function to level up some stats for an rpg system. Im trying to go this route because I would rather not have 30 functions that could -potentially- be summed up with one nice one.
anyway, onto the code:
for the sake of writing this Im going to say that the stat im passing is Strength and the original value is 3.

void AParallelCharacter::StatIncrease(int32 Stat)
{
Stat += 1;
}

was the first thing I tried, which doesnt work, I -think- it initializes a new variable with the value of 3 and increments that by 1, an onscreen debug message gives me the value of 4, but if I run the function again, it stays at 4.

so then I started testing with pointers, and that ended up the same way, a bunch of 4’s on the screen, but the strength value was left unchanged.

so any thoughts on how I could go about having 1 simple function be called several times without needing a bunch of switch/case/if statements layered in there?

Hi iLLo,
It’s a bit unclear what you are trying to do, it would help if you could post more code.

As it is written now, this function doesn’t really make any sense. The value you pass in to the function will just be copied to a new variable inside the function, incremented, then the function will return and the variable will go out of scope and no longer exist.

Perhaps you wanted to do this?:



void AParallelCharacter::StatIncrease(int32& Stat)
{
   Stat += 1;
}


However, I suspect that from your question there are some fundamental problems that need fixing. Post your code and I’ll take a look.

yeah its the fundamental problems with pointers and how I can use them.
so the code is for increment stats in an rpg. click a button and gain a stat, pretty straight forward.
the current code that works, is about 30 functions(1 for each stat that can level up) and is your typical
strength += 1;
another function
dexterity += 1;
and that all works fine.
however I would like for this new function, that Im having issues with, to take any arbitrary stat and increment that particular stat by 1,so I can reuse this one function for all the stats just by passing a new argument to it.
im my head, the following would both work with this function:
StatIncrease(Strength);
StatIncrease(Dexterity);
So Im wondering if there is a way to have the ‘Stat’ argument reference any arbitrary variable and have it increment that by 1.

Ok, so if I understand you correctly there are a couple of ways you could go about this although I don’t think you should necessarily balk at the idea of having a separate function for each - there may come a time when you want different stats to increment differently and having individual functions makes that easy.

Anyway, one method would be to use a return value:



int StatIncrease(int Stat)
{
   return Stat + 5;
}

void MyRunTimeFunc()
{
   // Time to increase the health stat
   iHealth = StatIncrease(iHealth);
}


Another method would be to use a reference parameter (note the ampersand!):



void StatIncrease(int& Stat)
{
   Stat += 5;
}

void MyRunTimeFunc()
{
   // Time to increase the health stat
   StatIncrease(iHealth);
}


I think you should read some more about pointers and references: