« first day (62 days earlier)      last day (2796 days later) » 
03:00 - 18:0019:00 - 22:00

user4710450
3:36 AM
Hi all
 
9:05 AM
I actually thought my question on SO wouldn't be downvoted
how naive of me
-2
Q: Best data structure to use for playable regions in a game and how to change behaviour at runtime

exitc0deI am making a Defcon inspired text based nuclear war game where you have to survive with the least casualties and you can upgrade your weapons and form alliances etc. In the game you can play as several regions e.g. Russia, USA, Europe. Each region has access to the same weapons and are exactly t...

 
user4710450
9:31 AM
Yeah you have badly pissed :(
 
user4710450
But I got it to 0 :)
 
user4710450
These days getting downvoted is very typical in these certain areas
 
user4710450
Every question I have asked relating C,C++ was heavily downvoted
 
user4710450
On the other hand, I made another account and asked Java Questions and Guess what ?
I have a +5 question there.
In 13 questions I asked relating to Java only one was -1 all the others were >= 0.
 
user4710450
C,C++ guys are a bit unkind to newcomers :|
 
user4710450
9:34 AM
Thats the reason I guess.
 
it really really sucks
its a brilliant platform
but the mindset is really shitty
just downvote anything that its vaguely broad
downvote anything vaguely opinionated
they should just vote to close it or whatever
not just downvote it
without even commenting why
 
user4710450
Ya
 
user4710450
they really suck
 
user4710450
Some of them are not really helpful
 
user4710450
They only try to find a negative point in ur question and mark it off-topic
 
user4710450
9:48 AM
No, its not broad
 
user4710450
You haven't seen broad questions!
 
10:39 AM
Shall we maybe not abuse the room to rant about unhelpful people. Tends to send the helpful people away
2
 
 
1 hour later…
11:48 AM
Hey
So I am wondering why this program returning null.
All I want is to print a pointer string. :)
 
It doesn't return null
 
It does
Have a look:
Enter name: Gaurav
Your name is (null).sh-4.3$ gcc -o main *.c
That's console output. ^
I tried running the program here: tutorialspoint.com/compile_c_online.php
 
scanf's %s specifier takes a pointer which points to a valid memory area
yours doesn't
also using %s is quite a bad idea since you can't restrict it
you need to use the specifier which takes a length too
I think it was %30s (where 30 is the maximum characters you can store)
...but without accounting for '\0'
 
12:03 PM
also since it's uninitialized, it may point to anything
since it doesn't point to a valid memory, using it is Undefined Behaviour
 
Should I initialized with null or something ?
 
no
8 mins ago, by milleniumbug
scanf's %s specifier takes a pointer which points to a valid memory area
null isn't
 
Then should I initialize with any string ? I mean what is the better way to give the pointer a valid memory as a part of its initialization ?
 
char str[30+1];
scanf("%30s", str);
 
Why is that "+1" ?
 
12:09 PM
8 mins ago, by milleniumbug
...but without accounting for '\0'
 
I see
But my question still there, I want to take a input from string pointer instead of character array.
 
of course if you want longer strings, you need to create a bigger array
there is no "string pointer"
as in, can you be more unclear
As far as C standard I/O and C "string" functions are concerned, string is a memory area filled with bytes, terminated with a zero character '\0'
if it doesn't end with a zero character, it's not a string
 
Thank you.
That really cleared my head.
 
No one likes C strings
3
1/4 questions on tag on SO are asked because the person asking has a problem with C strings
C++ has std::string which is way better
and can be used where C strings are needed thanks to .c_str() function
 
12:28 PM
rn I am studying C.
 
12:51 PM
Should I create the instances of each of my classes in their respective class constructors or in the functions that call them?
Because I want to create the Region class instances (Europe, China etc) as early as possible because they will be used
 
create them somewhere and pass them to what uses them
maybe at the start of the game
 
so in main of main.cpp?
I might make a start function
that just creates all the instances
that makes sense
 
you create them when the player selects "New game"
 
do you think I should capitalize each region instance?
 
do you capitalize your variables? no? then don't
 
12:57 PM
I did get some answers btw
and not too many downvotes
on SO and gamedev
I'm just going to have instances of Region and set a pointer to a region at run-time
rather than bother with a map I think
0
Q: Best data structure to use for playable regions in a game and how to change behaviour at runtime

exitc0deI am making a Defcon inspired text based nuclear war game where you have to survive with the least casualties and you can upgrade your weapons and form alliances etc. In the game you can play as several regions e.g. Russia, USA, Europe. Each region has access to the same weapons and are exactly t...

0
A: How to change a class's variables at runtime without knowing in advance which class's variables to change?

PhilippThere are many ways to build an object hierarchy. There is no one "right" way. Just methods which don't work at all, and you seem to have implemented one, because it doesn't allow you to easily do what you want to do. But this is how I would do it: Make Region a class. All the regions should be...

 
1:27 PM
my playerRegion pointer is going to have to be global isn't it @milleniumbug
 
because lots of different classes need to access it
unless I put it in the region class
 
not an excuse
 
I'll do that
 
no, don't
 
1:28 PM
No
I'll put it in playerRegion
* playerController
 
I'm gonna have to start separating classes into different files now
if I have a vehicle class and it has several child classes like submarine
Should I put those child classes into the same file
even if the file is vehicle.cpp
 
maybe
maybe not
I had my classes for game entities in a single file, but these were quite small
 
Apparently having a WeaponArsenal class was a bit useless
so I integrated Region and WeaponArsenal
but would it be better to nest WeaponArsenal inside Region
because otherwise the Region constructor will look like this:
Region()
{
/* Set region weapon values */

// DEFAULT ICBM VALUES
icbm.setCount(0);
icbm.setCost(120);
icbm.setRange(8000);
icbm.setDamage(80);

// DEFAULT MRBM VALUES
mrbm.setCount(0);
mrbm.setCost(100);
mrbm.setRange(2000);
mrbm.setDamage(70);

// DEFAULT SRBM VALUES
srbm.setCount(0);
srbm.setCost(80);
srbm.setRange(800);
srbm.setDamage(55);
}
and then far longer
 
1:47 PM
@milleniumbug where should I put the region instances?
should I make a RegionHandler class
and then dump the chooseRegion function in there
 
in a std::vector<Region>
 
2:00 PM
I could probably use a static array but what I meant was they need to be accessible from multiple functions
 
pass it by reference
 
wouldn't it be easier to just have a RegionHandler class with all of the regions
I know
I think I know what to do
 
no it wouldn't
 
it wouldn't be easier but I will
because I need an array of all the regions stored somewhere
And it can't be global
so unless I put it in the Region class
I need a class to put them in
 
2:15 PM
Finally sorted some stuff out
now compiles
I've finished the chooseRegion method
 
user4710450
Cool
 
user4710450
Hi all
 
user4710450
Hope u doing great :P
 
I shouldn't make my region array into a map instead should I
then I could call them by their names rather than index in an array
 
@TheLittleNaruto rn you're in a strange room, then
 
2:20 PM
to be honest one of the tags is c @sehe
 
Ok. Fair point :)
 
Region europe;
Region america;
Region china;
Region sea;
Region africa;
Region russia;

Region regions[6] {europe, russia, sea, america, china, africa};
 
I didn't pay attention to the tag mess before.
 
surely this code ^ means I have two copies of each region
 
Indeed. They might share details if the copy is shallow
 
2:21 PM
but I can't declare an array of regions until I define those regions
 
@exitc0de Erm. Region regions[6];
 
Yes but if I remove:
Region europe;
Region america;
Region china;
Region sea;
Region africa;
Region russia;
 
If you needed/used specific ctor args, Region regions[] = { Region("EU"), Region("AM"), ... }
 
oh
I want to store my regions in a static array
@sehe would it be better if I used a map instead
because at the moment this won't work:
void chooseRegion() {

int playerRegionIndex;

cout << "Regions:\n"
<< "0. Europe\n"
<< "1. Russia\n"
<< "2. SEA\n"
<< "3. America\n"
<< "4. China\n"
<< "5. Africa\n";

do {
cout << "Select a region\n> ";
std::cin >> playerRegionIndex; // Get user to input a region int
}
while(playerRegionIndex < 0 || playerRegionIndex > 5);

playerController.playerRegion = &regions[playerRegionIndex];
}
because the playerRegion pointer will point to the member of the array
and not the actual Region
 
okay no
 
2:26 PM
@exitc0de You just won't have those copies. Are you looking for references? coliru.stacked-crooked.com/a/fdf0add66c7a1e49
 
pointers point to objects
member of an array is an object
if your arrays contains Regions, a pointer to an array element will point to a Region
 
yes but what is this doing then:
Region europe;
Region america;
Region china;
Region sea;
Region africa;
Region russia;
if I have to do that for the array to work
 
nothing and being absolutely irrelevant
 
what is the point of the array
 
@exitc0de who said that
 
2:28 PM
main.cpp:146:24: error: ‘europe’ was not declared in this scope
Region regions[6] {europe, russia, sea, america, china, africa};
^
main.cpp:146:32: error: ‘russia’ was not declared in this scope
Region regions[6] {europe, russia, sea, america, china, africa};
^
main.cpp:146:40: error: ‘sea’ was not declared in this scope
Region regions[6] {europe, russia, sea, america, china, africa};
^
main.cpp:146:45: error: ‘america’ was not declared in this scope
Region regions[6] {europe, russia, sea, america, china, africa};
 
5 mins ago, by sehe
If you needed/used specific ctor args, Region regions[] = { Region("EU"), Region("AM"), ... }
 
I completely misread that
thanks
 
@exitc0de depends :) you can reverse it: coliru.stacked-crooked.com/a/0ce08e4fbac0f13f
@exitc0de why are you asking us. It's your array, AFAICT
 
Oh dear
I did that wrong:
main.cpp:139:39: error: no matching function for call to ‘Region::Region(const char [7])’
Region regions[6] {Region("europe"), Region("russia"), Region("sea"), Region("america"), Region("china"), Region("africa")};
^
main.cpp:139:39: note: candidates are:
main.cpp:93:5: note: Region::Region()
Region()
^
main.cpp:93:5: note: candidate expects 0 arguments, 1 provided
main.cpp:79:7: note: constexpr Region::Region(const Region&)
class Region
^
main.cpp:79:7: note: no known conversion for argument 1 from ‘const char [7]’ to ‘const Region&’
 
2:35 PM
> const char [7]
what the hell
 
no dw
 
Use std::string. NOW
 
I know what I did
wait what
 
Oh wait. It's just the error. Let's see
Yup. You don't show the code, that doesn't help. Add a constructor to accept the required arguments, maybe. Or you know, figure it out. I posted two helpful solutions. If you can't apply them to your code, that's mostly because we don't see it.
 
second I'll get the github
 
2:39 PM
I'm not sure that is what is required. But you know. We'll see
 
that's the only file so far
its pretty messy, I'm going to separate stuff out soon
 
@sehe In case you aren't already familiar with the situation, here we have a person trying to make quite a complex game, without having a clear set of requirements beforehand, without knowing how to use arrays
IOW a project destined for failure
 
No
It'll work
its just gonna take a while
And I do know how to use arrays
I just can't make an array of custom data types for some reason
Its the first project that I've actually enjoyed working on as well. Its just a text based game.
I was trying to just store the regions in my game (a region being a class), and store those in an array by the name of those regions but I have to define the different regions before I declare the array so I don't know what the point of the array is
I'm going to change the array to an array of pointers to the region objects @sehe @milleniumbug
 
Region regions[6]; // there you go
@exitc0de get a C++ book seriously
 
I do
I have several
we are misunderstanding each other
I mean when I go Region regions[6];
and I then declare the array
Region regions[] = {europe, america etc}
it doesn't like that I'm using custom data types that don't exist yet
 
2:49 PM
Region regions[6]; is an array declaration
 
I know
But what I want to do is this: Region regions[6] {europe, russia, sea, america, china, africa};
 
no you don't
 
why not?
 
because variable names don't exist at runtime
it literally doesn't matter whether you refer to the region as europe or regions[0]
 
Sorry @milleniumbug now I understand
only problem is referring to europe as regions[0] is a bit difficult to use
 
2:52 PM
20 mins ago, by sehe
@exitc0de depends :) you can reverse it: http://coliru.stacked-crooked.com/a/0ce08e4fbac0f13f
 
could I create a map of regions with their string name and their array indexes?
I think I get it now @milleniumbug
 
34 mins ago, by sehe
@exitc0de Erm. Region regions[6];
 
@sehe ikr
 
sorry it took me a long time to figure out what your code did @sehe
not that code obviously
what does <functional> do btw @sehe
 
it's a header
 
3:05 PM
yes I know
I was wandering what does it imports
 
ah well I was wandering what in @sehe code required <functional>
 
nothing I think
probably didn't bother to remove it
 
dont know whats going on here: { Region{"europe"},Region{"america"},Region{"africa"}, Region{"etcetera"}
because if I do that I just get thousands of errors
 
because you have a user-declared default constructor and you don't have a string member
it's calling a constructor
also you can have non-default constructors (spooky, I know)
24 mins ago, by milleniumbug
@exitc0de get a C++ book seriously
 
3:12 PM
I have some
more than one
I have C++ principals and practices
 
read a C++ book seriously
 
and I have another one
All they teach is syntax and philosophy
 
well you didn't know the syntax right now
and so you didn't know what the code did
 
There is a string member in Region
How might I fix the constructor problem @milleniumbug
 
do you know what a constructor is
 
3:16 PM
yes
I have a constructor in my Region class
Is it a constructor argument?
the Region{""}
still can't figure out what the issue is unfrotunately
@milleniumbug what's the constructor issue?
I need to set the name string in the constructor?
 
if you need to pass the string, then yes
but nobody is saying anything about strings
 
Ok this is the constructor
Region(string n)
{
/* Set region weapon values */
name = n;
// DEFAULT ICBM VALUES
icbm.setCount(0);
icbm.setCost(120);
icbm.setRange(8000);
icbm.setDamage(80);

// DEFAULT MRBM VALUES
mrbm.setCount(0);
mrbm.setCost(100);
mrbm.setRange(2000);
mrbm.setDamage(70);

// DEFAULT SRBM VALUES
srbm.setCount(0);
srbm.setCost(80);
srbm.setRange(800);
srbm.setDamage(55);
}
in Region is string name;
why isn't this working
when I call Region{"Europe"}
in the array
@milleniumbug
 
SSCCE please
 
go read about constructor initializer list
 
3:28 PM
yes I know I shouldn't define stuff in constructor body
but instead in a list
but I will sort that out later
I just want it to compile for now @milleniumbug
main.cpp:137:122: error: too many initializers for ‘Region [0]’
Region regions[] {Region{"europe"},Region{"russia"},Region{"sea"},Region{"america"},Region{"china"},Region{"africa"} };
Whaat?!
ok so I need an initializer list
I can't call a function from an initializer list
so why are there too many initializers
 
because you failed at using the correct syntax
 
waht did I do
this is so annoying
RegionHandler() : Region& europe(regions[EU])
                : Region& europe(regions[RU])
                : Region& europe(regions[SEA])
will that work
+ the {}
@milleniumbug
I have no idea what is wrong
 
22 mins ago, by milleniumbug
read a C++ book seriously
 
ok I promise I will
could you just help with this bit
RegionHandler() : Region& europe(regions[EU]),
                  Region& russia(regions[RU]),
                  Region& sea(regions[SEA]),
                  Region& america(regions[AM]),
                  Region& china(regions[CH]),
                  Region& africa(regions[AF])
that should work now
main.cpp:137:122: error: too many initializers for ‘Region [0]’
Region regions[] {Region{"europe"},Region{"russia"},Region{"sea"},Region{"america"},Region{"china"},Region{"africa"} };
^
main.cpp: In constructor ‘RegionHandler::RegionHandler()’:
main.cpp:141:23: error: type ‘Region’ is not a direct base of ‘RegionHandler’
RegionHandler() : Region& europe(regions[EU]),
^
main.cpp:141:29: error: expected ‘(’ before ‘&’ token
RegionHandler() : Region& europe(regions[EU]),
^
main.cpp:141:29: error: expected ‘{’ before ‘&’ token
any ideas @milleniumbug
 
@exitc0de No, it should not. The syntax for an initializer list is : variable(value), variable(value), ...
 
3:40 PM
thats what I did didn#t it?
oh
Need to declare first
 
no you have put a declarator there
 
@exitc0de Where would Region & fit into that?
 
ok
got it
thank you so much
 
11 mins ago, by exitc0de
main.cpp:137:122: error: too many initializers for ‘Region [0]’
Region regions[] {Region{"europe"},Region{"russia"},Region{"sea"},Region{"america"},Region{"china"},Region{"africa"} };
section "Member initialization"
 
wtf does this mean: main.cpp: In constructor ‘RegionHandler::RegionHandler()’:
main.cpp:153:41: error: no matching function for call to ‘Region::Region()’
africa(regions[AF])
so I have to do this @milleniumbug
                  regions[0](Region{"europe"})
please help I haven't eaten and its nearly 5pm I can't stop until I fix this or its going to really disturb me
Ok really now, this initialization list is really u729452ing tedious, should I just declare all these variables in a namespace which all the region classes are in or will that feck things up more
YESS
YESSSSSSSSS
it FINALLY WORKS
IT ACTUALLY COMPILES
 
4:08 PM
@exitc0de Now that that's out of the way, let me give you some serious advice: don't do this again. In particular, until you really know what you're doing, don't write a giant chunk of code like that without compiling and testing it. Write a little piece. Compile it and test it at least enough to have some confidence that it works. Then do another little piece (and when I say little, I mean something on the order of a function or possibly two--not hundreds of lines of code at once--ever.
Second basic point: when you have some time, you might want to read about pseudo-classes and quasi-classes. Your Weapon is a prime example of a quasi-class that can (and should) be simplified a lot.
Third: RegionHandler's constructor isn't doing what you think it is. It's creating a bunch of references that are local to the function, then at the end of the function destroying all of them. This does not accomplish anything useful.
 
ok
on your third point
@JerryCoffin firstly thank you very much for the advice
but also
where do I put all that stuff if not the constructor
it now looks like this
this should work?
Region& europe;
Region& russia;
Region& sea;
Region& america;
Region& china;
Region& africa;

enum RegionCode { EU, RU, SEA, AM, CH, AF};

Region regions[6];

PlayerController playerController;

RegionHandler() : europe(regions[EU]),
russia(regions[RU]),
sea(regions[SEA]),
america(regions[AM]),
china(regions[CH]),
africa(regions[AF]),
regions {Region{"europe"},Region{"russia"},Region{"sea"},Region{"america"},Region{"china"},Region{"africa"}}
{

}
now the initialization list
is just initializing variables declared in the class
 
@exitc0de That looks reasonable.
 
fantastic
sorry I forgot to update github @JerryCoffin
Ok I need to work on separating my code into files now
 
4:26 PM
@exitc0de For what it's worth, a slightly different take on some parts: coliru.stacked-crooked.com/a/b122c8958b1aa044
 
@Jerry you are all very helpful, thank you for this
 
Surely.
 
if you want you can submit a merge request on github so you get credit @JerryCoffin
 
@exitc0de That's all right.
 
on another note, any C++ book recommendations? I've read the list on SO several times and have tried a few but I don't want something like Principals and Practices with C++ because that's over a thousand pages and most of it I find is a bit too computer sciency
I'd like a super practical book
ideally software engineering/game programming oriented in terms of structure of things
 
4:40 PM
Accelerated C++ is decent to get started with proper C++, but it assumes previous programming experience
 
I'll take another look
thanks @milleniumbug
 
@exitc0de If I knew of other books to recommend, I'd probably add them to that list.
 
I know but its a long list @JerryCoffin
I'm not sure which ones I would prefer
I'll look again
 
@exitc0de It is. I believe I was the one who added Accelerated C++ to the list, so consider this a second (or maybe third) recommendation of it.
 
I must say I'm not entirely sure how your constructor manages to do the same as mine but is far less messy and much simpler
 
4:44 PM
@exitc0de I don't think it does all the same things. As I recall, I eliminated having the aliases referring to the items in the vector/array.
 
I'll read accelerated c++
and then effective modern C++
Eh quick question
so in main:
PlayerController playerController;
cout << playerController.playerRegion->name;
I get a segmentation fault (core dumped)
its the same RegionHandler, I haven't tried yours just yet
@JerryCoffin I never got a segmentation fault
*before
don't worry I don't need to call name anyway
actually it happens with both our code
do I need to use heap?
Welcome to The Contingency Project.
A global nuclear war is about to take place
Will you be able to survive with the smallest population decrease percentage?
Regions:
0. Europe
1. Russia
2. SEA
3. America
4. China
5. Africa
Select a region
> 2

Program received signal SIGSEGV, Segmentation fault.
0x00007f87b1dab45b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
@JerryCoffin
 
03:00 - 18:0019:00 - 22:00

« first day (62 days earlier)      last day (2796 days later) »