Thread: starting a statement

Results 1 to 7 of 7
  1. #1 starting a statement 
    Registered Member
    Join Date
    Mar 2012
    Posts
    88
    Thanks given
    4
    Thanks received
    3
    Rep Power
    36
    i was wondering since my objecthandler has alot of objects in it
    what woud be the best way to start your statement

    examples
    Code:
    if (id == ####) 
       do this
    if (id == ####)
       do that
    Code:
    else if (id == ####) 
       do this
    else if (id == ####)
       do that
    Code:
    else {
       switch (id)
          case ####:
    }
    or doesn't it matter at all how its done?
    Reply With Quote  
     

  2. #2  
    Registered Member _Andy's Avatar
    Join Date
    May 2013
    Age
    25
    Posts
    644
    Thanks given
    224
    Thanks received
    111
    Rep Power
    4
    well, as clicks are handled for each individual object, you can't really click 2 objects at once so there isnt much difference.

    however, try to use switch statements where possible (in sake of clarity)
    quit
    Reply With Quote  
     

  3. #3  
    Registered Member Ffsn99b's Avatar
    Join Date
    Aug 2013
    Posts
    137
    Thanks given
    3
    Thanks received
    9
    Rep Power
    16
    Code:
      switch (id) {
          case ####:
       break;
          case ####:
       break;
    }
    Quote Originally Posted by Sam Bartlett View Post
    Why would you want to remove the music tab? Idiot..
    I'm an idiot for removing something I don't plan to use? The irony.
    Quote Originally Posted by Sam Bartlett View Post
    I'm pround to be in your signature, you're an apsalute idiot and act clever. Then you make a fool of yourself.
    proud*
    absolute*
    This is going to go in my sig as well lol, gj
    Reply With Quote  
     

  4. #4  
    L O S E Y O U R S E L F
    Format's Avatar
    Join Date
    Aug 2013
    Age
    22
    Posts
    2,185
    Thanks given
    1,110
    Thanks received
    994
    Discord
    View profile
    Rep Power
    5000
    Use switch, much cleaner and easier to navigate through. Less clutter imo
    lol saem


    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Mar 2012
    Posts
    88
    Thanks given
    4
    Thanks received
    3
    Rep Power
    36
    Alright thanks for the quick answers
    Reply With Quote  
     

  6. #6  
    Banned
    Join Date
    May 2014
    Posts
    32
    Thanks given
    6
    Thanks received
    14
    Rep Power
    0
    Let's take in in depth look at what you're asking.

    Code:
    if (id == ####) 
       do this
    if (id == ####)
       do that
    So what you're doing is for EACH if statement, you are checking if the Id is equal to your number. Even if you have already found the correct statement, it will still keep checking against the IDs. You never want to do this.


    Code:
    else if (id == ####) 
       do this
    else if (id == ####)
       do that
    This is the better way, in which that once your ID is matched, it does NOT keep checking your ID against others in the statements and skips the rest altogether.


    Code:
    else {
       switch (id)
          case ####:
    }
    This is the same thing as above, HOWEVER case statements can be compiled to much faster code because you are guaranteeing that you will only be checking against primitive integers for all of your statements. This is generally faster.


    You're asking, which is the correct way? The answer is NONE OF THEM! You are using if statements for the wrong thing. This is a very bad programming practice if you are working with large sets of IDs (such as in RSPS) where you could have 10,000 different statements. What IS the correct way? Well there is no single answer, but I can give you a few examples.

    The most efficient way (in terms of CPU cycles) would be to create an array of some Interface which allows you to not do ANY if statements and go directly to the code you want to execute. Some psuedocode:

    You would have a general interface such as this:
    Code:
    interface Foo {
        void doAction();
    }
    Also you would have an array with length of the maximum ID of the object you will be accessing.
    Code:
    Foo[] foos = new Foo[MAX_OBJECT_ID];
    Next you need an implementation class such as thi
    Code:
    class DoorFoo implements Foo {
    
        void doAction() {
            openThe****ingDoor();
        }
    }
    Now at server initialization, you would do something like this:
    Code:
    foos[THE_OBJECT_ID] = new DoorFoo();
    Now, all you have to do to access the action is ONE operation! no if statements at all!
    Code:
    foos[objectClickedId].doAction();

    This is not "THE" proper way to do it, but if you're concerned with processing time such as in an RSPS, it's the way to go.

    Hope I helped you learn something!
    Reply With Quote  
     

  7. #7  
    Registered Member
    Join Date
    Mar 2012
    Posts
    88
    Thanks given
    4
    Thanks received
    3
    Rep Power
    36
    Learned something yeah, but gonna use switch statements for now
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Changing Starting Equipment (new coders)
    By hellz chevy in forum Tutorials
    Replies: 8
    Last Post: 08-06-2007, 07:41 PM
  2. The Halo Killas. Starting at 3.
    By Omegus in forum Showcase
    Replies: 6
    Last Post: 06-07-2007, 11:52 PM
  3. Adding A "LEET" Starting Place
    By JavaŠ in forum Tutorials
    Replies: 11
    Last Post: 06-07-2007, 08:02 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •