• 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.

Jemgine

  • entries
    22
  • comments
    7
  • views
    77815

DeyjaScript 7 : Scope fixes and Inheritance

Sign in to follow this  
Followers 0
Deyja

375 views

This entry will be formatted as a series of features, and some notes on their implementation.

First, I created a proper scope stack. This allowed me to fix the problem of variables 'leaking out of' blocks. Code like this used to be possible.


if (condition) {
int x = 5;
}
x = 6; //Just fine!


It takes more work to prevent that sort of thing, but code like that is dangerous enough to warrant it. Lets see what could happen if I allowed it.


if (condition that is false) {
Foo foo = new Foo();
//code that uses foo
}
//many more lines of code
foo.doSomething(); //<- Sometimes foo has been initialized, sometimes it hasn't.


If I detected the use of uninitialized variables like C# does, I could make this particular case an error. But it's easier to just prevent leaking. So, each block pushes a scope onto the scope-stack. Statements will search up the scope stack for the variables they reference. When the function scope is closed, it assigns ids to all the local variables used in all it's sub scopes. It doesn't need a unique id for every variable, it only needs to know the maximum number of variables used at once.

In this example, x and y will be given the exact same id.

if (condition) {
int x;
} else {
int y;
}


All's not well, though, because a naked statement is not a block. There's no block node involved to push scopes. So if I were to do like so


if (condition)
int x;


not only would I be accomplishing absolutely nothing, I'd be leaking x into the enclosing scope. Then consider a for statement, which often has a variable declaration in it, outside the block. I would leak that as well. The solution is for these control structures to also push scopes. In the case of an if statement with a block rather than a single statement, two scopes will be pushed. One of them will remain empty, but that's fine, all of these function-level scopes are discarded when function compilation is finished.

Next I added constructors. This was simple. I modify the grammar so that a new statement expects an argument list, and the node looks for a method on the type called 'construct' and that takes those parameter types. If the constructor was found, it emits bytecode to call the method. If it wasn't and there are no arguments, that's fine. If it wasn't found and there are arguments, it's an error. So all objects have a default constructor that does nothing.

The next feature I implemented was enabled by this scope-stack addition, and it was inheritance. Inheritance can be complicated, so I started with some simple restrictions. First, objects can only derive from one base. This means that I don't need to figure out where in an object's memory layout a particular base is at. The base is always first. I also check for and prevent base loops, where B derives from A, and A derives from B. This is only even possible because I allow objects to be declared in any order. Second, all methods are virtual. There is no additional cost for a virtual function since all method calls already involve looking up the function in a table. My particular implementation, however, prevents calling the base type's version of the method (Something I may want to support, particularly for constructors).

First I inherit member variables. Memory layout is a non-issue. Members of the base are just listed first. For example


A {
int x;
int y;
}

B is an A {
int z;
}


Here, in an instance of B, z will have an index of 2. If I convert a B to an A, x and y will still be in the right place; there will just be an extra member hanging on the end. The scope keeps track of how many member variables it has total so that ScriptObject can make enough space for all of them. I modify the scope to look for variables in base types too (Variables with the same name in derived scopes will hide base members) and it works.

Next, I want to inherit functions. This is a little more complicated because, unlike variables, I actually need to be able to lookup the function at runtime. So I still list base member functions first, but I duplicate the function list into the derived scope, and then add the local functions to it. When I find a local function with the same decorated name as a base function, I replace the base function entry. Since the dynamic type of a script object is stored in the script object, and base members are listed first, I can treat a B as an A, call a method from A, and it will have the same index in B's function list.

I think I have enough now to move on. Next time, I'll see about making sure I can call base implementations of virtual functions. It might also be a good idea if not every function was virtual. And then it's time for modules.

0
Sign in to follow this  
Followers 0


0 Comments


There are no comments to display.

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