Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Stupid, Clever, or Both?

Name: Anonymous 2007-10-24 2:02

I'm doing some expert programming of a tile-based game in which tile classes have references to anonymous functions that return the proper image for that tile. I figure I might as well not duplicate these. Should I make these functions onymous or just have a picture function manager that lets me get at them easily? It would be dead simple to create onymous functions for them at runtime, but it makes me feel funny to do it.

Name: Anonymous 2007-10-24 2:14

Avoid creating managers. Managers considered harmful.

Name: Anonymous 2007-10-24 2:35

Picture function managers will create synergies and optimize cashflows. You should also use picture function manager factories for maximal ENTERPRISE compliance.

Name: Anonymous 2007-10-24 4:08

>>1
Implement some kind of resource broker, so that a function can say "I want this image", and the broker will reuse it if loaded, or load it if unloaded. The broker should also flush (unload) all resources when told so, which you would e.g. when moving to the next stage.

Name: Anonymous 2007-10-24 5:55

Just stick them in a hash table (or some other kind of mapping data structure, I like rb-trees myself) keyed by filename or something, and consider them read-only after they're loaded from the file. Between level changes (or something) when it's certain that no pointers to the images are around, flush the table.

No need to get fancy. "First, make it work."

Name: Anonymous 2007-10-24 9:11

>>5
That's basically my >>4, only you nigger handle it from all functions together, so instead of

img = GetSprite('image.png')

you do

sprites.setdefault('image.png', LoadImage('image.png'))

and flushing becomes insecure. I prefer the former way, as it's shorter to use, abstracted in a function (implementation details hidden, can change anytime) and is safe to flush. But it's another good way to do so.

Name: Anonymous 2007-10-24 10:30

>>6
Oops, WTF was I doing, strict evaluation is going to rape you in the second case. You should be doing:
if 'image.png' in sprites:
    img = sprites['image.png']
else:
    img = sprites['image.png'] = LoadImage('image.png')

which is a lot uglier and more error prone than
img = GetSprite('image.png')

Name: Anonymous 2007-10-24 11:04

>>7
More like

if 'image.png' not in sprites:
    sprites['image.png'] = load_image('image.png')
img = sprites['image.png']


which is just an s/'image.png'/name and s/img =/return away from a perfectly good definition of get_sprite (or `GetSprite' as some idiots would like to name it.) IOW, not wrapping it in a function would be unnecessary duplication.

Name: Anonymous 2007-10-24 12:34

>>4
Oh, I did that first.
(let ((pictures))
  (defun init-pics ()
    (setf pictures (make-hash-table)))
  (defun get-pic (image-name)
    (gethash image-name pictures))
  (defun load-pic (game-file image-name)
    (setf (gethash image-name pictures)
      (sdl-image:load (game-file game-file)))))

Trivial. (I should probably make this work with setf rather than having both get and load.) But each tile has a function (probably implemented in terms of get-pic) to return its proper image. The upshot of this system is that they can animate themselves with no outside code if I want. The functions don't know how to get the tile's state without a minor rewrite, but they can be swapped out easily, so this isn't really an issue.

The question is whether I should store these in a hash table or just give them names. It seems kind of silly to make a hash table for them when there's already a system for referring to functions by name. On the other hand, it seems strange to use the function system in this way.

Name: Anonymous 2007-10-24 12:48

>>9
If you don't have a way to keep track of all of them, you can't easily perform any ``do this on all pictures'' operations, such as flushing them. Storing them in some kind of container would make it trivial. Also, not using any kind of container would pollute the namespace.

Name: Anonymous 2007-10-24 18:02

>>10
The pictures are stored in the pictures variable up at the top of >>9. The functions that choose which pictures to display at any given moment are the data in question. If I want to iterate over them for some reason, I can put them all in a package together. I can't think why I'd want to destroy one of these functions.

Name: Anonymous 2007-10-24 18:04

>>10
As for "polluting the namespace", when is that ever going to approach being an issue? Am I going to find myself wishing that I hadn't used up pic-fn-lava or something because I have a game routine that totally should be called that?

Name: Anonymous 2007-10-24 18:09

>>12
If your game is very simple, there shouldn't be a problem, but if your game grows a bit, it will quickly get very ugly.

Name: Anonymous 2007-10-24 19:29

>>9
LEARN TO OBJECT ORIENTED

Name: Anonymous 2007-10-24 19:30

>>13
I seriously doubt it will get ugly. Since I haven't heard any convincing arguments to the contrary, I'm going to try simply implementing it with regular named functions. I'll come BAAAW about it here if it doesn't work out.

Name: Anonymous 2007-10-24 19:35

>>14
LEARN TO NOT THROW OBJECTS AT EVERY PROBLEM

What would the advantage of OOP be here?

Name: Anonymous 2007-10-24 20:14

>>16
BECAUSE HE JUST IMPLEMENT A SHITTY OBJECT SYSTEM WITH CLOSURES, USE REAL OBJECT SYSTEM FOOL!±!!!

Name: Anonymous 2007-10-24 22:42

>>17
Who wants to manage objects?

Name: Anonymous 2007-10-24 22:54

Best practice is to use class onymousClassFunctionDispatcherFactorySingleton

Name: Anonymous 2007-10-24 22:59

>>17
there is no advantage between using a shared varible and ZOMG SINGLETONS

Name: Anonymous 2007-10-24 23:31

>>20
Especially when you're not sure what a singleton is.

Name: Anonymous 2007-10-25 2:35

Name: Anonymous 2007-10-25 4:20

>>22
Is all that spamming really woth a $5 steam account? You fail at life faggot.

Name: Anonymous 2007-10-25 5:11

>>23
He has already demonstrated that his time is worthless, so yes probably

Name: Anonymous 2009-11-24 19:25

rfyg

Name: Anonymous 2009-11-24 20:01

>>23
You fail
And you "fail at life" for being a /b/tard

Name: Anonymous 2009-11-24 22:36

>>9
I don't see any reason not to make them onymous; presumable the tile instances (or the classes themselves?) hold persistent references to the functions they need once created, so if you make them onymous now it should be easy to change them to a function hashtable or some such later.

Also, I don't think storing the functions themselves in a hashtable is that unconventional. It gives you a lot of power really easily; you can load them from separate files, you can have a drop-down console to edit them, etc. I suppose you can do that with onymous functions as well, but it doesn't seem as clean that way; with a hashtable you can easily dump the edited versions back out to disk for instance. Like a simple in-app IDE for editing your tile generators.

Reading >>1 again, it seems you're actually generating these functions at runtime. So you're not persisting them? So my above advice is useless then.

Just what exactly is going on here? At runtime, during gameplay, you are somehow automatically generating functions that return tile image references (presumably from some sort of tile cache), and you'd like to not duplicate the instances of these functions as you generate them?

If that's the case, then just memoize the return values of your function generator. That's where your hashtable should be; wrap the generator in a simple function that checks the arguments against a hashtable. If it's not in there, run the generator, catch the return and drop it into the hashtable; if it is there, just return a reference to the function already in the hashtable. No need to name anything. This is of course assuming your function generator has no side effects, and has a single point of entry.

I'd recommend taking a step back and thinking more about what you're doing. This is way too fucking complicated. At least it has assuaged my withdrawl symptoms from lack of /prog/ though, and I like the word "onymous". I wouldn't say no to a more in-depth explanation of your project.

Name: Anonymous 2009-11-24 23:55

Aw fuck off, I wrote all that shit for a goddamn necro thread? assholes

Name: Anonymous 2009-11-25 3:40

>>28
Aww, don't feel bad :)

Name: Anonymous 2009-11-25 4:00

>>28
This is what you get for trying to be helpful on /prog/.

Name: Anonymous 2009-11-25 4:04

I know this is pointless to post, but >>9 would be nicer to be done using some accessor (SETF and normal), or at least something like:

(let ((pictures))
  (defun init-pics ()
    (setf pictures (make-hash-table)))
  (defun get-pic (image-name)
    (symbol-macrolet ((pic (gethash image-name pictures)))
      (or pic
          ;; provide lookup-image somewhere if you need it,
          ;; otherwise replace game-file with image-name
          (let ((game-file (lookup-image image-name)))
            (setf pic (sdl-image:load (game-file game-file))))))))

Which would make loading transparent.

Name: TRUE TRUTH EXPERT !!TthtFzrtPXElUy7 2009-11-25 7:02

>>31
((pictures)) IS THE SAME WITH (pictures) IN THAT CONTEXT. gOOD-HEARTED LOYAL LISPERS DON'T LIKE TO USE TOO MANY PARENTHESENSEIS.

i AND I WRITE THE CODE RASTAFARAAAAAAAAAAAAAAAAAARI

(defun make-pic-db (&rest rest)
  (let (pictures)
  (values
    (lambda () (setf pictures (apply #'make-hash-table rest))
    (lambda (name)
      (or #1=(gethash name pictures)
          (setf #1#
               (sdl-image:load (game-file (lookup-image name))))))))

wARNING NOTE: iF YOU'RE WRITING A PROJECT > 100 LINES, THIS DESIGN ISN'T SUFFICIENT.

Name: Anonymous 2009-11-27 17:32

>>26

Saying that someone "fails at life" is not the same as "hurr durr epic fail lolcats!" "You fail at life" has been an acceptable phrase since before any of us were even born.

Name: Anonymous 2009-11-27 17:43

>>32
wARNING NOTE: iF YOU'RE WRITING A PROJECT > 100 LINES, THIS LANGUAGE ISN'T SUFFICIENT.
Fix'd

Name: Anonymous 2009-11-27 17:50

>>33
not it hasn't. it's gramatically incorrect.
only /b/tards use "fail" in that uniquely stupid way.

Name: sage 2009-11-27 17:57

>>33
Saying that someone "fails at life" is not the same as "hurr durr epic fail lolcats!"
"You fail at life" has been an acceptable phrase since before any of us were even born.

I have to agree on the first point, but as for the second one, you are simply biased because you learned to accept it when you were younger and therefore it seems normal to you now.  I personally think both phrases are /b/tardedly moronic and don't belong outside of grade-school.

Name: Anonymous 2009-11-27 18:34

Op is a school project doing motherfucker, not an expert programmer

Name: Anonymous 2009-11-27 18:34

some time ago i was walking behind a group of teenagers in the city.
the following is what i saw them say and do:
teenager1: is that our bus? run!
teenager1: *starts running to try and catch the bus before it leaves, but doesn't make it in time.
teenager2: FAIL! Garry, Garry, FAIL! You FAIL! HAHAHAHA FAIL! YOU FAIL, Garry. FAIL! EPIC FAIL!

this is an accurate representation of anybody who uses the word "fail" in a /b/tardly manner.

Name: Anonymous 2009-11-27 18:37

posting in a fail thread

Name: Anonymous 2009-11-27 21:45

[b]n[sup]igger[/b]

Name: Anonymous 2009-11-28 10:02

>>38
The only 'fail' I see here is spelling Gary with two [i]r[/i]s.

Name: Anonymous 2009-11-28 11:15

>>40,41
The only ``fail'' I see here is your BBCODE failure.

Name: Anonymous 2010-11-28 16:16

Name: Anonymous 2011-02-04 11:35

Name: Anonymous 2013-04-20 17:59

Name: Anonymous 2013-04-20 19:14

Use an Enum class, e. g.

public enum Picture {
    PIC1 ("picture1.png"),
    PIC2 ("picture2.png");
   
    private String filename;
    private Picture(String f) {
        filename = f;
    }
   
    public String getFilename() {
        return filename;
    }
}

Name: Anonymous 2013-04-20 19:28

Another idea with anonymous functions:

public enum PictureProvider {
    TILE_BLACK {
        @Override
        public String getFilename() {
            String f;
            doSomething( ... );
            return f;
        }
    },
    TILE_WHITE {
        @Override
        public String getFilename() {
            String f;
            doSomethingElse( ... );
            return f;
        }
    };
   
    public abstract String getFilename();
}

Name: Anonymous 2013-04-20 23:41



Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?

 Mail me at jeremiahgoldstein@hotmail.com

 25$ a pop


Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?

 Mail me at jeremiahgoldstein@hotmail.com

 25$ a pop


Searching for legit Microsoft Product keys, Windows 8,7,Studio,Server etc.?

 Mail me at jeremiahgoldstein@hotmail.com

 25$ a pop

Don't change these.
Name: Email:
Entire Thread Thread List