You're probably getting the function prototype, what does it look like?
Lets say string [code]typedef std::basic_string<char, std::char_traits<char>, std::allocator<char>> std::string[/code] or even constructors show code without explanation to what it does.
Because a string isn't a class
[QUOTE=Meatpuppet;39539198]Because a string isn't a class[/QUOTE]
snip
misunderstood
[QUOTE=Meatpuppet;39539198]Because a string isn't a class[/QUOTE]
Uh what
[QUOTE=WeltEnSTurm;39537667]It's a constructor. If you create an object you can pass arguments to it, in this one it just sets self.lyrics to what you pass it[/QUOTE]
Yeah, I get that. But I'm confused as to how passing an argument to Song, automatically (I think) passes it to the lyrics argument in __init__.
[QUOTE=mobrockers2;39539493]Uh what[/QUOTE]
i guess i misunderstood him
[QUOTE=Zally13;39539988]Yeah, I get that. But I'm confused as to how passing an argument to Song, automatically (I think) passes it to the lyrics argument in __init__.[/QUOTE]
I think __init__ is just a "magic" name used by Python. When you create an object you use the nice class name but internally python calls the class' __init__ method.
[QUOTE=Zally13;39539988]Yeah, I get that. But I'm confused as to how passing an argument to Song, automatically (I think) passes it to the lyrics argument in __init__.[/QUOTE]
[cpp]
def __init__(self, lyrics):
self.lyrics = lyrics
[/cpp]
This is the contructor of the class Song. When you make a new Object of the class Song
[cpp]
happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"])
[/cpp]
You actually call the constructor.
[cpp]
self.lyrics = lyrics
[/cpp]
This says that the local variable (self) lyrics = the variable lyrics that holds the data passed through the constructor, you could just name it differently and not bother with it.
Like this:
[cpp]
def __init__(self, passed_lyrics):
self.lyrics = passed_lyrics
[/cpp]
If you do it like this you could even omit the self. as you don't have to show the difference between the local variable and the name of the variable holding the data passed through the constructor.
[editline]10th February 2013[/editline]
This is totally up to you, though most people do use the same name to make it clearer.
-snip-
I added an icon to my Windows program's executable but it doesn't show up on the titlebar or the task switcher. It shows up in the taskbar and Windows explorer just fine (and it works at every size too). What gives?
[b]Edit:[/b]
Figured it out. It's an SFML thing you have to call.
[QUOTE=elevate;39542383]-snip-[/QUOTE]
I hate this, now I'm curious.. :/
[QUOTE=mobrockers2;39544884]I hate this, now I'm curious.. :/[/QUOTE]
Nothing important. But if you really must know, I wanted to see how I could optimize this piece of code.
[cpp]#include <cstdio>
#include <cstring>
#include <fstream>
#include <sys\stat.h>
#include "arg.h"
#include "files.h"
#include "stricmp.h"
using namespace std;
//"ifError" takes all the argument flags, as well as a string to output. If silent flag is
//enabled, then "ifError" will not output that text. I put this into the function to reduce
//lines and characters of code.
void ifError(argFlags& argFlags, char* errorMessage)
{
if (!argFlags.sil)
{
if (!argFlags.err)
{
printf("Converts DOOM WAD into VMF and/or BSP file(s).\n\n");
printf("DOOM2BSP [-silent] [-WAD [path][name]] [-VMF [path]] [-BSP [path]]\n\n");
printf(" -WAD Location of WAD.\n");
printf(" -VMF Create VMF file(s) in path.\n");
printf(" -BSP Create BSP file(s) in path.\n");
printf(" -silent Disable text output.\n\n");
}
printf(errorMessage);
}
argFlags.err = true;
}
//Checks if directory exists.
bool checkDir(char* path)
{
struct stat st;
if (!stat(path, &st))
if (st.st_mode & S_IFDIR != 0)
return true;
return false;
}
//This function analyzes our arguments, deals with them accordingly, and decides if there
//are any errors or not. Also opens wad if it exists.
void handleArgs(argFlags& argFlags, files& files, int& argc, char**& argv)
{
//Analyzes all arguments to find "-silent" arg. If silent arg is found, the silent flag
//is turned on, signifying that this program should not output any text.
for (int arg = 1; arg < argc; ++arg)
if (!stricmp(argv[arg], "-silent"))
argFlags.sil = true;
//Analyzes the rest of the arguments, with the silent flag in mind.
for (int arg = 1; arg < argc; ++arg)
{
//If the "-wad" arg is found, turn on wad flag. Look for wad name. If wad name is
//found, check if for its validity and existance. If check fails or if wad name arg
//is not found, output error.
if (!stricmp(argv[arg], "-wad"))
{
argFlags.wad = true;
if ((arg + 1) < argc)
{
files.wad.open(argv[arg + 1], fstream::out | fstream::binary);
if (!files.wad.good())
ifError(argFlags, "Error: WAD name is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: WAD name is not specified.\n");
}
//If the "-vmf" arg is found, turn on vmf flag. Look for vmf path name. If vmf path
//name is found, check for its validity and existance. If check fails or if vmf
//path arg is not found, output error.
else if (!stricmp(argv[arg], "-vmf"))
{
argFlags.vmf = true;
if ((arg + 1) < argc)
{
files.vmfPath = argv[arg + 1];
if (!checkDir(files.vmfPath))
ifError(argFlags, "Error: VMF path is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: VMF path is not specified.\n");
}
//If the "-bsp" arg is found, turn on bsp flag. Look for bsp path name. If bsp path
//name is found, check for its validity and existance. If check fails or if bsp
//path arg is not found, output error.
else if (!stricmp(argv[arg], "-bsp"))
{
argFlags.bsp = true;
if ((arg + 1) < argc)
{
files.bspPath = argv[arg + 1];
if (!checkDir(files.bspPath))
ifError(argFlags, "Error: BSP path is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: BSP path is not specified.\n");
}
}
//Decently self-explanatory.
if (argc == 1)
ifError(argFlags, "Error: No parameters.\n");
else if (!argFlags.wad)
ifError(argFlags, "Error: WAD parameters missing.\n");
else if ((!argFlags.vmf) && (!argFlags.bsp))
ifError(argFlags, "Error: VMF/BSP parameters missing.\n");
}[/cpp]
Why does the value of i become uninitiated? I am trying to access my map using the [] operator, but since it's 2D I have to chain them. All is well, I need to create a proxy to for the chain. However, when I do, everything works, except the value that is passed down becomes uninitialized.
[cpp]Map::Proxy::Proxy(Map & map, int in): i(in), map(map){
std::cout<<"The value of the value going in is " <<in <<std::endl;
std::cout<<"The value of the member is " << i<<std::endl;
std::cout << this<< std::endl;
}
int Map::Proxy::operator[](int index){
std::cout<<"The value of the member in the operator is " << i << std::endl;
std::cout << this<< std::endl;
return 1;
}
Map::Proxy & Map::operator[](int i){
return Proxy(*this, i);
}[/cpp]
When I test it with [4][4], the values that get printed are:
The value of the value going in is 4
The value of the member is 4
(a pointer)
The value of the member in the operator is (uninitialized data)
(the same pointer)
[QUOTE=elevate;39544898]Nothing important. But if you really must know, I wanted to see how I could optimize this piece of code.
[cpp]void initFiles(argFlags& argFlags, files& files, int& argc, char**& argv)
{
for (int arg = 1; arg < argc; ++arg)
if (!stricmp(argv[arg], "-silent"))
argFlags.sil = true;
for (int arg = 1; arg < argc; ++arg)
{
if (!stricmp(argv[arg], "-wad"))
{
argFlags.wad = true;
if ((arg + 1) < argc)
{
files.wad.open(argv[arg + 1], fstream::out | fstream::binary);
if (!files.wad.good())
ifError(argFlags, "Error: WAD name is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: WAD name is not specified.\n");
}
else if (!stricmp(argv[arg], "-vmf"))
{
argFlags.vmf = true;
if ((arg + 1) < argc)
{
files.vmfPath = argv[arg + 1];
if (!checkDir(files.vmfPath))
ifError(argFlags, "Error: VMF path is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: VMF path is not specified.\n");
}
else if (!stricmp(argv[arg], "-bsp"))
{
argFlags.bsp = true;
if ((arg + 1) < argc)
{
files.bspPath = argv[arg + 1];
if (!checkDir(files.bspPath))
ifError(argFlags, "Error: BSP path is invalid or does not exist.\n");
}
else
ifError(argFlags, "Error: BSP path is not specified.\n");
}
}
if (argc == 1)
ifError(argFlags, "Error: No parameters.\n");
else if (!argFlags.wad)
ifError(argFlags, "Error: WAD parameters missing.\n");
else if ((!argFlags.vmf) && (!argFlags.bsp))
ifError(argFlags, "Error: VMF/BSP parameters missing.\n");
}[/cpp][/QUOTE]
Switch case statements would clean that up a whole lot.
[QUOTE=Meatpuppet;39545404]Switch case statements would clean that up a whole lot.[/QUOTE]
I'm too tired to see how or how to implement it.
If it's not too much to ask, can you show me how by using my program, while I try to continue figuring this out?
Sorry dude, I'm on my phone right now, but I'm sure some of the people in this thread are willing to help. if not, ill help you tomorrow.
I need something to program. I don't have any drive to make my own little games, so I never get around to doing anything. Project Euler was awesome for this at first but it got too hard for me :(. Anyone know of a place with little programming challenges?
make a fully functioning main menu in sfml that responds to mouse hovering over buttons. then you can use that as a base for any 2d game you make in the future.
[QUOTE=WTF Nuke;39545077]Why does the value of i become uninitiated? I am trying to access my map using the [] operator, but since it's 2D I have to chain them. All is well, I need to create a proxy to for the chain. However, when I do, everything works, except the value that is passed down becomes uninitialized.
[cpp]Map::Proxy::Proxy(Map & map, int in): i(in), map(map){
std::cout<<"The value of the value going in is " <<in <<std::endl;
std::cout<<"The value of the member is " << i<<std::endl;
std::cout << this<< std::endl;
}
int Map::Proxy::operator[](int index){
std::cout<<"The value of the member in the operator is " << i << std::endl;
std::cout << this<< std::endl;
return 1;
}
Map::Proxy & Map::operator[](int i){
return Proxy(*this, i);
}[/cpp]
When I test it with [4][4], the values that get printed are:
The value of the value going in is 4
The value of the member is 4
(a pointer)
The value of the member in the operator is (uninitialized data)
(the same pointer)[/QUOTE]
Ok for some reason the data works in release mode but not debug. Can someone explain to me how to fix it in debug and why this is happening?
[QUOTE=mobrockers2;39540258][cpp]
def __init__(self, lyrics):
self.lyrics = lyrics
[/cpp]
This is the contructor of the class Song. When you make a new Object of the class Song
[cpp]
happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"])
[/cpp]
You actually call the constructor.
[cpp]
self.lyrics = lyrics
[/cpp]
This says that the local variable (self) lyrics = the variable lyrics that holds the data passed through the constructor, you could just name it differently and not bother with it.
Like this:
[cpp]
def __init__(self, passed_lyrics):
self.lyrics = passed_lyrics
[/cpp]
If you do it like this you could even omit the self. as you don't have to show the difference between the local variable and the name of the variable holding the data passed through the constructor.
[editline]10th February 2013[/editline]
This is totally up to you, though most people do use the same name to make it clearer.[/QUOTE]
Thanks a ton! This makes sense, I never really got it. Just to be clear, if I pass something to Song's argument, it goes to __init__ as arguments?
[editline]10th February 2013[/editline]
[QUOTE=account;39540207]I think __init__ is just a "magic" name used by Python. When you create an object you use the nice class name but internally python calls the class' __init__ method.[/QUOTE]
And thanks to you too!
[QUOTE=Zally13;39545950]Thanks a ton! This makes sense, I never really got it. Just to be clear, if I pass something to Song's argument, it goes to __init__ as arguments?
[editline]10th February 2013[/editline]
And thanks to you too![/QUOTE]
Yes, because when you create a new Object song you're actually calling the constructor, __init__.
In other languages this is shown much clearer because the constructor method is usually the same name as the class name, why python dev's have decided to call it __init__ is beyond me (I've simply never used python before so I wouldn't know).
In java for example, you'd do:
[cpp]
public class Song {
String lyrics; //You have to declare variable types in Java
public Song(String lyrics) { //This is the constructor, you have to define the input type you expect to receive in Java
this.lyrics = lyrics;
}
}
[/cpp]
[editline]11th February 2013[/editline]
Oh and self is called this in Java I suppose.
Inside C(++) code, how do I redirect the lua-function "print" so I can write what it prints to my own textbox?
The function is already printing its output to another window and I do [I]not [/I]want to break this!
Is there something like "addCallback("print")" ??
[QUOTE=Felheart;39554877]Inside C(++) code, how do I redirect the lua-function "print" so I can write what it prints to my own textbox?
The function is already printing its output to another window and I do [I]not [/I]want to break this!
Is there something like "addCallback("print")" ??[/QUOTE]
Lua hooks straight into the stdio library so if you want to override it you'll need to redirect all printed output from your application into your own textbox.
If you don't want to do that (you most likely dont), then you can either override the print function in lua just like you would any function, by pushing the function and setting as the global "print".
If I were you I'd write a new function and use that for redirecting to your own textbox though, and call it something other than print.
So I'd be hooking the print function. Ok.
In essence I just want to get a callback to my native code whenever print is called so I think that would work very good.
But how do I call my own callback (native c++ code) from lua?
Suppose my new print function is like this:
[code]
function myPrint(arg)
SomehowCallMyNativeCallback(arg) < how?
OriginalPrint(arg)
end
[/code]
edit: got it, lua_register
I've got a basic game engine written in Java, but I'm having a problem with input from the keyboard. For example, when moving the character by holding down the up arrow button, it moves once, pauses shortly, then continues moving. I can't seem to remove the small pause from the movement. So far I've tried this:
[code] //***************************************************************************************
// Method: moveUp()
// Type: void
// Purpose: Decrease the yLoc instance variable from outside the class
//***************************************************************************************
public void moveUp()
{
if (yLoc > 0) //If the paddle isn't at the top of the JFrame, allow it to go up.
{
yLoc -= SPEED; //Move up in increments of SPEED
}
}
//***************************************************************************************
// Method: moveDown()
// Type: void
// Purpose: Increase the yLoc instance variable from outside the class
//***************************************************************************************
public void moveDown()
{
if (yLoc < 448) //If the paddle isn't at the bottom of the JFrame, allow it to go down.
{
yLoc += SPEED; //Move down in increments of SPEED
}
}[/code]
Which directly forces the character to move up or down, and I've tried this:
[code] //***************************************************************************************
// Method: update()
// Type: void
// Purpose: Update the position of the player
//***************************************************************************************
public void update()
{
if (moveUp)
{
yLoc -= SPEED;
}
else if (moveDown)
{
yLoc += SPEED;
}
moveUp = false;
moveDown = false;
}
//***************************************************************************************
// Method: moveUp()
// Type: void
// Purpose: Decrease the yLoc instance variable from outside the class
//***************************************************************************************
public void moveUp()
{
if (yLoc > 0) //If the paddle isn't at the top of the JFrame, allow it to go up.
{
moveUp = true; //Move up in increments of SPEED
}
}
//***************************************************************************************
// Method: moveDown()
// Type: void
// Purpose: Increase the yLoc instance variable from outside the class
//***************************************************************************************
public void moveDown()
{
if (yLoc < 448) //If the paddle isn't at the bottom of the JFrame, allow it to go down.
{
moveDown = true; //Move down in increments of SPEED
}
}[/code]
which tells the player that it will move up or down next time update is called by the main loop. However, the small pause remains with both ways of doing it. Is it something that the KeyListener does by default? Is there any way to circumvent it while still using the Java KeyListener?
The full source is available [url=http://dl.dropbox.com/u/22565769/PongSRC.zip]here[/url]. Please excuse the sloppy coding, as it is an earlier project of mine, but it suffers from the same issue as my later projects.
It sounds to me like you're moving it every key press rather than when the key is pressed, if you get what I'm saying.
[QUOTE=Dr. Evilcop;39556238]I've got a basic game engine written in Java, but I'm having a problem with input from the keyboard. For example, when moving the character by holding down the up arrow button, it moves once, pauses shortly, then continues moving. I can't seem to remove the small pause from the movement.[/QUOTE]
If KeyListener has separate events for when a key is pressed and when it is released, you could handle those separately: set SPEED to something nonzero when the key is pressed, and set it to zero when the key is released.
Hey guys, an account of mine was recently hacked and to combat this I thought i'd make a random password generator in C++. It works somewhat but whenever I run it the string of characters is always the same.
[CODE]while(true)
{
char chL = 'a' + rand() % (('z'-'a') + 1);
char chU = 'A' + rand() % (('Z'-'A') + 1);
char ch = '!' + rand() % ((')'-'!') + 1);
int num = 1 + rand() % ((9-1) + 1);
cout<<chL<<chU<<num<<ch<<endl;
system("pause");
};[/CODE]
[URL="http://gyazo.com/ae5398e0f22a15c7c78e5580e4c668da"]First run[/URL]
[URL="http://gyazo.com/a53e5aa758c8581e4dd88061f26fde9d"]Seccond run[/URL]
Is there any reason for this? how do I fix it.
Thanks
[QUOTE=Shadaez;39556552]It sounds to me like you're moving it every key press rather than when the key is pressed, if you get what I'm saying.[/QUOTE]
[QUOTE=Larikang;39556603]If KeyListener has separate events for when a key is pressed and when it is released, you could handle those separately: set SPEED to something nonzero when the key is pressed, and set it to zero when the key is released.[/QUOTE]
So basically, have yLoc always being mutated by SPEED every frame, and have the KeyEvent change SPEED rather than tell the player to move? Sounds interesting, I'll give it a shot. I'll report back in a few moments.
Sorry, you need to Log In to post a reply to this thread.