Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 324 325 [326] 327 328 ... 796

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

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4875 on: August 31, 2013, 08:34:59 am »

Hmm, I wonder if someone can help me. I'm doing a little game project in C#/XNA, it has a tiled map and I want to be able to put modifications on the tiles to change their resource output. At the moment I have XML files with a list of modifications to each resource, and whether that modification is absolute or not - so for example,
Code: [Select]
+3 +2 +1 true, which would then be evaluated for each mod on the tile.
I want the code to be user-moddable (just because), so I thought maybe it would be better to just have an interface with a method
Code: [Select]
int getResource1(int current), and then call that method on each tile mod, chaining the inputs and outputs:
Code: [Select]
int resource = 0
foreach(Mod m in mods) {
      resource = getResource1(resource);
}
return resource;

But how would I do that in such a way that I don't have to hard-code each modification?

sorry for bad explanation
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4876 on: August 31, 2013, 11:07:46 am »

I'm writing a program in C++ that performs arithmetic functions for my forum game. The function that outputs stuff (in this case the number of structures that an empire has) to another file only does so if the values are greater than zero. I accomplished this with a string of if statements, but I was wondering if I could make a loop that goes through all of the variables in a struct and only prints them if they have a value greater than zero, or some other way of doing things that doesn't involve a chain of if statements.

http://pastebin.com/ta6thm9N
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4877 on: August 31, 2013, 11:18:54 am »

Other than changing it to a map with enums as keys, nope.


I've been working with c# a bit and it's such a delight. Want to convert a string to an int? Just use Convert.ToInt32()! If I want to write to a file, make a StreamWriter and WriteLine(). Also, C#'s intellisense is even better than C++ and cuts down a lot of the typing I do.
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

da_nang

  • Bay Watcher
  • Argonian Overlord
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4878 on: August 31, 2013, 11:32:50 am »

I'm writing a program in C++ that performs arithmetic functions for my forum game. The function that outputs stuff (in this case the number of structures that an empire has) to another file only does so if the values are greater than zero. I accomplished this with a string of if statements, but I was wondering if I could make a loop that goes through all of the variables in a struct and only prints them if they have a value greater than zero, or some other way of doing things that doesn't involve a chain of if statements.

http://pastebin.com/ta6thm9N
I believe this is a case where polymorphism could help.
Logged
"Deliver yesterday, code today, think tomorrow."
Ceterum censeo Unionem Europaeam esse delendam.
Future supplanter of humanity.

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4879 on: August 31, 2013, 05:00:16 pm »

Python problem. I fixed it with a workaround but I would like to know why the following happens
basic version of my code:
Code: [Select]
class ONE:
    def TWO(self, three=[]):
        # sometimes stuff get appended to three in this function
       
    def FOUR(self):
        self.TWO()
Whenever something gets added to the list three, it stays in there, while I expect it to clear itself every time the function ends. Why does it not do that?
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #4880 on: August 31, 2013, 05:15:17 pm »

What would be the fastest way to draw isometric tiles in the correct order?

Currently I am using LINQ's OrderBy, which works, but is not very fast for large numbers of tiles...
Why are you using an OrderBy when you could be using a 2 dimensional array?
This here. Remember, view distance ordering only needs to be correct when the tiles overlap (I'm assuming you're using a grid). So you can just draw them in 2d array reading order, as long as you start with the furthest tile.

I forgot to mention that the world is 3d (x, y, z) and certain objects may exist between tiles.

then use a 3d array, that part doesn't change anything.

And what exactly do you mean by "between" tiles?

like walls? in that case the "wall" can be just another tile. Though then you get into the issue that you have to alternate the width of your tiles.

Another option is for your tile to be more complex structure that keeps a list of decorations present within itself, that way you only need to sort the much smaller number of decorations within a given tile.

Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4881 on: August 31, 2013, 05:17:54 pm »

Python problem. I fixed it with a workaround but I would like to know why the following happens
basic version of my code:
Code: [Select]
class ONE:
    def TWO(self, three=[]):
        # sometimes stuff get appended to three in this function
       
    def FOUR(self):
        self.TWO()
Whenever something gets added to the list three, it stays in there, while I expect it to clear itself every time the function ends. Why does it not do that?

Check this out. Some of my coworkers got bitten by this not too long ago.

A quick fix (even though you've already fixed it) is to give three a default value of None and then check if three has been provided a value.
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4882 on: August 31, 2013, 05:27:35 pm »

Thanks for the link, and that is the quick fix I used as well.

I actually expected it to be a feature related to it being in a class, but I guess I just didn't notice this before in my other functions.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4883 on: August 31, 2013, 08:55:43 pm »

And what exactly do you mean by "between" tiles?

Tiles are generally fixed to a grid, but they can exist between the grid (they use floating point coordinates). For example of something was being moved from tile 0,0,0 to tile 0,0,1 it would be moved smoothly to that location, existing at 0,0,0.5 at some point in time.

Now that I have written code to only display tiles on screen, I might just stick with the OrderBy. After benchmarking it apparently only takes about 2ms to complete...
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4884 on: September 04, 2013, 11:34:58 am »

So I switched over to Ninja as a build tool instead of MinGW Makefiles (I use cmake to generate the build files for both), and sweet jesus the build times are so much smaller.

Running a complete rebuild via the whole
Code: [Select]
cmake .. -G"MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=make -DCMAKE_BUILD_TYPE=Release
make

Would take about 2 minutes. Change it to:
Code: [Select]
cmake .. -G"Ninja" -DCMAKE_BUILD_TYPE=Release
ninja

It takes 30 seconds. Parallelism in builds is awesome. Stupid windows mingw not having support for multiple concurrent jobs...

EDIT: Aaaannnd it crashes when I use Ninja on the ol' Raspberry Pi. Well, not sure what I was expecting xD On linux make supports jobs anyway so it's not as big a deal...
« Last Edit: September 04, 2013, 01:24:26 pm by MorleyDev »
Logged

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4885 on: September 05, 2013, 07:04:26 am »

mingw-make -j works for me.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4886 on: September 05, 2013, 04:17:42 pm »

make -j works but it's a little dangerous, it's got no upper-bound on the number of jobs so crashes on some builds/machines and isn't *the* most efficient way to do it because you've got more jobs than the CPU can actually handle in parallel. ninja, by default, bases it's number of jobs off of the CPU core count of a machine.

make -jN, where N = the number of jobs you want, doesn't work on Windows with recursive Makefiles like the ones cmake typically generates.  Interestingly enough, make "MAKE=make -jN" does work with recursive Makefiles that use the $(MAKE) variable to specify the make program. It's still more exponential than is optimal, since it generates N jobs per sub-makefile. And the link step is still somewhat slow with make.

Basically it works but it's not as good as it could be. For comparison, with UnitTest11 on my machine:
time make "MAKE=make -j10" gives around 27-28 seconds.
time ninja gives around 21-22 seconds.

It makes sense that ninja would do better since it's supposed to be simple, whilst make allows for more complex logic. Ninja just uses dumb final build script for the compiler to work off, cmake handles the actual "is this or this or this or what about this define?" logic.
« Last Edit: September 05, 2013, 06:13:00 pm by MorleyDev »
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4887 on: September 10, 2013, 12:04:04 pm »

In C++,

Code: [Select]
    typedef std::pair<int,int> Point;
    typedef std::map<Point,Cell> CellGrid;
    int px, py;
    for (int x=0; x < width; x++) {
        px = (x + xOff);
        for (int y=0; y < height; y++) {
            py = (y + yOff);
            other->set_cell(px, py, cellGrid[Point(x,y)]);
        }
    }


Is Point(x,y) guaranteed to be executed before cellGrid[]?
« Last Edit: September 10, 2013, 12:05:57 pm by SethCreiyd »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4888 on: September 10, 2013, 12:17:18 pm »

IIRC, that's compiler specific behavior, but Point(x,y) is standard. Or something like that, it's all off the top of my head and fuzzy.

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #4889 on: September 10, 2013, 01:32:58 pm »

You can't call a function without knowing its arguments (in C++ at least), so Point(x,y) will always be called first.
Logged
Pages: 1 ... 324 325 [326] 327 328 ... 796