Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 787 788 [789]

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

Uristides

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11820 on: September 03, 2020, 06:52:00 am »

C'mon guys. You mean "?SYNTAX ERROR ON LINE 532" is not enough information?

EDIT: forgot the question mark.
Not when the actual syntax error is on line 520.
Logged

Eschar

  • Bay Watcher
  • Not a Pathos alt
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11821 on: September 03, 2020, 08:38:41 am »

You synner
Logged
"Continue struggling, laws of physics be darned."
 - NRDL
"What, are you stupid or something? Every one of the snake's bones is crushed! Its internal organs are torn apart! There is no way you can - (6) You resuscitate the snake."
- Gatleos

McTraveller

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11822 on: September 03, 2020, 09:20:07 am »

This is an old one, but let me introduce you to this language for dealing with coding errors:

https://github.com/munificent/vigil
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #11823 on: September 03, 2020, 10:08:13 am »

I remember that one :) Honestly I'd love a 'A stupidly strong type system' outside of Haskell. Like being able to define

Code: [Select]
let i = random 0 100 // i is of type int range(0, 100)
let j = random -50 50 // i is of type int range(-50, 50)
let u = random_int () // i is of type int
let k = i + j // k is int range(-50, 150)
let s = i + u // s is just int
let t = if s > 150 then 150 else s // t is range (int.Min, 150)
let q = if t < -100 then -100 else t // q is range(-100, 150)

// or maybe something like
let i = random 0 100
let j = random_int ()
let k = // k is type range(0, 150)
   match i + j with
   | range(0, 100) q -> q // as q is of type int range(0, 100)
   | q -> max 0 (min q 150)  // and result of this is of type int range(0, 150)
let allowed: int range (-200, 200) = k // 0 to 150 is inside the range
let compiler_error:  int range (50, 60) = k // k could be outside the range of 50 to 60 so compiler error

You could create helpers to do that in any language with operator overloading and generics but for a compiler to optimise the overhead would make type-enforced value safety a potentially amazing language feature for guaranteeing correct code.

Imagine a function in a library being able to, at the type level, require the string being passed into a function to be capable of holding a value capable of matching certain a certain regex expression. It'd be physically impossible as a user of that library to get a bug where the wrong string is passed to that function, you'd be forced to have tested the value first.

And even if you used a language feature to bypass that assertion, you would know exactly what could be passed to that function without looking at a line of documentation so you're less likely to misunderstand the functions intended usage.
« Last Edit: September 05, 2020, 06:29:04 am by MorleyDev »
Logged

Sime

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11824 on: September 04, 2020, 08:10:28 am »

I'm addicted to Haskell, all thanks to playing Factorio which led to the natural progression of interests :-

 Factorio --> Petri-Nets --> Linear Logic --> Category Theory --> Haskell.

A  rather steep learning curve tho for a non-functional programmer, putting it mildly.
Logged

Cthulhu

  • Bay Watcher
  • A squid
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11825 on: September 21, 2020, 11:27:15 pm »

I am learning C# for purpose of making silly little games in Unity, possibly something more elaborate once I have a better foundation.  Going pretty well so far, I have this little prototype thing I've been slowly putting together to test how well I understand the stuff I'm learning in videos.  So this is more of an academic question, I ran into something I couldn't figure out and wanted to see if it's doable, and in the meantime I just used if else to make it happen and wait to see if a solution presents itself as I get better.

The prototype thing I'm doing is a little pseudo-adventure game.  There's a player character with six stats, which are determined by his character class.  Originally this was hardcoded, but I've set up a new thing so the game stores the class (RPG term, lowercase) definitions inside dictionaries in a separate Class (programming term, uppercase).  The player Class then has a dictionary for its personal stats.  It selects a random class and populates the dictionary from the global dictionary (using this separate Class setup because at some point I want to have multiple instances of the player character that can have different classes).

If that all makes sense, the code is below.  There's three jobs (going to say job instead of class to avoid confusion), each stores its stats in a dictionary, and the three dictionaries are then in a dictionary of dictionaries, which was mainly to facilitate the thing I failed to do.  The dictionary of dictionaries stores the jobs with integer keys.  My plan was to use this to pick a random integer and then pass it as an argument for the GetPlayerClass function, but that didn't work.  GetPlayerClass(GameControl.playerClasses[x]) would only take a direct reference to the key, I couldn't get it to take an integer variable and recognize what it's supposed to be.  So instead I used the if else thing at the end of the sequence, just a different function call for each class.  My question is, is that possible?  To use a single function call that can pass a randomly selected dictionary as a reference?

Maybe more importantly, is it worth doing that over just doing if else?

Spoiler (click to show/hide)
Logged
Shoes...

McTraveller

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11826 on: September 23, 2020, 07:55:00 am »

@Cthulhu - were you getting errors or just getting an empty dictionary when you tried GameControl.playerClasses[ x ]?

If you were getting an empty dictionary, did you ensure you called GameControl.Start() before you called Player.Start()?
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11827 on: Today at 12:53:50 pm »

Honestly I'd love a 'A stupidly strong type system' outside of Haskell.

Ada says hi. Well, except for the string type bit.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #11828 on: Today at 04:52:59 pm »

Maybe more importantly, is it worth doing that over just doing if else?

Well it depends, if you want to use dictionaries in the future for this kind of stuff then working out what's broken will mean you're using them better. It's never a good idea to have game data hard-coded into a game.

However, you want to incrementally improve the design.

For a start, have you tried this instead of using the if statements:

PlayerClass = Random.Range(0, 2);
GetPlayerClass(GameControl.playerClasses[PlayerClass]);

Then if that works, move that code into a function, inside the same class it's in now.

BTW, this whole way of using string keys is horrible and inefficient. The stats should have int IDs too and have a single table that returns the stat name for any value.
« Last Edit: Today at 04:55:26 pm by Reelya »
Logged
Pages: 1 ... 787 788 [789]