Ways to make python code more concise (functions etc)


#1

Obvious disclaimer here: this is for more veteran coders for use on the repeated levels (Brawls and such).

I’ve used functions in a couple of cases, which hasn’t actually made the code more concise, but does separate out some of the looping and makes it easier to read the code and debug (especially when you can only look at 10 or so lines of code in the window).

Yesterday I was peeking at someone else’s code from the high scores window (which usually doesn’t work for me, btw, but sometimes does). Unfortunately I can’t find it agan and post it, but I believe it was python with decorators. So even though it was a higher level of (I think) Sarven Treasure, the code was very brief, and most lines started with @, which is beyond my python skillz but I believe those are decorators (fancy functions).

But, I didn’t notice where the decorators/functions were defined. So, immediate question is can I store functions somewhere and then globally access them on individual levels?

And more generally, I’m just looking for any tips for dealing with the fact that it gets to be a pain to edit things in code combat once your program exceeds 30 or so lines. I know copy and paste to an external editor is one option, but I’m looking for other ways, if they exist. Thanks!


#2

What you most likely saw was Coffeescript, another language that is supported.
You can select it from the Hero-Selection-Screen.
Try it out. Though it sometimes behaves a bit… strange, most levels work out of the box. It is a combination of Python and JavaScript. The whole engine is based on it.


#3

Yeah, it might be cool to experiment with other languages but from other threads it sounds like a bit of a pain b/c you lose all your existing code when switching languages. Also, if that’s the case I doubt it actually is any more concise than comparable python code unless coffee script adds the ability to access external/global functions?

In some sense, I’m asking a more general question than just about python: can I make a library and then access it from individual levels? I assume the answer is no but no harm in asking…


#4

This is currently not possible (though there is some work in this direction planned/ongoing). We will inform you (and everybody else) as soon as this is possible. This will most likely happen in it’s own topic though.


#5

Cool, looking forward to that and/or a better text editor! But don’t mean to sound like I’m complaining, it’s nice to see the continual expansion and improvements in other areas.


#6

You can “make a library” . . . well, OK not in the way you meant it but…

create a file on your computer (or on the web/cloud if you do codecombat from more than one computer) and copy all your code into it…

now when you want to re-use a function you already wrote, just copy and paste it from your “code library” into your current codecombat level…

You also have a copy of all your level-code so you can redo levels in all the languages without losing anything.


#7

Is that right, though? It was discussed here but I didn’t understand how exactly (or if) the code is saved

http://discourse.codecombat.com/t/changing-language-without-losing-current-code/2697


#8

Yup, as discussed in that thread, everybody likes the idea including “codecombat” themselves, but…

For now, it only saves the code currently in the “code box.” (Well, it also has some sort of copy for the currently submitted ladder game.) (as far as I know it doesn’t keep a history of all your submits, but I could be wrong on that point and even if it does we don’t have direct access to it (you would have to convince Nick to dig it out for you.))

So, for now at least, you have to do your own “source control” where/how-ever you wish to do so.

Well, I just thought of another way…

Change language,
cut code,
"reload" default code,
paste old code at bottom of code-window,
comment out old code (with language appropriate method),
have fun with new language,
reverse/repeat.

All your code for each language is always there in the window (well, except during the cut-reload-paste moment, of course.)


#9

you can create a new email address ‘login’ by adding + before your email address.

Eg
myemailaddress@gmail.com might be your normal Code Combat email login

If you wanted to create a new account for coffeescript, you can do
coffee+myemailaddress@gmail.com
the select CoffeeScript. :smile:

Mmmm coffee…


#10

As far as I know this only works for gmail-adresses though. You should definitly try this with other adresses (web, gmx, …) before playing.


#11

Does anybody actually use anything besides gmail these days?

Well done Google. Takin’ over the planet’s email system!

domination


#12

Me.

(I think I actually do have a gmail account but there is only one person I ever use it to email.)


#13

It would actually be myemailaddress+coffee@gmail.com (the stuff after the plus is ignored, not the stuff before the plus). And I think some other email providers also support plus addressing, not just Gmail.


#14

and some support minus addressing (-)

Hooray for consistency!!!


#15

The optimistic provider says:
email+stuff@optimistic.com

The pessimistic provider says:
email-stuff@pessimistic.com

The normal provider says:
Why should I do this, most users won’t use that feature anyway and I have less work


#16

I’m growing tired of typing the same code over and over. Saved functions to the rescue (in the workplace!).

When will codecombat offer users the ability to call on their saved functions?

thank you
Dennis