Jump to content

The Programming Megathread!


McJobless
 Share

Recommended Posts

As seen on Blockland...

 

if-youre-new-to-coding-this-is-the-progrshutterstock-programming.jpg

 

printf("Welcome to the \"Programming MEGAthread\"!");

 

In this Megathread, feel free to discuss your latest work, get feedback, ask questions, find resources and learn more about the languages and logic for Programming. Since many old LEGO games involve scripting systems, feel free to also talk about scripting for video games and visual programming systems as well (such as Unreal's Blueprints).

 

If you're new to Programming, here's some common Q&A to get you started:

 

What specifically is Programming about?

Programming is the act of writing instructions for your computer to perform. Turns out that while your computer can process things WAY faster than we ever could, the computer is actually fairly dumb; they need to be told exactly what to do and how to do it. Just in the same way as we humans can speak to each other a multitude of languages, there are many ways to communicate with your PC (and other devices such as a phone or games console). Every language is different, yet they all exist for the purpose of telling your computer what to do.

 

What are some of the Programming Languages?

In reality there's a very long, every expanding list, but here's just a taste of the languages you could learn and use, along with documentation.

 

 

No programming language is the "one to rule them all" and every language is more-or-less founded on the same logic steps. The language is just the way you express your ideas; good programming is really about finding the "best" way to achieve a goal.

 

I've never programmed before, where do I start?

That's such a difficult question to answer, because it depends a lot on what you'd like to do as a programmer, how you learn, and what tools you have access to. Personally, I'd recommend that you set up a goal (such as "I want to make a infinite runner game"), and then you learn as you go, finding lessons for the things you're not sure about.

 

Free General Website Resources:

 

 

-more resources (including paid and language-specific as well as game engine tutorials) to be posted in the future-

 

Your OP sucks and you should feel bad!

Then help me rewrite it to be awesome! Give me resources to include, links to better documentation and other types of resources (such as compilers/libraries/toolkits), better descriptions of programming, tips for newbies and so on. I'd like this OP to be an ever-evolving flow of knowledge that can help people get started.

 

 

  • Like 3
Link to comment
Share on other sites

I learnt something yesterday night.

 

While doing programming exercises in C++, I discovered "Array Decay". Because arrays are just a fancy version of a pointer, when you pass them in as an argument to a function/method, they lose their array properties and are just treated like standard pointers. Normally this works fine; pointer notation allows you to use [] on it anyway. The issue is using Range-Based For loops for iteration over an array.

The Range-Based For loop looks like this:

for (tempType tempVariable : arrayName) {}

It's the equivalent of the foreach loop in C#. Each iteration of the loop, the tempVariable is equal to the current element of the array, and at the end of the iteration tempVariable is saved back to the element in arrayName. When your array and the range-based for loop are in the same scope, this works perfectly, however...

Because an array parameter is treated like a standard pointer, it loses certain necessary properties (such as array::begin) that the Range-Based For loop requires to work. Because there's no workaround for this and arrays don't have "length" property like in C#, you end up having to either manually keep track of the length of the array or hardcode a maximum number in and then break from the loop once it reaches some kind of terminator character.

Thoroughly frustrating.

 

Should also mention; sizeof() correctly returns the length of your whole array in bytes when used in the same scope as the array. sizeof() only returns the length of the first element in an array in bytes when it is used on an array parameter. I was hoping to use simple maths (sizeof(arrayName) / sizeof(int)) to get the size of the array for this programming exercise, but that didn't work for the reasons I've said up above.

Link to comment
Share on other sites

I love how the images you posted are of Python and JavaScript.

 

I learned a neat thing in Python recently. Normally when accessing a key-value pair in a dictionary, you have to safeguard it in a try...except block if there is a chance the key does not exist:

try:
    myDict["myKey"]
except KeyError:
    # The key does not exist, handle the error

The neat thing I learned is all dictionaries have a get() method that returns the value or None if the key does not exist. This changes the above code into:

myValue = myDict.get("myKey")

if myValue is None:
    # The key does not exist

I honestly wished I had learned this sooner. No Python book I have ever read (not even the book from college) taught this.

  • Like 2
Link to comment
Share on other sites

I had a fun exercise where I had to write code for primitive generation in OpenGL. Cone, Cylinder, Tube, Sphere, Torus. All working and all beautiful.

Unfortunately I accidentally gave the torus flipped normals before I submitted the assignment. The renderer didn't have a wireframe so it was easily missed. Works now though.

OskPtZN.png

 

:D

Link to comment
Share on other sites

Sometimes the best way to learn is by knowing what not to do.

 

I recently began managing a WordPress site, and by managing I mean fixing the huge mess of technical debt while trying to add a few new things while waiting on my laptop to be repaired so I can make a brand new site. One of those new things are a series of recently recorded promo videos. Per my advice, they are being uploaded to the site for embedding rather than being posted to YouTube and dealing with the mass amount of JS it loads.

 

What I wanted to do was put the videos at the top of page and center them. In this way, they are prominent and visible. However, WordPress will not allow that. It lacks the ability to center or right align videos, only default left-align. OK, this shouldn't be too hard to fix. Just write a bit of CSS and add a class to the markup. Tada!

 

Except with WordPress, nothing is ever that simple.

 

See, when embedding videos, WordPress uses it's own shortcode format which is internally translated into the appropriate markup. The shortcode also lacks a way to add a class to the resulting markup.

<!-- It is so close to raw HTML yet so, so far -->
[video width="1280" height="720" mp4="http://www.example.com/wp-content/uploads/2016/03/promo-video.mp4"][/video]

Now I could still fix this in CSS. Rendered, this shortcode turns into a <div class="wp-video"></div> containing all subsequent markup. All I have to do is center the div:

 

.wp-video {
  margin-right: auto;
  margin-left: auto;
}

 

Except you should never write such broad styling like that. I do not want all videos on the site center-aligned on the page, only these ones. I should be able to choose what videos have what alignment.

 

To that end, I resorted to JavaScript to resolve the issue (despite there already being a ton of JavaScript on the site from the theme). I started by adding a paragraph tag with the class video-center before the shortcode.

<!-- When rendered, the DOM structure will be:
<p class="video-center"></p>
<div class="wp-video"> ... </div>

It must be a paragraph rather than a more ideal element (span) because
WordPress automatically wraps every non p, a, or h1-h6 tag in a paragraph, 
which breaks the desired DOM structure.
-->
<p class="video-center"></p>[video width="1280" height="720" mp4="http://www.example.com/wp-content/uploads/2016/03/promo-video.mp4"][/video]

In this way, I can selectively choose what videos I want to be centered. I then load the following JS on each page:

 

// Use querySelectorAll so multiple videos on a page can be aligned
var vidCenter = document.querySelectorAll(".video-center");

// Make sure we have at least one center element
if (vidCenter) {

  // Beacause querySelectorAll returns an array-like NodeList
  // instead of a proper array, we have to use a for-loop instead
  // of the more ideal forEach method. This can be worked around
  // but it's useless for such trivial code.
  for (var i = 0; i < vidCenter.length; i++) {

    // Check if the next sibling element is the embedded video
    if (vidCenter[i].nextSibling.classList.contains("wp-video")) {
      
      // Select the video and apply the margin-centering rules
      var video = vidCenter[i].nextSibling;
      video.style.marginRight = "auto";
      video.style.marginLeft  = "auto";
    }
  }
}

The comments explain everything, but of note is the nextSibling attribute. It is what selects the embed video.

 

Of course, this could be done as well with CSS:

/* In this case, the + selector is the same as nextSibling */
.video-center + .wp-video {
  margin-right: auto;
  margin-left: auto;
}

I've chosen JS for now as it was a tad easier to side-load it, but I'll likely switch to CSS soon.

 

And that is how not to go about centering a wrapper div. This whole post and issue could have avoided if WordPress just allowed the right way to do it (add a class to the wrapper div via the shortcode and/or support video alignment), but nope. Hopefully you have learned something through all this.

Link to comment
Share on other sites

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.