Author Topic: Simple Question  (Read 2862 times)

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Simple Question
« on: April 12, 2010, 08:12:09 PM »
I really dunno how to word this question... Basically, instead of doing this:
Code: [Select]
if (unitWeapon == mapLaser || unitWeapon == mapRailGun || ...)
I want to do something like this:
Code: [Select]
if (unitWeapon == anEdenWeapon)
Where that will return true if the unit's weapon is a weapon available to Eden.

I'm pretty sure this is doable (and relatively easy), I just forget how...
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Simple Question
« Reply #1 on: April 12, 2010, 11:15:57 PM »
I don't know of anything built in. You probably just have to write your own function.

Code: [Select]
bool IsEdenWeapon(weaponType)
{
  switch(weaponType)
  {
    case mapLaser:
    case mapRailGun:
    // ...
    return true;  // Use switch case fall through (no "break" statement)
  }

  // Nope
  return false;
}



if (IsEdenWeapon(weaponType))
{
  // Eden weapon type code goes here
}


// Or for the alternate case
if (!IsEdenWeapon(weaponType))
{
  // Non-Eden weapon type code goes here
}

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Simple Question
« Reply #2 on: April 13, 2010, 06:56:50 AM »
Hmm, I could've sworn there was something that could do that...  But I guess not.  Well, thanks anyways.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Simple Question
« Reply #3 on: April 13, 2010, 09:51:21 PM »
Well, there is now. I thought that would be useful generic functionality, so I added some functions to the OP2Helper project.

Code: [Select]
bool IsEdenOnlyWeapon(map_id weaponType);
bool IsPlymouthOnlyWeapon(map_id weaponType);
bool IsCommonWeapon(map_id weaponType);
bool IsEdenWeapon(map_id weaponType);  // Common or Eden only
bool IsPlymouthWeapon(map_id weaponType);  // Common or Plymouth only

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Simple Question
« Reply #4 on: April 13, 2010, 10:50:51 PM »
Ah, well, that works.

I think I was thinking of an enum, which kinda does what I wanted but not really...  Oh well.  Irrelevant now.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Simple Question
« Reply #5 on: April 14, 2010, 03:37:57 AM »
Enums are basically just named constants. They don't really provide any if shortcuts. At least not in C++. If you arrange the constants nicely though, you can get some degree of shortcuts. But we're dealing with an already specified set of constants which we can't reorder, and they're not ordered nicely.

If you could reorder constants to keep releated ones consecutive, you can do something like:
Code: [Select]
if (weaponType >= mapLaser && weaponType <= mapAcidCloud)
{
  // ....
}

Mind you, you might want to add secondary constants in a case like that, such as mapEdenFirstWeapon, mapEdenLastWeapon. It would make the if statement slightly clearer, and the needed additions to the enum would be fairly obvious in meaning.
Code: [Select]
enum map_id
{
  // ...

  mapEdenFirstWeapon,
  mapLaser = mapEdenFirstWeapon,
  // ...
  mapAcidCloud,
  mapEdenLastWeapon = mapAcidCloud,

  // ...
};

I'm not sure if you can use the enum constants within the enum declaration though, so the values on the right of the = might need to be actual numbers.

But yeah, irrelevant.
 

Offline Moley

  • Jr. Member
  • **
  • Posts: 95
Simple Question
« Reply #6 on: April 14, 2010, 06:25:09 AM »
don't know if your code was supposed to work...
but you have an assignment operator backwards...
I HATE SPELLING!!!!!!
if i spell something or screw up grammer,
ignore it or tell me if you dont understand what i typed.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Simple Question
« Reply #7 on: April 15, 2010, 03:07:46 AM »
Where?
 

Offline Hidiot

  • Hero Member
  • *****
  • Posts: 1018
Simple Question
« Reply #8 on: April 15, 2010, 05:41:55 AM »
mapLaser = mapEdenFirstWeapon
"Nothing from nowhere, I'm no one at all"

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Simple Question
« Reply #9 on: April 15, 2010, 11:27:57 PM »
Well, it can't be the other way around if you declare the one on the right first.

I just thought I'd put the range delimiting ones on the outside, kind of like parenthesis.