• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sobeit

deleting code at run time?

19 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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. 

1

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?

0

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.

0

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.

0

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.

1

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
0

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. 

0

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.

0

Share this post


Link to post
Share on other sites

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. 

 

 

FWIW, there is a "slightly less evil" strategy of making use of function pointers.

 

the basic idea being that the logic is mostly broken down into pieces ("basic operations"), and then structs and function pointers are used to glue everything together. as things change around, then the various structures and function pointers can be changed around, ...

 

when executed, the logic can jump fairly directly from one operation to the next and essentially walk the structure.

 

if done well, this can be fairly flexible and actually a fair bit faster than the "general purpose" logic (lots of complex branching if/else chains or switches).

the drawback, however, is that it is typically much more bulky and nasty-looking, and potentially difficult to understand and debug.

 

generally, I think reorganizing code where possible is preferable to resorting to something like this.

 

 

generally, in my case, it is mostly confined to use in my scripting VM and dynamic type-system and object-system facilities (*1).

 

I had a few times tried using it in image codecs, but typically it is faster/easier just to write special-case versions of functions (say: "hey, we are using 4:2:0 subsampling and YCbCr and an RGBA image buffer" -> use a special version of the logic specifically for this case, falling back to the general case when encountering an unexpected combination of parameters).

 

the tradeoff is that specialized versions of code can also contribute a fair bit of bulk, and are inherently "not really all that flexible" since each only addresses a few specific parameters (say, if we had 3 subsampling modes, 4 color-space transforms, and 4 image-buffer layouts, naively this would mean 48 versions of the image-transform, which is impractical, so usually only 2 or 3 "very common" cases get special handling).

 

sometimes a "mix and match" compromise is possible, with some special-casing of the logic, mixed with the use of function-pointers in other places.

 

 

going beyond this, there is crafting specialized machine-code sequences at run-time, which can execute faster, but opens up a lot of issues (namely portability and complexity). however, machine-code can accomplish a few things which function-pointers can't (it is free from the constraints of the language and ABI, ...).

 

direct self-modifying code and writing into compiler-generated code generally seems like a bad idea IMO.

 

 

*1: actually, they tend to use a mixture of structs and function pointers, and directly-generated native code.

 

in some tests (for script code), using a straight-C route I have gotten within about 10x of native, and about 2x-3x of native C for cases where typically a form of "call-threaded code" is used (the native code = sequence of calls to C functions implementing the logic), with occasional operations being generated directly as machine code (typically things like variable loads/stores and arithmetic operations and similar).

 

typically, this is good enough.

 

a much more "advanced" strategy is to use (or implement) a full compiler, but, this is another level of pain, and adds its own costs.

 

 

using such a code generator for the original problem though would likely be severely overkill.

 

 

or such...

0

Share this post


Link to post
Share on other sites


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.

 

that's nothing to worry about! <g>.

 

just code it. it'll run plenty fast. even if you do a couple hundred checks per frame.

 

you can nest the checks to make it run faster:

 

if (first opponent killed)

    {

     // run second opponent dead check

    }

 

 

in general, the code will probably take the form of:

 

if (achievement A not awarded)

    {

    check for achievement A completion

    }

else 

    {

    check achievements that require A to be completed first

    }

 

 

for example, you only check for kill #2 once kill #1 is competed.

 

if you have achievements for the first 10 kills, at first it just does it does one if (is A done already?), then does the check for achievement A. only at the end does it do 9 if's (A through I completed) and then check for achievement J.

 

you make be able to implement this in a generic manner, with flags for each achievement saying if they're completed. you might also use flags for stuff like "all 10 first kill achievements done".  you'd check this before checking for kills. once the player had 10 kills, the code would just do that one check per frame.

 

i was thinking you might be able to write a slick generic routine that used the completed flags, dependency info (what achievements are prerequisites for which other achievements), and the various achievement checks to process any and all achievements.  but that would require some sort of check function as a variable. granted you could possibly do function variables for each achievement check. i use this in CAVEMAN. one of the fields in an actions database record is a function variable that contains the address of the action handler code. another common way to implement this is with a big switch() statement. if i did it again, i'd just use a big switch:

 

 

int achievement_done(int achievement _num)

switch(achievement_num)

    {

   case 0:

                 // code to check completion of achievement #0 goes here

                return(result)

   case 1:

                 // code to check completion of achievement #1 goes here

                return(result)

   // add additional checks here

   }

1

Share this post


Link to post
Share on other sites

You could maintain a list of achievements you iterate over. When one has been completed remove it from the list to check. No need to hard code each achievement in a giant if/else or case statement. 

0

Share this post


Link to post
Share on other sites

thank you guys! your replies really helped me a lot. I think I've got the basic idea on how to do this.

0

Share this post


Link to post
Share on other sites

You could maintain a list of achievements you iterate over. When one has been completed remove it from the list to check. No need to hard code each achievement in a giant if/else or case statement. 

 

agreed.

 

using a list of objects of some sort would probably be better than hard-coding it, with a class method or similar to determine when the achievement has been completed.

0

Share this post


Link to post
Share on other sites

 if i did it again, i'd just use a big switch:

 

thank you for your advice. I will try it out.

Edited by sobeit
0

Share this post


Link to post
Share on other sites

Assuming C++/Java/C#:

 

I'll second the have some kind of container (vector, list) of objects which are derived from a base class which has a pure virtual

 

bool SatisfiedAchievement()

 

method which returns true if the achievement is satisfied, so it can be removed from the container after it has been completed.

 

But what you don't want is each achievement to be a separate class derived from the base class - most achievements are similar (kill enemy X, collect X items, kill X enemies of type Y, etc.), so you want to have the different types of achievements as classes which take parameters on construction (or a separate initialisation step) which provides the details.

 

You can still have really complicated achievement types to have a class of their own, but these should be an exception rather than the norm. Also consider compound achievements which can have several subtasks (such as kill X goblins and collect Y golden nostrils), you can have a compound achievement class which checks for both (so will contain 2 or more instances of concrete achievement classes as members) which only returns true from SatisifiedAchievement when both tasks are complete.

 

 

If you are using C, you can do the same with function pointers.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites


You could maintain a list of achievements you iterate over. When one has been completed remove it from the list to check. No need to hard code each achievement in a giant if/else or case statement. 

This way, I need to do all the checks in one place which means I need to keep some static variables or database for those events that will trigger achievements, right?

0

Share this post


Link to post
Share on other sites

generally speaking when an event occurs (such as player making 1st kill), you can process it immediately, or you must store the event (somehow - perhaps with an event_X_occurred flag) for later processing.

 

this is one of the design options i tend to ponder over with every game i do. should i process events immediately, or store them for later processing? 

 

processing them immediately means i don't have the extra complexity of storing events which are pending processing. 

 

storing them for later processing means i can process all at once, which may be more efficient and/or a more modular design.

 

but usually, storing them for later processing is more work and complexity, and less efficient as well. its not necessarily overhead that a game can't handle, but essentially, its more work to code, and runs slower, just for the benefit of a more modular design.

 

so i usually process events immediately "and be done with it".

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0