deleting code at run time?

Started by
18 comments, last by Norman Barrows 10 years, 9 months ago

Hi there,

I'm not sure if this is actually possible to do, deleting some portion of code at run time.

What I want to do is to create a achievement system for my game. Let's say, the player will get an achievement when she kills the first enemy. In this case I need to do an "if test" if(enemyNumber == 1) {.....}. However this portion is completely useless for the rest of the game after she earns this achievement. And I need to do as many this kind of tests as the number of achievements in the game. I wonder if there is a way to deal with such problem, deleting code or changing code at run time? How do you guys normally handle it?

thanks in advance.

Advertisement

Typically, you want to remove your achievements from the actual guts of your game, since acheesements don't really have anything to do with your gameplay. They're just frosting. So your gameplay shouldn't ever be doing anything like "if (achievement criteria==true) then do achievement end" because it just doesn't belong in your gameplay code. Instead, your achievement system should "hook" into your gameplay code and observe things as they happen, siphoning off the events that it cares about. For example:

Every time an enemy is killed, the gameplay system will send some kind of "Enemy Killed" event. This event can be listened for by various subsystems in your game to handle the various things that should happen when an enemy is killed. One of the subsystems that might listen for this event would be the Statistics subsystem, which keeps a count of all enemies killed so that the player can know how awesome she is. Another subsystem listening for that event would be the Achievements subsystem, which would handle that event by passing it on to the various achievements who sign up to listen for it. One of these listening achievements would be the First Kill Achievement which would fire off its payload upon the receipt of an EnemyKilled event, then disable itself (to prevent subsequent events from repeating the payload). This way nothing has to actively poll to see if the achievement should fire, it happens instead by passively listening and you don't waste CPU time in a huge nest of if conditionals.

Of course, this does require that the heart of your gameplay system be event-based, but that's a pretty good idea anyway.

Note that changing code at run time is generally possible, but the ease/difficulty of doing so depends on what programming language you are using. For dynamic languages like Ruby replacing function definitions at run time is essentially trivial. For compiled languages, you have to do evil things like disabling data execution protection and rewriting code pages.


Of course, this does require that the heart of your gameplay system be event-based, but that's a pretty good idea anyway.

thank you, do you know where I can find some example or tutorial on this kind of game design?

What you're proposing sounds like a premature (and probably unnecessary) optimisation.

However, I agree with the spirit of JTippetts response - that you might want to make some sort of declarative or script-based system for handling "achievements" especially if there are a great many of them.

Hi there,

I'm not sure if this is actually possible to do, deleting some portion of code at run time.

Use Lisp.

Or, come back when your game is too slow, and the people here will help you sort it out. Until then, don't think about the cost of your if statements. The event-driven system with listeners would give you something like what you want, but it probably wouldn't be an optimization in any way - a hard-coded string of if statements is much easier to compute than iterating over some list of objects with virtual functions to call.

If there was a speed improvement once you managed to remove enough listeners from the list, it's not a worst-case optimization. It's more like a best case optimization. Best case optimizations are the worst kind - they only help when your game needs the least amount of help. Your game will start out with all the achievements, and so you won't get any savings until the user is deep into the game. If you have performance problems from your achievements, you cannot expect your user to achieve some to help speed up the game for you.

A data-driven system would help you organize your code, so it's worth it for that. I wouldn't want you to not implement something useful like that due to worrying about the cost. I just don't want you to prematurely over-engineer your game either.


However this portion is completely useless for the rest of the game after she earns this achievement. And I need to do as many this kind of tests as the number of achievements in the game. I wonder if there is a way to deal with such problem

in the big picture, its only a problem if your code exceeds ram. you'll want to go with the implementation that gets the job done with the least amount of effort. building games is a big job. you want to work smart, not hard.

the last time i had to seriously engineer around something like that was about 1982 when i was writing my first game, a text mode D&D rpg clone on a Sperry Rand 8088 PC, dos 2.11, and MS Basic. the PC only had 64K of RAM, so you had to use overlay files (modules that paged in and out of memory automatically, with about a 1 second delay). nowadays, windows pages ram as needed, so you have lots of ram, up to the limits of physical ram + max page file size (typically 10% of hard drive? maybe less with today's huge drives).

usually its horrendously huge data structures, not code, that makes you run out of ram.

i've done games as big as 100,000+ lines of code with no problems.

my current main project is at 70,000+ lines of code, and i don't even BEGIN to think about the size of my code segment. OTOH, i HAVE declared data structures that result in "error: program too big to compile", or "data segment exceeds addressable ram", or some such thing. that's when i go for paging strategies.

dynamic allocation exceeding available ram can be an issue.

"code segment too big" almost never is these days.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php


in the big picture, its only a problem if your code exceeds ram.

thank you for the reply, but I don't really worry about "code exceeding ram"(sorry, I didn't state my question clearly enough). my problem is that I need to do many if statement to test whether the player has completed certain achievements, which is unnecessary after the player has completed those achievements. for example

if(KilledTheFirstEnemy == true)

{........}

if(FinishLevelWithin3Munites)

{........}

if(GetANewCostume)

{........}

if(.....)

{........}

the problem is that a player would complete "KilledTheFirstenemy" in the very early stage of the game, but the if test still needs to be executed through out the whole game which is kinda wasting in terms of computation.


A data-driven system would help you organize your code, so it's worth it for that. I wouldn't want you to not implement something useful like that due to worrying about the cost. I just don't want you to prematurely over-engineer your game either.

thank you for your suggest, I would look into these design patterns.


the problem is that a player would complete "KilledTheFirstenemy" in the very early stage of the game, but the if test still needs to be executed through out the whole game which is kinda wasting in terms of computation.

When your player starts the game, you're going to have all the checks in there, right? So, if these checks have some cost (though I don't really think you have to worry about it), that cost will be in full effect at the beginning of your game. I would not attempt any optimization that does not help this worst-case scenario, a scenario that everyone who plays the game will have to experience. If you run into a problem with your achievements, which is possible depending on how you check each of the various ifs (for example, going to a database for each check), you will need to come up with a solution that helps the performance right at the beginning of the game.

My career has been optimization for games. Often, I see other people make optimizations hoping for best-case scenarios. They think, maybe if the player doesn't turn, I don't need to redo this vision check here, so I can skip this, etc. They focus on whatever is easiest to compute, and then let the frame rate suffer when the camera is turning, or when the scene is transitioning, or when there are just too many things happening at once. I really believe it's better to get straight after the worst part, and make sure that works fast enough, before going after any easy targets of opportunity. You should really be focused on how to make sure you can process your maximum number of achievement events quickly enough, instead of thinking about what would be wasted when the number of possible achievements gets smaller over time.

This topic is closed to new replies.

Advertisement