Jump to content
  • Advertisement
Sign in to follow this  
sobeit

deleting code at run time?

This topic is 1764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by sobeit

Share this post


Link to post
Share on other sites
Advertisement

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. 

Share this post


Link to post
Share on other sites


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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

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.

Edited by sobeit

Share this post


Link to post
Share on other sites


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. 

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!