Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - dogmaan

Pages: [1]
1
General / The 'new' keyword, factory patterns
« on: January 25, 2010, 07:57:01 pm »
Quote from: "Walker"
Okay, I think the only bit now that I am unsure of is what sort of container to use to store objects and perhaps the actual create object function in the factory. Does the function return the object?

Code: [Select]
return new Pizza(X, Y);

or

Code: [Select]
object = new Pizza(X, Y);
return object;


What is the correct way to use the 'new' keyword?

EDIT: Okay, I managed to create a new object using a function, and add it to an STL vector, now my question is how do I access one of my stored objects?



Code: [Select]

vector <object>      objectVec;
vector <object *>    objectPtrVec;



// if each of your pushed back objects have an update function you would access the first object like this

int i = 0;

// if it is an object
objectVec[i].update();

// if it is a pointer to an object
objectPtrVec[i]->update();

2
General / The 'new' keyword, factory patterns
« on: January 24, 2010, 03:16:48 am »
this is how i do it

Code: [Select]


void scenemanager::addObj_lua (string lab, int X, int Y, string ty)
{
    // if object doesn't exist
if (entityMap.find(ty) == entityMap.end())  
{
//create new master entity
entity * Entity = new entity (*this, App, luaVm, entityFileList[ty], ty, X, Y);
entityMap[ty] = Entity;
entityList.push_back(Entity);
cout << "testing addobj " << entityFileList[ty] << endl;
    }

        else
{
// create a copy of the entity
entityMap[ty]->createEntityCopy(X, Y);
}


}

void entity::createEntityCopy(int X, int Y)
{
if(player)
{
baseEntity * newPlayer = new playerEntity(X, Y, *this);
derivedEntities.push_back (newPlayer);
newPlayer->init();
   }
   else
   if (staticEntity)
   // create a static entity
   etc etc
   
}





each master entity has access to the LuaVm which it pulls it's variables from

3
General / Dynamic KeyBinding via script?
« on: January 23, 2010, 10:49:07 pm »
i'm planning on the game engine only running lua snippets for AI etc, for custom entities, i.e. all my own ingame entities AI will be controlled in C++, but if somebody wants to modify or create their own enemy etc, they can do that by defining a custom lua file to go with their entity

that should only cause a slight performance hit for custom entities, and allow external modders to access an ingame console and also create their own content, without having to worry about delving into the bowels of my C code

4
General / Dynamic KeyBinding via script?
« on: January 22, 2010, 10:47:16 pm »
Quote from: "WitchD0ctor"
how long did it take you to be able to use lua with C++/SFML?


it took me ages (about 2 weeks to get started), considering I started with no knowledge of C, C++, Lua or Luabind.

the need was kind of born out of necessity, as I realised I was hard coding a game engine to only really do one thing, I didn't really fancy my levels and entity data and other variables controls, gravity, friction etc to be hard coded, Lua seemed to be the quickest scripting language that i could use to instantiate objects at runtime using an object factory class.

once you get used to luabind it becomes quite easy to export class functions to Lua, the main problem is using STL containers, i opted to have all level and entity data passed serially, rather than trying to convert a table to a vector or map

i.e.

Code: [Select]


lua:

-- Sprite sheet table
spritesheets =
{
     [1] = "whatever.png";
     [2] = "something.png";
};

-- how many sprite sheets are there?
numOfSpriteSheets = (table.maxn(spritesheets));


function setSprites (entityPtr)

    -- for each sprite sheet export to C class function
    for iterator = 1, numOfSpriteSheets do
         entityPtr:setSpriteSheet_Lua (spritesheets[iterator])  --C++ exported class function
    end
end

c++

class entity
{
    public:
    void setSpriteSheet_Lua (string);

    private:
    vector<string> spritesheets;
};

void entity::setSpriteSheet_Lua (string spritesheet)
{
    spritesheets.push_back (spritesheet);
}




understand though, if you do decide to integrate a scripting language it will probably take you 10x longer than if you where hard coding a game

5
General / Dynamic KeyBinding via script?
« on: January 21, 2010, 12:10:37 am »
Okay I solved the problem

first I created these maps and functions and exported them with luabind, in my game they reside in an input class

Code: [Select]

map <string, char> keyMapChar;
map <string, int>    keyMapInt;

void setCharKeys_lua (string name, string id)
{
const char * buf = id.c_str();

keyMapChar[name] = *buf;
}

void setIntKeys_lua (string name, int id)
{
keyMapInt[name] = id;
}


then these lua functions are called

Code: [Select]

keytable =
{
[1] = {"left", 291 };
[2] = {"right", 292 };
}


function setKeys (inputPtr)
numberofkeys = (table.maxn(keytable))
for key = 1, numberofkeys do

if (type(keytable[key][2]) == ("number"))
then
inputPtr:setIntKeys_lua  ((keytable[key][1]), (keytable[key][2]))
else
if (type(keytable[key][2]) == ("string"))
then
inputPtr:setCharKeys_lua ((keytable[key][1]), (keytable[key][2]))
else
-- insert error function here
end
end
end
keytable = (nil)
end


then I use this ridiculously inefficient way to find if the key is down, I'll optimize it later

Code: [Select]

bool isKeyDown (string key)
{
if (keyMapChar.find(key) != keyMapChar.end())
{
if (App->GetInput().IsKeyDown(sf::Key::Code (keyMapChar[key])))
   {
return true;
   }
else
{
return false;
}
}
else
if (keyMapInt.find(key) != keyMapInt.end())
{
if (App->GetInput().IsKeyDown(sf::Key::Code (keyMapInt[key])))
{
return true;
   }
else
{
return false;
}
}
else
{
cout << "Error, key: " << key << " not found" << endl;
return false;
}

}


if anyone would like a better explanation of this please let me know

6
General / Dynamic KeyBinding via script?
« on: January 10, 2010, 12:21:14 pm »
Hi Everyone,

I'm currently writing a 2d game , i'm using SFML, C++ and Lua + Luabind for scripting.

I can load and draw a level + entities and set resolution, screen depth etc, via Lua script, but I am having trouble thinking of an efficient way of dynamically binding different keys via a script

i am going to use a lua table that will look something like this

Code: [Select]

keys =
{
    ["right"] = "Right Arrow";
    ["left"] = "Left Arrow";
    ["jump"] = "z"
    etc....etc
};


I'm having trouble thinking of a way to compare the above to the SFML enum types sf::key::somekey, so that Right Arrow == sf::Key::Right  dynamically

any ideas?

thanks

Pages: [1]
anything