• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Global variables

Explorer ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

Hi, I couldn't find anything in forum (correct me if I'm wrong), regarding storing data on global level. So, my question is, what is the way to do this? I would like to save some random numbers in an array or std::vector and then use them only once in render function, I don't want to call random function each frame, so I need to save it in global variable. SDK Guide doesn't really explain this from what I could find. Thanks in advance!

TOPICS
SDK

Views

173

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 30, 2024 Sep 30, 2024

well, it's not really an issue specific to AE's sdk, but rather a general programming principle.

i'll start with the good news:

no one will die if you'll use some global vars... especially if that data is constant and doesn't change once it's set.

 

global varibales are considered a bad practice for a few reasons:

1. it makes your code less readable, because functions rely on data other than the args passed into them.

so when reading your code, it won't be apparent how the result is reached via

...

Votes

Translate

Translate
Community Expert ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

well, it's not really an issue specific to AE's sdk, but rather a general programming principle.

i'll start with the good news:

no one will die if you'll use some global vars... especially if that data is constant and doesn't change once it's set.

 

global varibales are considered a bad practice for a few reasons:

1. it makes your code less readable, because functions rely on data other than the args passed into them.

so when reading your code, it won't be apparent how the result is reached via the passed args and you'll need to dive into the function.

2. it's might perform slower, because the cpu cache holds the passed args from one ram segment, and then needs to get the global vars from another ram segment.

3. in read/write scenrios, it means that your code is not multithreadable, or must require mutexes that take a performance tax.

4. it's just generally frowned upon.

 

having said that, i'll just note that WE ALL DO IT! yes, even the purists sectretly wat to "just store a couple of bytes"...

there are a couple of things to consider though:

1. if you're using std::vectors, you're not using AE's ram allocation but rather going straight to the system. it's highly recommended that you let AE decide about ram allocation and it's prioritization, so you won't eat more memory than AE wants to use. it's about the user's overall experience.

2. that global will be used by all instances of your effect. if you're cool with that, then fine.

3. that memory will be deallocated only when AE shuts down, even after global_setdown. so even if you allocated memory using AE's suites, at that time, you won't be able to release that memory using AE.

 

if you want to play nice, you can allocate some memory and store it in some pointer on your global_data handle. this would be the most "ram friendly" solution, and would give the appearance of a pro programmer that doesn't use global vars (THOGH WE ALL DO IT!!!).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

Thank you so much for insightful information. Just one more info, it's best to avoid std::vector?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

LATEST

std::vector internally uses malloc (or new...), so as you add stuff to it, it allocates system memory.

you can write an allocator that uses AE mem, and then instanciate std::vector with that allocator. it's a bit of a hassle to set up, but i think it's worth while.

of course, the world won't end if you used std::vector as is... i personally chew through these inconveniences and set them up right. once you're done, you're with it forever. 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines