What does 'const' mean? My thoughts at this early stage!

I reckon it is short for constant. So depending on the following code, in this instance in the course;

‘const int a = 4;’

…would probably mean it doesn’t want the value to change, perhaps even negating any variable code that would normally change the value of ‘a’.

Let’s see if I am right!

2 Likes

Hmm i think you are on to something :smirk:

1 Like

Hi James,

I’d love to go a little deeper into this with you, because there are some nit-picky nuance items about this idea.

The keyword ‘const’ indeed refers to a constant. It is, as you have guessed, used in a condition where we don’t want the variable to change.

Where to use a const? That can be difficult to conceptualize when we’re trying to think of a thing that doesn’t need to change: bullets count down and reload, score points add up, hit points go down.
Where would we ever need a ‘const’?

Well, in game development, it might be difficult to find a place. We declare variables all the time because the gaming environment is so dynamic, both in-game and outside. Expansion packs will add weapons and spells and interfaces, etc…everything is constantly changing.

// BeginRant();

Also, now for the nit-picking…If it can’t change, it shouldn’t be called a variable. Period. By definition, variables are expected to ‘vary’. That’s why they’re called ‘variables’…because they are ‘able to vary’. It bugs me when people say ‘a variable that doesn’t change’ because it’s bad grammar…

…It’s like when the news reads ‘armed gunman’ but they know the gunman was already armed, because he’s a friggin’ gunman, der! Each and every gunman happens to be a person armed with a gun. That’s what makes them gunmen. Armed is implied in the title of ‘Gunman’…but that’s me being a nit-picky word-nerd.

// EndRant();

So yeah, a constant is a variable that doesn’t change.

In web development, and I use this in JavaScript a lot, we declare anything as a ‘const’ until we decide that it needs to change. Once we notice a need for something to change, we will change the declaration from ‘const’ to ‘let’ (JS, remember). We can use ‘var’ in JS, but it’s an older practice.

Here’s the fun part: JS is not as strongly typed as C#, an an array in JS can act like a list (change size, size does not have to be declared) and hold any type, and MULTIPLE types of data! It’s nuts!

So, in JS, one could declare a:

const array[userName, pin, dob, occupation, vehicleType, age = currentYear - dob, cityOfResidence];

In this array, we would have:
[
userName: String
pin: int
dob: int
occupation: string
vehicleType: object
age: an expression returning a value to the variable ‘age’
cityOfRedidence: string
]

…and keep adding stuff to it and removing stuff from it, even though it is a const. This is acceptable behavior for an array in JS. Way different from strongly typed C#, and of course, for it’s own reasons.

It’s really cool! A little confusing, but definitely cool!

Also, I’m not a very experienced programmer either. I’ve been studying for a couple of years, but I haven’t earned my entry level job yet. I still have a lot to learn.

Keep at it, and keep making games!

I hope this is helpful!

1 Like

Also, if you declared the int ‘a’ as a const, the program would throw an exception at runtime if you implement something that attempts to change the value of ‘a’.

So, like:
const pistolMagazine = 8; // Would store the value of 8

public void FirePistol(){
pistolMagazine = pistolMagazine - 1; // Would throw an exception
}

Sorry if my syntax is a little off. I haven’t written C# in a while, but I think you’ll get the idea.

Wow, thank you so much for taking the time to go into that level of detail!

Whether the syntax is right or wrong, I still need a little more practice to know better myself! So don’t worry there! I completely understand everything you have said in principle though. I think the pistolMagazine example is a really good way of explaining it. I will remember that.

To be honest, whether I actually apply all of what I learn here into making a game is still questionable. I am a writer and only wanted to learn this on the side in order to be more appealing when applying for a Narrative Design job. Being able to work side-by-side with the technical teams, and understand the language they might use, will definitely be a plus to an employer. As for my final triplex game, I made sure to add some prose flair :wink: (I also second your notions about the use of grammar here… your ‘armed gunman’ made me chuckle, and nod in agreement).

Anyway, thank you again for your help and attention there. I love the engagement with the community so far. Be it here, discord, or on the Facebook page, everyone has been super friendly and talkative. Something I value greatly!

Stay safe, and I hope your entry level job is right around the corner!

James.

1 Like

Thank you James,

Yes, I like to write. It helps me vent…and think about stuff…etc…I mean I don’t really care if anyone reads it at all. I just like to write.

I actually do have my first programming job coming up soon! Kind of…

I live in a fairly small town in Kansas, so these types of jobs just are not around here…not like in bigger places I’ve lived like Orlando or SD.

So I was really digging into the GameDev stuff with the intention of getting Unity certified next year and so on and so forth…then one weekend I saw an ad for a React Native/JavaScript developer and the job is here in my town.

So I bought a React Native class and a JavaScript class on Udemy and the following Monday I went in to the app development place and basically said, “Hey, I know I’m not the guy you’re looking for right now, I don’t know JS nor React Native, I’m not even a very good programmer at the moment, but I study almost every day and I’ve been programming Android apps in AndroidStudio/Java and video games in Unity/C# for a couple of years, and I think that by the end of summer, after I take these two classes, I think I can be the guy you’d want to hire.”

The meeting went well and I’ve been sending him updates and code samples every week or so to show my progress.

You see, I currently work as a plant electrician in a frozen pizza plant, but I want to change careers. I’m a good tech, and I do work among masters, but I work with some green guys and jackasses, too.

I don’t want to lift anything heavier than a laptop anymore, I don’t want to mess with LV or HV 3 Phase anymore, troubleshoot soggy drive motors…I’m not gonna lie, VFDs really aren’t all that complicated, but man they bore the crap out of me.

I really do want to get back on track with building games, but I’d rather take the job that I can right now and work on GameDev as a hobby, rather than remain in a factory for another couple of years while I build a killer game developer portfolio.

I’ve been there for about 2 years now. Therapy helped me get through my first year. GameDev, especially Rick, and more therapy helped me get through last year. I’ve been feeling better, but I know I’m only in that job for the money and benefits.

I care about the work inasmuch as I’m a craftsman and I take pride in the work I do. Other than that, it’s just a building full of greasy, dirty fire hazards and there’s no love lost.

Oh, the React Native gig pays like $20k more than my current position as well. I could get in at the low end of the pay range and still earn an extra $10K for less physical work, and I’d be able to ride my bicycle to work a lot more (or work from home on rainy days) too.

One of these days, after I finish my React Native class and the app shop is ready to hire me, I’ll take all of my vacation days, then come back and put in my notice.

Anyway, yeah, I like to write. It helps me vent and think about stuff. :rofl:

This is also a good place to plug Rick’s “How to get a job in the games industry” course.

Sam has an RPG quest and dialogue course that may interest you as well. I’m not sure if it is more of a ‘writer’s course’, but I do think writing for games is part of it. I bought it, but I haven’t taken it yet.

The others did a good job at explaining what a const is but I want to take a moment to explain why you would want to use it. First let me say that I do not know much about game dev (hence why I am here) but i do use blazor/C# .net core 5 for my day job so I know a little about the subject at hand. So as the others said a constant is similar to a variable since we can assign it a value when coding but differs because we can’t change it’s value at run time (constants are not variables, they are similar to variables). So why would we want to do this rather then using a variable? The answer is memory usage / performance improvements. Basically the system has to allocate x amount of space for a variable and you can’t put more data then the space that is allocated for that variable type. Most of the time however you are not using anywhere near the amount of memory that was allocated for your variable, the system can’t free up that memory though because that value might change. If you define a constant though the system can look at it and say “Oh they only need 5% of the allocated memory, let me free that up so other resources can use it”.

Ok so we know that it helps memory and performance but hard coding it would have an even better effect so why use a constant when you can just write the number or string directly into the formula or into the string? The answer for that is convenience. Let’s say that I am creating a game and this game also has a web component to it whether it be to take the user to the webpage or to pull data from an API (for leader boards, fitbit data, etc). My base path to that website should never change at runtime. I write a class that is specifically for handling things going to and from the site. Since my company will never change I hard code my base path everywhere it is needed in this class, for arguments sake let’s say that we used it in 53 places. Now 2 years down the line I forget to renew my domain name or I merge with another company either way I get a shiny new domain name on a new server. Since I hard coded it I now need to change the domain in 53 places and I need to make sure there are no typos in 53 places. Let’s rewind and say that you used a constant instead. I change the constant in the one place at the top of the class and I am done.

1 Like

Amen!

…and welcome to the community!

Privacy & Terms