Recent posts by bengarney on Kongregate

Subscribe to Recent posts by bengarney on Kongregate

Nov 9, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / c++ programmers

Flash’s vector renderer is written in highly optimized C++. The bottleneck is the script processing you have to do to get the vector renderer the commands it needs to draw a 3d (or any other) scene.

The edge other languages have on Flash in 3d is mostly because they have access to the hardware. But that’s a double edged sword. Flash will give a more consistent experience everywhere because it relies only on the CPU. You can do less, but you can do it almost everywhere. As opposed to using the latest 3d HW features, where you can do awesome stuff on a few computers, and suck everywhere else.

 
Sep 30, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Multiplayer games

I think peer-to-peer connections with Flash will be difficult due to the TCP security restrictions.

 
Sep 14, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Java - worth learning?

Learning Java is free and easy. Why not give it a try?

If you take a college programming class, it’s likely to be in Java. And despite its limitations, it is widely used, and it works pretty well. Sometimes it’s good to learn a “bread and butter” language in addition to fancier stuff.

 
Aug 21, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Better way to code this?

I would go with the third option. It’s the easiest to extend, understand, and maintain. All the state data is out where you can get at it.

The anonymous function route is handy, but in the first case it involves storing data in an invisible scope object that the runtime manages for you. In the second, you’re incurring the (hopefully small) overhead of an anonymous function. In the third there’s nothing special going, just a variable added.

Really any of these routes are fine to use unless we’re talking about hundreds of movie clips or situations where allocation must be extremely fast. Unless you have evidence to show it’s a performance problem, I wouldn’t worry about it.

 
Aug 21, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

It’s probably very helpful when you have a lot of self contained code that does something useful. For instance, Quake has lots of self-contained game logic, and the software renderer “just” blasts bits to a buffer. zlib also doesn’t really talk to anything, just processes chunks of data. So for those sorts of things it is probably quite helpful.

But you probably have to rewrite, or add emulation shim code, anywhere it’s touching external things like the OS.

It’s super cool tech, that I’m excited to see make it out into the world, but it’s no magic wand. ;)

 
Aug 19, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

You could get by with a ByteArray if you wanted to (the guy doing c→as3 compilation does this). In addition, the array object tries hard to keep things contiguous. (see http://hg.mozilla.org/tamarin-central/index.cgi…

Performance isn’t great, compared to native code, but it’s hardly an overcomplicated data structure, especially given the use (and abuse) that Array faces regularly in the flash world.

 
Aug 19, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

Arrays in Flash are dynamically sized. For a guy who’s trying to learn Flash, you really should avail yourself of the docs out there. :)

 
Aug 18, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

@Phantasmagoria: Good way to look at it. Neither of us quite has the full story. :)

On the strings – I hope you’re right that they’re passed by reference. But of course as soon as you modify it it makes a copy so you don’t mess up calling code.

 
Aug 18, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

Yup. You implement them identically.

Personally I would prefer arrays to linked lists as they’re simpler for the garbage collector to process, but I don’t have any data to back that up. And if I was in a situation that really benefited from linked lists I wouldn’t clutter my code trying to avoid it.

 
Aug 17, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

Well, now we’re healthily into nitpicking territory, so let me preface by saying that I don’t think there’s any real disagreement here. The description in the docs, your description, and my code sample are all consistent with one another and it doesn’t affect coding at all. Nor does what I’m about to say :)

To repeat: you and the docs are right that that’s how things appear to work; however while atoms CAN encode references to every type as an immutable separate object, in many situations it’s possible to store the data with no allocation and as a result, the value is encoded directly in the atom. http://hg.mozilla.org/tamarin-central/index.cgi… line 3128 shows the implementation for integers, and http://hg.mozilla.org/tamarin-central/index.cgi… line 366 shows the logic used when loading an integer into the stack. This code path is used when calling a function, and so we can confidently say that in some cases primitives are passed by copy.

In addition, at the beginning of every interp() call, all the passed arguments are boxed via AbstractFunction::boxArgs, which does actually make copies of all the parameters. JIT generated code also uses the normal x86/PPC C/C++ calling conventions, which involve copying parameters in many situations (see the logic in MethodEnv::call for instance).

Indeed, there’s a very fine line between “pass-by-reference-to-an-immutable” and “pass-by-copy” – in fact, nearly zero given that the optimization just described works fine. And, to be clear, “copy of parameter” in many (but not all situations) means a copy of the reference to the parameter, not the parameter itself.

To summarize: AS3 does not always pass reference to an immutable object, however this doesn’t change anything from a user perspective. Things that should act like copies, do, and things that should be references, are.

 
Aug 17, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

Phantasmagoria is right; you should be able to exactly replicate your game logic in AS3. You might not be able to be as clever with your pointers, but that’s what you get for using a managed language – you give up freedom in managing and manipulating memory in exchange for other things.


re: “primitive types are passed by reference”

function foo(blah:int, obj:Point):void { blah = 200; obj.x = 200; obj = null; }
function main():void 
{ 
   var myNumber:int = 1000; 
   var myPoint:Point = new Point(100, 100); 
   foo(myNumber); 
   trace(myNumber + myPoint); 
}

I think it’s actually the opposite. Everything is copied across function call boundaries. But it’s only a shallow copy. So when the function sets obj and blah, the copy is changed and the change does not propagate outside the function’s scope. But when it changes obj.x, the object referenced is changed and the change is visible externally.

You don’t have to take my word for it. Both the AS3 compiler and Tamarin, its runtime, are open source and you can review the actual implementation of the calling conventions.

 
Aug 17, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Porting C++ to ActionScript

ActionScript passes most everything by reference, except for primitives (int, Number, String and such). All objects are passed by reference.

So it does let you do pointer-like things, but you can’t do any problematic things like pointer arithmetic, manual memory allocation, arbitrary type-casting, etc.

Have you considered reading over the AS3 reference docs? They’re pretty good, you’d probably learn a lot. If you’re really serious go buy Essential ActionScript 3.0, it’s a great guide to the whole language.

 
Aug 10, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Costly operations in Flash

I’d double check any advice in that article. Flash has come a long way since version 8. In fact in several places the article mentions that AS3 is exactly the opposite (as it should be since it compiles to native assembly).

I’d especially not trust anything that suggests that something shorter and more cryptic runs faster. In AS2 it does because the compiler is not optimizing. In AS3 the compiler is optimizing so the differences will be less.

Do your own tests. And especially don’t make your code all cryptic before you have to. The value in Flash is that it is flexible and easy to work with. Implementing lots of weird code will just make your life harder when you inevitably have to go back and change something. If AS3 can run Quake and Mode7 in script, it should have enough oomph to do what you need it to as well.

 
Aug 9, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Costly operations in Flash

If you’re really concerned, get Flex Builder Pro and use the profiler. There’s no hard and fast rules. Write your code, then measure it and see if it’s going fast enough. If it is, don’t worry. If it isn’t, optimize.

Make sure to test on a low end computer.

You can also write your own profiler but it might take some care in order to get accurate measurements from it.

 
Aug 1, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Anti-Cheating measures

Remember the last time you went to the museum? How Picasso stood watch over you while you looked at his painting? That time he threw that lady out of the museum because she didn’t appreciate it the right way?

Or maybe you have a print of something by da Vinci. Better not deface it. Leonardo will come by in his crazy flying machine and kill you.

...

Face it, as creators we only have limited control over our creations. Are you going to lock up your creations so no one can enjoy them, least of all you, or put them out in the world and see what happens? There’s a hundred guys alive today that can freehand just as well as da Vinci did. That’s not what made him an artist of incredible renown. It’s the whole package – the genius who moved many fields forward with his study, who painted remarkable art, who had a little mystery (like the Mona Lisa and the backwards writing), who was prolific and let his work exist independent of himself.

 
Jul 11, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / graphics class in AS3

When are you calling beginFill/endFill? You should do them before/after each individual shape, not after all of them, for proper results in this scenario.

What I’d do in the symbol case, is probably draw my graphics into a separate Sprite and then add the graphic as a child of the sprite or vice versa – whichever worked right.

 
Jul 6, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Connecting Flash to a Server

One thing that’s usually a gotcha for me is that Flash’s XML parser wants quotes around every attribute’s value, not just ones that have strings. ie <foo /> isn’t allowed.

 
Jul 6, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Connecting Flash to a Server

Dunno if you’re having formatting issues due to the forums eating it but you have:

<?xml version=”1.0” encoding=”UTF-8”?>
<cross-domain-policy>
    <site-control />
    <allow-access-from>” headers=”*”/>
</cross-domain-policy>

And the allow-access-from line is definitely wrong. But that might be an artifact of the forums.

 
Jun 24, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Duplicating in AS3...

Maybe it’s possible to reduce the amount of stuff you’re drawing? If it takes that long to draw it all it probably is slowing down FPS, since Flash has to redraw it every frame.

This is an annoying gap… the solution I’ve used is to just store the info I need to redraw the shape quickly, then do that when I need a new copy. My code was actually cleaner and easier to follow by the time I did that, so although the lack of a clone() sucked, I just didn’t have much rage left I finished solving the problem. :)

 
Jun 17, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Sniper Class & reload(); makes flash go slow! (AS2.0)

So what piece of gameplay comes next? :)

 
Jun 17, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Sniper Class & reload(); makes flash go slow! (AS2.0)

While there’s certainly no hard technical reason to avoid indentation, it tends to make code that’s easier to read and modify. Consider:

 if(conditionA){
    if(conditionB) {
        if(conditionC) {
            for(var i:int=0; i<1000; i++)
            {
                if(data[i]) == 0)
                {
                    trace("Oh boy!");
                {
                else
                {
                    doAnotherThing(data[i]);
                }
            }
        }
    }
}

as opposed to:

if(!conditionA)
   return;

if(!conditionB)
   return;

if(!conditionC)
   return;

for(var i:int=0; i<1000; i++)
{
   if(data[i]) == 0)
   {
      trace("Oh boy!");
   {
   else
   {
      doAnotherThing(data[i]);
   }
}

Anyway – it’s not a hard rule but something you might want to consider to help keep your code nice and readable.

 
Jun 17, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Pygame, but for C++

Good luck with your games. :)

 
Jun 17, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Sniper Class & reload(); makes flash go slow! (AS2.0)

You might want to consider using more and more meaningful comments. Labelling your functions as functions doesn’t really add a lot of information to the code. Commenting (for example) in playerCrosshair ”// Set up the crosshair and listeners.” helps make it clearer what you’re doing. The idea behind comments is that they explain things that the pure syntax of the code doesn’t tell you already, like the intent of the programmer (that’s you).

You also might want to consider rewriting some of the code so that it’s less deeply indented. For instance, in shootGun(), you could do an if(isReloading) return; right at the top and it would work the same way and be simpler to follow (since you could tell right away the code does nothing if isReloading is true, instead of having to read the whole thing and track the curly braces). Usually when I’m writing production code I try to minimize my indentation levels – if a function gets really deep that’s a clue it ought to be broken up into smaller functions.

Also your class name should have the first letter capitalized.

Those are all minor things, basically you’re doing good. The code doesn’t read like an expert coder’s, but it’s definitely not a newbie’s, either. And the difference to “expert” level is just experience, practice, and learning…

 
Jun 16, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Pygame, but for C++

http://developer.popcap.com/forums/pop_index.php http://garagegames.com/products/torque/tgb/

 
Jun 16, 2008
avatar for bengarney bengarney 28 posts

Topic: Programming / Sniper Class & reload(); makes flash go slow! (AS2.0)

PS – the latest code you posted ought to work OK – although you might want to just check if(isReloading) return so users can’t inadvertantly prevent themselves from shooting by hitting reload inside of a reload…