Jump to content
  • Advertisement
Sign in to follow this  
SoulBound

Seed Random, Global objects, and SDL_Threads

This topic is 3504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello GD community, I am a new member and would like to know you all throughout my programming experience. Although, I feel rude for help right away, I have a problem with SDL_thread, random seeding, and global variables for the past few days. I am trying to make a Shoot'em Up in C++, SDL, and with Code::Blocks. The problem I am facing is I am trying to create an Enemy ship that will get a different x offset by using a random number. The problem is the seed does not reach the argument of the object class, because the object is declared as a global variable. The issues is, I need my Enemy object to be created in globals, for it to call it's functions in two separate SDL_threads. //Thread A int threadA(void *data) { myEnemy.functionA(); return 0; } //Thread B int threadB(void *data) { myEnemy.functionB() return 0; } Do I need to create the myEnemy object in the globals in order to call the data member functions into 2 separate threads, or is there a better way? If I can't, is there a way to reach a random seed "srand(time(0))" into the argument of the global object?

Share this post


Link to post
Share on other sites
Advertisement
There are several options.

Option 1: Pass anything you need to into the thread. That data pointer is there for a reason. You'd just have to do something like:

if (data == NULL)
; // Die horribly
EnemyShip* myEnemy = (EnemyShip*)data;
myEnemy->functionA();
// Or better:
EnemyShip& myEnemy = *(EnemyShip*)data;

I don't see why you'd be passing just one ship, but that's the basic idea.

Option 2: Handle seeding in the constructor of your ship using static variables either in the class or in the method. Like this:

EnemyShip::EnemyShip()
{
static bool seeded = false; // This variable is shared between all enemy ships
if (!seeded)
{
srand(time(0));
seeded = true; // By setting this shared variable to true, I ensure srand is only called once.
}
// Use rand()
}

Option 3: Global pointer or data structure. The fact that something is globally accessible doesn't mean you have to construct it straight away. You can have a global pointer which is initially null, or a global vector which starts out empty, and you can fill them out later on. Make sure they're constructed before you start your threads. People generally frown on global variables though.

Share this post


Link to post
Share on other sites
Thank you razorunreal :D, I tried all suggestion and the 2nd one works with what I have right now. I am new to SDL and programming, and don't know what is wrong with globals. I tried the first one, as it looks the most promising, but I did not understand it that well ><. Thanks again :D

Share this post


Link to post
Share on other sites
But you only need to seed rand() once during the lifetime of the program. So in your initialise function or somewhere at the start of the program, you would call srand(time(0)) and that's it. You don't need to do it in a constructor or anything.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!