Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 653 654 [655] 656 657 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 903950 times)

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9810 on: July 10, 2016, 01:29:23 am »

Thanks for the explanation but that solution didn't seem to work. I removed the LevelMap constructor and moved everything into an initialization function before my game loop. I'm still getting a segfault that's complaining about strlen() whenever I initialize the std::string name in a Tile object. I'm just going to revert to an older build and keep everything awkwardly stuffed in the World object.

On second thought, I have a hunch the problem might be with the struct I'm getting the Tile's name from. I'm defining it globally so it might have the same issue. I'll try that tomorrow!

Success! I finally got rid of that awful world object. I'm initializing all of my globals in the correct order and I can very easily access the current level from anywhere with things like level.getTile(1,1). Thank you for helping me, Reelya.

Shouldn't there be a semi-colon after the end of your class definition?

Sorry, that was a typo when writing my post. All of my class definitions have semicolons afterwards as they should have.
« Last Edit: July 10, 2016, 12:23:13 pm by DragonDePlatino »
Logged

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #9811 on: July 12, 2016, 09:20:45 pm »

After learning that debuggers exist, I've found this error using Firebug:
Spoiler (click to show/hide)

But why should there be a semicolon after "else"? I thought you didn't have to close functions with semicolons in JavaScript? The offending function:
Code: [Select]
function weaponAttack1(might1, strength1, magic1) {
  if (weaponType1 == !null) {
    if (weaponType1 == "Slsh" || "Thru" || "Crsh" || "Side" || "Pier" || "Sper" || "Pole" || "Hack" || "Bldg" || "Thrw" || "Recu" || "Long" || "Blst") {
        attack1 = might1 + strength1;
      } else (weaponType1 == "Crss") {
        attack1 = might1;
      } else (weaponType1 == "Fire" || "Wind" || "Thdr" || "Holy" || "Jdge" || "Ward" || "Dred" || "Drud" || "Anci" || "Heal" || "Ailm" || "Buff") {
        attack1 = might1 + magic1;
}
  }
  return attack1;
}

The real problem (that the above might be related to?) is that when I press the "Battle" button it doesn't pop up the alert window that I'm using for testing, and I'm not sure why it doesn't want to work since it did before. In Firefox's default debugger, I got an error that said the writeStats() function is undefined. What does that mean?

HTML pastebin
JavaScript pastebin

Also, is it okay for me to declare global variables the way I did in the JavaScript file?
« Last Edit: July 12, 2016, 09:25:04 pm by GUNINANRUNIN »
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9812 on: July 12, 2016, 10:12:38 pm »

But why should there be a semicolon after "else"? I thought you didn't have to close functions with semicolons in JavaScript?

That may be the listed error but that's not what's actually wrong. Else doesn't need a conditional. You want else if.
Logged

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #9813 on: July 12, 2016, 10:50:31 pm »

Ah I see, so "if" lets you use conditionals. But yeah there's still the "writeStats() is not defined" problem and I haven't a clue what that means.
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9814 on: July 13, 2016, 12:10:01 am »

Does it still report that if you fix your conditional?  Syntax errors in one part of your code can cause the whole script to misbehave, and it may have given up on parsing the source file because of it.
Logged
Through pain, I find wisdom.

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #9815 on: July 13, 2016, 01:14:21 am »

It didn't. I worked with Skyrunner on it and she figured out that the JS wasn't loading properly. The HTML was loading faster than the JS or something and the button thought the function I wrote didn't exist. It also had something to do with the way I declared my variables.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #9816 on: July 13, 2016, 08:17:39 am »

BTW, yourif statement won't work as intended.

Quote
weaponType1 == "Slsh" || "Thru" || "Crsh" || "Side" || "Pier" || "Sper" || "Pole" || "Hack" || "Bldg" || "Thrw" || "Recu" || "Long" || "Blst"

What happens is first weaponType1 == "Slsh" is evaluated. It will be either true or false. Next, it'll evaluate true || "Slsh" or false || "Slsh". In true case, it'll turn into true (because true OR anything becomes true), and each other string is evaluated afterwards as all true. In the false case... something weird will happen.

First, false || "Thru" becomes "Thru". Why? Javascript shenanigans, I don't know. Next, "Thru" || "Crsh" happens, which ... also becomes "Thru." vOv. This is probably not intended.


To fix it you could either do:

weaponType1 == "Slsh" || weaponType1 == "Thru" || weaponType1 == "Crsh" || ... || weaponType1 == "Blst"


.... or make a list of all the valid types and check if weaponType1 matches any of them.

["Slsh", "Thru", "Crsh", ... "Blst"].indexOf(weaponType1) >= 0

indexOf() finds the index of the argument (weaponType1) if it's in the array, or returns -1 if it's not, hence the >= 0 check.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9817 on: July 13, 2016, 08:39:45 am »

also as a side note using strings in this manner is pretty bad because if you misspell it anywhere it wont work without any errors or any easy way to see what is actually wrong. this is usually referred to as "magic strings" (or "magic numbers")

you probably want to do something like

Code: [Select]
const SLSH = 1;
const THRU = 2;

because this will throw an undefined variable exception if you misspell anything.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9818 on: July 13, 2016, 09:26:56 am »

A better way of doing that would be with enums.

okay, js doesn't have enums directly, but you can do something similar.


Then you can use a switch statement to chose the formula.

Code: [Select]
var Types = {
Slsh:0,
Thru:1,
Crsh:2,
Side:3,
 ....
 Heal:18,
Ailm:19,
Buff:20
}

...
...

switch (weaponType1) {
    case Types.Slsh:
    case Types.Thru:
    case Types.Crsh:
        attack1 = might1 + strength1;
        break;
    case Types.Crss
        attack1 = might1;
        break;
    case Types.Heal:
        attack1 = might1 + magic1;
        break;
}
« Last Edit: July 13, 2016, 09:32:47 am by Japa »
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9819 on: July 13, 2016, 10:08:47 am »

A better way of doing that would be with enums.

okay, js doesn't have enums directly, but you can do something similar.


Then you can use a switch statement to chose the formula.

Code: [Select]
var Types = {
Slsh:0,
Thru:1,
Crsh:2,
Side:3,
 ....
 Heal:18,
Ailm:19,
Buff:20
}

...
...

switch (weaponType1) {
    case Types.Slsh:
    case Types.Thru:
    case Types.Crsh:
        attack1 = might1 + strength1;
        break;
    case Types.Crss
        attack1 = might1;
        break;
    case Types.Heal:
        attack1 = might1 + magic1;
        break;
}

You could use a switch with what miauw62 suggested as well, and your switch wouldn't be littered with "Types." all over the place.
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9820 on: July 13, 2016, 10:52:29 am »

Yeah, I usually prefer using #defines over enums, but JS doesn't have #defines either so...
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #9821 on: July 13, 2016, 11:11:50 am »

I don't like the idea of using a billion consts defined in a file though. A fake enum like Japa said might be a happier middle ground between totally data and totally hardcoded.


Also, #define is evil in a high-level language -- it's entirely type-ignorant, unlike enums which often do have types.


... NOT THAT JAVASCRIPT HAS TYPES OR ANYTHING THAT WOULD BE SILLY
« Last Edit: July 13, 2016, 11:14:12 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #9822 on: July 13, 2016, 12:48:13 pm »

BTW, yourif statement won't work as intended.

Quote
weaponType1 == "Slsh" || "Thru" || "Crsh" || "Side" || "Pier" || "Sper" || "Pole" || "Hack" || "Bldg" || "Thrw" || "Recu" || "Long" || "Blst"

What happens is first weaponType1 == "Slsh" is evaluated. It will be either true or false. Next, it'll evaluate true || "Slsh" or false || "Slsh". In true case, it'll turn into true (because true OR anything becomes true), and each other string is evaluated afterwards as all true. In the false case... something weird will happen.

First, false || "Thru" becomes "Thru". Why? Javascript shenanigans, I don't know. Next, "Thru" || "Crsh" happens, which ... also becomes "Thru." vOv. This is probably not intended.
For specifically this statement, no it wouldn't matter if multiples were found true, but for other ones it totally would. And also yeah that false evaluation sounds like it would break everything.

Thanks for the advice everyone.

Edit: Also I think I'm going to throw all these strings in a dropdown menu anyways to make the end user's life easier because currently yeah he needs to enter these manually, which even a scrub like me can recognize is awful.
« Last Edit: July 13, 2016, 12:56:51 pm by GUNINANRUNIN »
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #9823 on: July 14, 2016, 06:10:30 am »

BTW, yourif statement won't work as intended.

Quote
weaponType1 == "Slsh" || "Thru" || "Crsh" || "Side" || "Pier" || "Sper" || "Pole" || "Hack" || "Bldg" || "Thrw" || "Recu" || "Long" || "Blst"

What happens is first weaponType1 == "Slsh" is evaluated. It will be either true or false. Next, it'll evaluate true || "Slsh" or false || "Slsh". In true case, it'll turn into true (because true OR anything becomes true), and each other string is evaluated afterwards as all true. In the false case... something weird will happen.

First, false || "Thru" becomes "Thru". Why? Javascript shenanigans, I don't know. Next, "Thru" || "Crsh" happens, which ... also becomes "Thru." vOv. This is probably not intended.
For specifically this statement, no it wouldn't matter if multiples were found true, but for other ones it totally would. And also yeah that false evaluation sounds like it would break everything.

Thanks for the advice everyone.

Edit: Also I think I'm going to throw all these strings in a dropdown menu anyways to make the end user's life easier because currently yeah he needs to enter these manually, which even a scrub like me can recognize is awful.

It's not that multiple were found true, it's that the only thing that'll matter in the end is whether weaponType1 == "Slsh" is true or false.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #9824 on: July 14, 2016, 01:27:44 pm »

I see.

This is what I eventually settled on, which fixed the problem of the writeStats() function not loading properly.
Code: [Select]
// call init() when page is loaded
document.addEventListener("DOMContentLoaded", init);

function init() {
//link Battle button to writeStats()
document.getElementById("submit").addEventListener("click", writeStats);
}
Without 'getElementById("submit").' it calls the function no matter where you click. >_>
Logged
Pages: 1 ... 653 654 [655] 656 657 ... 796