Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Author Topic: Unbelievably basic variable question  (Read 3917 times)

0 Members and 1 Guest are viewing this topic.

Mikael

  • Supporter
  • Regular poster
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 127
    • View Profile
    • MDNA Games
Unbelievably basic variable question
« on: April 03, 2007, 12:07:48 AM »

OK, this is really basic, and I'm kind of embarrassed for asking it, but here goes:

Let's say that I want different things to happen when I left click a certain region entity, depending on which value the variable "myvariable" is set to. I have no problems with the if...else statements, as long as there are only two conditions involved, but trying to use more conditions only makes me confused.

I've tried this:

global myvariable;

on "LeftClick"
{
  if(myvariable==1) Game.ChangeScene("scenes\scene1\scene1.scene");
  if(myvariable==2) Game.ChangeScene("scenes\scene2\scene2.scene");
  if(myvariable==3) Game.ChangeScene("scenes\scene3\scene3.scene");
  else Game.ChangeScene("scenes\scene0\scene0.scene");
}


It doesn't work at all, off course. How the heck should I code this?

Regards,

Mikael
Logged

Mikael

  • Supporter
  • Regular poster
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 127
    • View Profile
    • MDNA Games
Re: Unbelievably basic variable question
« Reply #1 on: April 03, 2007, 12:31:36 AM »

I got it!

on "LeftClick"
{
  if(myvariable==1) Game.ChangeScene("scenes\scene1\scene1.scene");
  else if(myvariable==2) Game.ChangeScene("scenes\scene2\scene2.scene");
  else if(myvariable==3) Game.ChangeScene("scenes\scene3\scene3.scene");
  else Game.ChangeScene("scenes\scene0\scene0.scene");
}

Right? Please correct me if I'm still doing something stupid here.

Thanks,

Mikael
Logged

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Unbelievably basic variable question
« Reply #2 on: April 03, 2007, 02:55:54 AM »

Hi Mikael,

consider using switch instead. It would definitely look cleaner.

on "LeftClick"
{
   switch (myvariable)
   {
       case 1:
         Game.ChangeScene("scenes\scene1\scene1.scene");
         break;
       case 2:
         Game.ChangeScene("scenes\scene2\scene2.scene");
         break;
       case 3:
         Game.ChangeScene("scenes\scene3\scene3.scene");
         break;
      default:
         Game.ChangeScene("scenes\scene0\scene0.scene");
   }
}

Hope this helps.
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

adonf

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 124
    • View Profile
Re: Unbelievably basic variable question
« Reply #3 on: April 03, 2007, 10:04:44 AM »

Your solution in "if ... else if ... else if ... else if ... else" would have worked too. But, yes, using a "switch case" statement is cleaner
Logged
I am the Milkman. My milk is good.

Mikael

  • Supporter
  • Regular poster
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 127
    • View Profile
    • MDNA Games
Re: Unbelievably basic variable question
« Reply #4 on: April 03, 2007, 02:28:28 PM »

That's interesting. Thanks Metamorphium and Adonf. Is there somewhere I get get more information about coding like this?

Mikael
Logged

TheDerman

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 225
    • View Profile
Re: Unbelievably basic variable question
« Reply #5 on: April 04, 2007, 01:58:22 AM »

You say it's cleaner, and I take that to mean it involves less overall code writing, but is there really any advantage to using switch instead of if...else...if...?

Does it improve performance or anything like that?
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Unbelievably basic variable question
« Reply #6 on: April 04, 2007, 12:41:35 PM »

You say it's cleaner, and I take that to mean it involves less overall code writing, but is there really any advantage to using switch instead of if...else...if...?

Does it improve performance or anything like that?
It's really just about readibility of the code. Readable code = maintainable code.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave
 

Page created in 0.038 seconds with 23 queries.