Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

If Then Statements (SOLVED!)


Synectics
 Share

Recommended Posts

```
If ConditionA = 1 AND ConditionB = 1 Then
  Effect1
End If
If ConditionC = 2 AND ConditionD = 2 Then
  Effect2
End If
```
The above isn't working, and I'm beyond frustrated with it. For some reason, if the first If Then statement doesn't get run, the scripting language decides to skip **EVERYTHING** AFTER IT. >_<

In this example (and it is purely an example, as I didn't want to post all my GetVar codes to make it even more confusing), if ConditionA and ConditionB aren't met, but ConditionC and ConditionD ARE met… Effect2 still does not happen. And this isn't a case of a syntax error -- though C and D were met, Effect2 still wasn't happening. So I met the conditions for A and B, and suddenly both Effect1 **AND** Effect2 happened.

Is this really working as intended? Is there something I'm missing? Please tell me I don't have to do a few hundred nested If Then statements just to get my desired effect. x.x
Link to comment
Share on other sites

actually, thats because you don't need another if statement.
Remove your first End if and Put ElseIf instead of the 2nd If.
here we go:
```
If ConditionA = 1 AND ConditionB = 1 Then
  Effect1
ElseIf ConditionC = 2 AND ConditionD = 2 Then
  Effect2
End If
```
Link to comment
Share on other sites

I was curious as to if ElseIf statements worked in EE's scripting, but hadn't had the patience to test it with anything I already had working. Thanks for confirming that for me. XD

Nonetheless, that's epic annoying for my brain, because according to my logic, it should just keep running down the list of If Then's. But then, it's been forever since I've touched code this much.

DAMMIT COMPUTER, I GAVE YOU A CHECKLIST, NOW RUN THROUGH IT WITHOUT QUESTIONING ME! AND I SWEAR IF YOU DON'T DOUBLE-CHECK EACH AND EVERYONE I'M NEVER GOING TO PUT IN THAT NEW STICK OF RAM I'VE BEEN PROMISING FOR YEARS.
Link to comment
Share on other sites

@Synectics:

> DAMMIT COMPUTER, I GAVE YOU A CHECKLIST, NOW RUN THROUGH IT WITHOUT QUESTIONING ME! AND I SWEAR IF YOU DON'T DOUBLE-CHECK EACH AND EVERYONE I'M NEVER GOING TO PUT IN THAT NEW STICK OF RAM I'VE BEEN PROMISING FOR YEARS.

I lol'd
–-
It needs to know what to do if the 'if' is false xd hence, else or elseif
Link to comment
Share on other sites

Back already. /sigh /shakefist@computer

So, here's the code simplified:

```
If NPC = 5 AND Condition1 = True Then
  Effect1
ElseIf NPC = 5 AND Condition2 = True Then
  Effect2
ElseIf NPC = 5 AND Condition3 = True Then
  Effect3
End If
```
And once more, the same sort of deal. Effect2 is only happening if NPC = 5 AND Condition1 = True AND Condition2 = True, as opposed to what I obviously would like it to be doing.

If I start off with just NPC = 5 and Condition2 = True, then Effect2 never happens. But as soon as I enable Condition1, Effect 1 happens, followed by Effect2.
Link to comment
Share on other sites

Small update. Tried the following:

```
If NPC = 5 Then
  If Condition1 = True Then
      Effect1
  ElseIf Condition2 = True Then
      Effect2
  ElseIf Condition3 = True Then
      Effect 3
  End If
End If
```
But also to no avail.

And adding End If seems like it'd close the If altogether, meaning ElseIf wouldn't work. But then, I would think If Then statements following one another should work…. >.> Trying it now.
Link to comment
Share on other sites

I really hope you don't listen to him Synectics…
Yes, ElseIf works
Yes, It DOES run down the list of If's, when you have them like your first example
No, you do not need a Else/ElseIf when using If's
His most recent post, will cause errors... Speaking of Errors, have you tried turning "Scripting Errors" on for your server? That might help you more than anything we have to say...

Side note: Check the tutorials, they still help, even if you know this stuff
Here's one for example: [http://www.touchofdeathforums.com/smf/index.php/topic,850.0.html](http://www.touchofdeathforums.com/smf/index.php/topic,850.0.html)

Side Note#2: Syntax errors in GetVars cause scripts not to work, so you might as well just post the whole script here, otherwise, you're going to get alot more vague help
Link to comment
Share on other sites

```
Sub OnNPCDeath(Index, Map, NPCNum, NPCIndex)
If NPCNum = 5 Then
Call BattleMsg(Index, "You killed a " & NPCNum & ".", BRIGHTRED, 0)
If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "Part1") = 1 Then
Call PlayerMsg(index, "Quest complete: Nate's Trainee. Return to Nate Issley.", BrightGreen)
Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "DummyHit", 1)
End If
If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "Part1") = 1 Then
Call PlayerMsg(index, "Quest complete: Ivory's Trainee. Return to Ivory.", BrightGreen)
Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "DummyHit", 1)
End If
End If
End Sub
```
Been working on slimming down the code from what it was. This is what I have at the moment, copied over.

When GetVar…QuestIvory = 1, nothing happens. But if both GetVar...QuestNateIssley and GetVar...QuestIvory = 1, then it will run both, one after the other.

EDIT: In fact, I just moved the BattleMsg down below the GetVar If's, but before the last End If, like so:

```
Sub OnNPCDeath(Index, Map, NPCNum, NPCIndex)
'Call BattleMsg(Index, "You killed a " & NPCNum & ".", BRIGHTRED, 0)
If NPCNum = 5 Then
If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "Part1") = 1 Then
Call PlayerMsg(index, "Quest complete: Nate's Trainee. Return to Nate Issley.", BrightGreen)
Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "DummyHit", 1)
End If
If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "Part1") = 1 Then
Call PlayerMsg(index, "Quest complete: Ivory's Trainee. Return to Ivory.", BrightGreen)
Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "DummyHit", 1)
End If
Call BattleMsg(Index, "You killed a " & NPCNum & ".", BRIGHTRED, 0)
End If
End Sub
```
Therefore, if it were going all the way down the list despite the conditions not being met, the BattleMsg should still be called. Problem is, it's not getting called at all. Even though the If statements above it don't get run, it should still get called, amirite?
Link to comment
Share on other sites

Yeah. It's still broken. Now I've tried:

```
If NPCNum = 5 Then
If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "Part1") = 1 Then
Call PlayerMsg(index, "Quest complete: Nate's Trainee. Return to Nate Issley.", BrightGreen)
Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "DummyHit", 1)
End If
If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "Part1") = 1 Then
Call PlayerMsg(index, "Quest complete: Ivory's Trainee. Return to Ivory.", BrightGreen)
Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "DummyHit", 1)
End If
End If
Call BattleMsg(Index, "You killed a " & NPCNum & ".", BRIGHTRED, 0)
```And get this: the BattleMsg will **only** get called if:
a) The NPC is NOT 5
b) The two If Then statements inside of If NPCNum = 5 are both true

WTF.
Link to comment
Share on other sites

Yes, you are, however when this is the case, it's generally because of Syntax errors…

Specifically in your case. When you use GetVar() it returns it as a string... SO you need to convert your string to another type of something.. For your script, i'd use Int()
```
Sub OnNPCDeath(Index, Map, NPCNum, NPCIndex)
  If NPCNum = 5 Then
      Call BattleMsg(Index, "You killed a " & NPCNum & ".", BRIGHTRED, 0)
      If Int(GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "Part1")) = 1 Then
        Call PlayerMsg(index, "Quest complete: Nate's Trainee. Return to Nate Issley.", BrightGreen)
        Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "DummyHit", 1)
      End If
      If Int(GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "Part1")) = 1 Then
        Call PlayerMsg(index, "Quest complete: Ivory's Trainee. Return to Ivory.", BrightGreen)
        Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "DummyHit", 1)
      End If
  End If
End Sub
```
Link to comment
Share on other sites

Even with the exact code you just gave, it's still not working.

Using your version, the Battlemsg gets called, but again, if IvoryQuest is 1 and NateIssely isn't, IvoryQuest's effect doesn't get called.

I even tried placing a BattleMsg at the end of the code – outside of the If NPCNum = 5 -- and it won't get called if both If's above aren't true... and it SHOULD. If you kill NPC 5, and neither quest code is true, it should keep on going to the bottom of the Sub and call the Battlemsg.

And again, if BOTH quest conditions are true, BOTH get called.

Out of the few hundred lines of code I've placed, including a LOT of custom menu code and LOTS of If Then statements one after the other, such as in the Sub MenuScripts, I've never had anything like this happen. Definitely the most frustrating thing I've ever had happen with code.
Link to comment
Share on other sites

It's actually not as complicated as it seems. It's just a common misconception about GetVar. GetVar returns a _String_ value. You are comparing it to a numeric value. VB converts most values automatically, but if the variable is not set (i.e ""), there is no numeric counterpart for that. The result is an RTE 13, type mismatch, which effectively breaks the script.

It's a really easy fix. This will work:

```
Sub OnNPCDeath(Index, Map, NPCNum, NPCIndex)
  If NPCNum = 5 Then
      If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "Part1") = "1" Then
        Call PlayerMsg(index, "Quest complete: Nate's Trainee. Return to Nate Issley.", BrightGreen)
        Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestNateIssley", "DummyHit", 1)
      End If
      If GetVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "Part1") = "1" Then
        Call PlayerMsg(index, "Quest complete: Ivory's Trainee. Return to Ivory.", BrightGreen)
        Call PutVar("Scripts\Charextras\" & GetPlayerName(index) & ".ini", "QuestIvory", "DummyHit", 1)
      End If
  End If
  Call BattleMsg(Index, "You killed a " & GetNPCName(NPCNum) & ".", BRIGHTRED, 0)
End Sub
```
If you'll notice, the only thing I did was put quotation marks around the 1 that you were comparing the GetVar to. This is all it takes.

Oh, I lied. I also added a GetNPCName to your BattleMsg. Doesn't make sense for a player to get a message like "You killed a 5."  ;)
Link to comment
Share on other sites

Oh for the love of gawd, it's finally ing working now. I can finally move on to bigger and better things!!!!

Thank you very much for that brief explanation, too. It's so frustrating to know what I want and need to do, but not understand the HOW part.

/tip of the hat @ all who helped!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...