[Plugin] SwitchCase

Post your completed addons to share with the community

Post » Thu Nov 17, 2011 11:58 am

Plug-in: SwitchCase
Allows the developer to execute actions based on a traditional switch-case structure.

I originally developed this while designing a text parser in C2. I found that using the switch-case structure helped me to keep my event blocks more organized.

Download SwitchCase plug-in (Version:01; Release date: 17 November, 2011)

Changes
- Added the Case Compare and Case List conditions
- Case Compare allows the selection of a comparison operator to be used in conjunction with the value
- Case List allows the entry of multiple items in a list, each of which will be compared separately. This effectively simulates an OR condition.

Installation
- Extract the contents of the zip file into \Construct2\exporters\html5\plugins
- Folder name: wastrel_switchcase

Features
- Create switch-case structure to process actions
- Create Case conditions using a single value, a range of values, a comparison operator with a value, or a list of values
- Included a string search Case condition, since I already had it written

Usage
- Create a SwitchCase -> Switch event using an existing variable, the value of which will be checked in the Case statements. The action for this event should be empty.
- Create a SwitchCase -> Case sub-event, under the Switch event. Enter a value to check against the Switch variable. This value can be a number, string, or any type of variable.
- Add any number of actions that should be processed for that case.
- Add a SwitchCase -> Break action at the end of the action block, to bypass the remaining case conditions in the switch. Leave the break off to process the remaining case conditions.
- Add a SwitchCase -> Default sub-event at the end of the case conditions, to process a default condition. The Default event is optional.
- The case statements do not technically need to be sub-events of the switch statement. They will be processed no matter where they are in the event sheet, since they are all part of the same object. Adding them as sub-events just helps to better organize the sheet.

Conditions
- Switch: Starts the SwitchCase block, using a single variable against which the Case statement values will be checked.
- Case: Adds a Case statement, using a single value to check against the Switch variable. Value can be an int, string, or any type of variable.
- Case range: Adds a Case Range statement, using a range of values to check against the Switch variable. Values can be int, string, or any type of variable.
- Case contains: Adds a Case Contains statement, using a single string value. Statement is true if the string is contained in the Switch variable.
- NEW Case compare: Adds a Case Compare statement, which combines a comparison operator (=, !=, <, <=, >, >=) and a value. Statement is true if the Switch variable meets the comparison condition to the value.
- NEW Case list: Adds a Case List statement, which allows the entry of a list of values. Each value is compared in succession, and when a value is equal to the Switch variable, the statement returns true. This effectively simulates an OR condition.
- Default: Adds a Default statment. The actions in the default statement will be run if no other case condition is true in the switch structure. There should only be one Default statement, and it is optional.

Actions
- Break: Adds a break action, to be placed at the end of a block of actions. This will bypass the remaining case conditions in the Switch structure. The break can be left off to continue processing case conditions.

Expressions
- None

Examples
- switchcase_example.capx: Demonstrates the switch structure, all five case conditions, the default condition, and the break action.
Last edited by Wastrel on Fri Dec 11, 2015 2:57 pm, edited 3 times in total.
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Thu Nov 17, 2011 1:49 pm

[QUOTE=Wastrel]...Leave the break off to process the remaining case conditions...[/QUOTE]

[code]switch (var)
case 0: // if var == 0
case 1: // or var == 1
case 2: // or var == 2
    .... do something
    break[/code]

You could add "compare" condition like "system condition:Compare two value", to be used in expression's "OR" operation.

[code]if var1 > 3
or var2 < 5
or var3 == 7
    .... do something[/code]

Uh, it is not the same with traditional "switch case". So it might be better to create another plugin. Sorry.rexrainbow2011-11-17 14:01:22
B
97
S
22
G
178
Posts: 4,120
Reputation: 104,047

Post » Thu Nov 17, 2011 8:20 pm

I added two new case statements, based on @rexrainbow's feedback.

The Case Compare condition allows the use of a comparison operator (=, !=, <, <=, >, >=) in conjunction with a value. The Switch variable is compared to the value using the selected operator.

The Case List condition allows the entry of a list of values, each of which is compared individually. If a match is made, then the statement comes back as true. This effectively emulates an OR condition.

And yes, these various case statements make this a non-standard switch-case structure, but I am not clever enough to come up with a better structure name.

The files in the OP have been updated.

Thanks!Wastrel2011-11-17 20:24:55
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Sat Nov 03, 2012 7:46 am

Error 404
B
12
S
3
G
3
Posts: 8
Reputation: 2,661

Post » Sat Nov 03, 2012 2:40 pm

@sayhitoarvind, try it now
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Sat May 03, 2014 5:07 pm

Error 403. Is it still working with current C2 version btw?
http://www.schizokoa.com we are a small game jam group, visit us!
B
23
S
5
Posts: 44
Reputation: 1,270

Post » Tue May 06, 2014 1:09 pm

@lahssoo, try it again. It works with the latest version of C2.
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Thu May 08, 2014 2:37 pm

Thanks a lot, my code will be readable :D
http://www.schizokoa.com we are a small game jam group, visit us!
B
23
S
5
Posts: 44
Reputation: 1,270

Post » Fri Sep 25, 2015 2:24 pm

@Wastrel where can i find the switchcase_example.capx ?

thanks!
B
9
S
2
G
1
Posts: 17
Reputation: 669

Post » Mon Oct 05, 2015 4:19 pm

does anyone have a working example of this they'd be willing to share?

much appreciated!

-Rik
B
9
S
2
G
1
Posts: 17
Reputation: 669

Next

Return to Completed Addons

Who is online

Users browsing this forum: No registered users and 1 guest