Aller au contenu

Photo

Comparing a local race string


  • Veuillez vous connecter pour répondre
18 réponses à ce sujet

#1
Badwater

Badwater
  • Members
  • 113 messages
I want a NPC to take a certain action based on a PC's race. The NPC has a local race variable, sRace.  The if script line for a race comparison might be

if ( GetRacialType(oPC) == RACIAL_TYPE_DWARF )

so what is the proper syntax for inserting the sRace variable in this if statement? (The string has been previously defined, of course).

Thanks!

#2
Kato -

Kato -
  • Members
  • 392 messages
If you want to retrieve the race from sRace instead of from GetRacialType(), it would be:

if(sRace == RACIAL_TYPE_DWARF)

But is it what you're trying to do?

Kato

Modifié par Kato_Yang, 22 août 2011 - 08:32 .


#3
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
Yup...

string sRace = GetRacialType(oPC);

if (sRace == RACIAL_TYPE_DWARF)
{
// Do Dwarf Stuff
}

else if (sRace == RACIAL_TYPE_ELF)
{
// Do Elf Stuff
}

else if ....

#4
Badwater

Badwater
  • Members
  • 113 messages
I have different racial towns and I want to use a common conversation that will do two different things based on whether the NPC is the same race as the PC. I did not want to create a special script to handle this, as I already have the scripts to perform the actions I have in mind.

The NPC would have a local variable of sRace. What I want is a conditional that if sRace = PC race then one action is taken, and if they are not equal then a different action is taken.

So what I'm trying to get at is

if ( GetRacialType(oPC) == RACIAL_TYPE_sRace )

Modifié par Badwater, 23 août 2011 - 03:59 .


#5
Alex Warren

Alex Warren
  • Members
  • 179 messages
If I understand correctly sRace may be 'dwarf' 'elf' etc, right?

GetRacialType(oPC) returns an int so you can't compare it with string. You need a function that will convert your string to int, ie:
int MyRace(string sRace)
{
if sRace == "dwarf" return RACIAL_TYPE_DWARF;
else if sRace == "elf" return RACIAL_TYPE_ELF;
...

or you could just set sRace on PC in OnEnter event and than compare strings.

#6
Badwater

Badwater
  • Members
  • 113 messages
Argh. This is why I hate scripting. I know what I want, I just don't know how to get to it.

If I'm reading the above correctly, then I'm still not at a place where I have a conditional in conversation where the PC's race compares to the NPCs race or it doesn't. I guess it really doesn't matter whether it's comparing an integer or a string, I just want a local variable on the NPC where I can simply compare and the conversation goes in one direction or it goes in the other.

#7
Alex Warren

Alex Warren
  • Members
  • 179 messages
Ok, lets say you placed local strings on your NPCs. If you use MyRace function the code for conversation conditional should look like this:

int iNPCRace = MyRace(GetLocalString(oNPC, "NameOfVar"));
if(GetRacialType(oPC) == iNPCRace)

Modifié par Alex Warren, 23 août 2011 - 06:15 .


#8
Badwater

Badwater
  • Members
  • 113 messages
Thanks AW. I will give that a shot and see how badly I can screw it up. :P

#9
Failed.Bard

Failed.Bard
  • Members
  • 774 messages
Something like this, if the other doesn't work for you, maybe?

int StartingConditional()
{
return (GetRacialType (GetPCSpeaker() ) == GetRacialType (GetLastSpeaker() ) );
}

#10
Xardex

Xardex
  • Members
  • 217 messages
You cant compare an integer with a string.
RACIAL_TYPE_* is a integer.

Even if your sRace is "RACIAL_TYPE_DWARF" it will not match RACIAL_TYPE_DWARF that is outside quotation ("") marks, because the engine treats it as a number.

If you compare wrong types of variables the script shouldn't even compile.

if(sRace == "asdf") {}

#11
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<Tossing in two coppers...>

Badwater wrote...

I have different racial towns and I want to use a common conversation that will do two different things based on whether the NPC is the same race as the PC. I did not want to create a special script to handle this, as I already have the scripts to perform the actions I have in mind.

The NPC would have a local variable of sRace. What I want is a conditional that if sRace = PC race then one action is taken, and if they are not equal then a different action is taken.

So what I'm trying to get at is

if ( GetRacialType(oPC) == RACIAL_TYPE_sRace )


You might just grab the NPC racial type:

if ( GetRacialType(oPC) == GetRacialType(OBJECT_SELF) ) ...

<...and stealing four>

Modifié par Rolo Kipp, 23 août 2011 - 03:51 .


#12
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
return Get2DAString("racailtypes","Constant",GetRacialType(oPC)) == sRace;

#13
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<looking decidedly puzzled..>

If he just wants to compare PCs race to NPCs, why would you even mess with strings? Wouldn't

{
if ( GetRacialType(oPC) == GetRacialType(OBJECT_SELF) ) return TRUE;
return FALSE;
}

give him the conditional he wants without worrying about a variable on the NPC or pulling a string out of the 2da?

Not capping or anything. Really want to know.

<...but interested>

#14
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
@Rolo Kipp: Yes, but he seem to be fighting against that answer. It is what he need to do, in my opionion also. But if he really want to put the Lable for the constant into a local var and check against that. *shrugs* you might as well pull the lable out of the 2da. They shure dont exsist in the engine anywhere. So either redefine them, Kind of defeats the perpose of them being constant. Or get there deffinitions from the only place you can.

#15
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<wipes persperishun...>

Ahhh, thank you. Thought I was missing something... again :-P

<...er, persp... *sweat* from his brow>

#16
Badwater

Badwater
  • Members
  • 113 messages
Thanks to everyone for their replies. I don't learn anything if I don't know my options. And I have learned something from every reply.

Thanks for your answer, Rolo - that's the one that has the most options for me. But again, thanks to all of you, because I learned something today....*South Park segue*

#17
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<wishing he was...>

NP :-) That's how I learn, too

I am one of the oldest scripters on this board (well, on the old Interplay board...), but, thanks to years wandering in the desert, one of the greenest noobs :-P An awkward dichotomy.

Still, I'm having a lot of fun =) And I like Lightfoot8's algo for something else I was frustrated by... so, yeah, I learned something too.

@Lightfoot8 Is "return (boolean condition);" more efficient cpu-wise than "if (bCondition) return TRUE; return FALSE;"? Or, to be clearer, do they compile differently? Does the second method add an extra branch?

<...still young enough to blush>

Modifié par Rolo Kipp, 24 août 2011 - 04:20 .


#18
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
Yes it is more efficent CPU wize.
No they do not compile to the same thing.
Yes it adds another branch. along with having to move the value of the TRUE/FALSE constant onto the stack and move it around.

EDIT: you can always look at the compiled code for your scripts with nwnexplorer. 
If you want to know what all the codes mean they are listed at the following off site link.

http://www.nynaeve.n...tation/ncs.html

Modifié par Lightfoot8, 24 août 2011 - 10:24 .


#19
Rolo Kipp

Rolo Kipp
  • Members
  • 2 791 messages
<grins slyly...>

Lightfoot8 wrote...

Yes it is more efficent CPU wize.
No they do not compile to the same thing.
Yes it adds another branch. along with having to move the value of the TRUE/FALSE constant onto the stack and move it around.

Thank you! It is true in C, but I've been told over and over that (though based off C) NWScript is *not* C. :-/
(Mostly when we were arguing with Don Moar for arrays...)

EDIT: you can always look at the compiled code for your scripts with nwnexplorer. 

An old fox like me? When cool cats like you are so willing (and effective!) at helping? ;-)

(translation: Didn't know that :-) Cool. Sometimes I get on a real OCD, minimalist coding kick... I'll use that then)

<...and slips out of the coop)

Modifié par Rolo Kipp, 24 août 2011 - 10:50 .