XCode - Redefine macro - objective-c

Small problem I'm having where I have an amount of objects (enemies) on the screen at one particular time and cannot redefine their value. I set my enemies to begin with 3 on the screen.
My objective is to change the amount of enemies based on the current score.
I've attached snippets of the code below.
#define kEnemies 3
- (void) EnemyIncrease
{
if (self.score>=100) {
#define kEnemies 4
}
}
// I've also tried amongst other things
#define kEnemies 3
- (void) EnemyIncrease
{
if (self.score >=100) {
#undef kEnemies
#define kEnemies 6
}
}
Would really appreciate some assistance.
I have now changed my code to the following
int numberOfEnemies;
if (self.score>=0) {
numberOfEnemies = 3
}
else if (self.score>=100) {
numberOfEnemies = 4
}
however the issue now is that the array does not update the numberOfEnemies when the score meets the new condition.
for(int i = 0; i < numberOfEnemies; i++)
Apologies I'm still new to coding and trying to modify existing code

Macros are preprocessed, that means that they are processed before the rest of your code is even compiled.
Taking out your code, that means the preprocessor sees this (using your second example). Ultimately, the value of kEnemies is 6:
#define kEnemies 3
#undef kEnemies
#define kEnemies 6
It's not really viable to use #defines for variables, I only use them for constants.
You could use a member variable:
int numberOfEnemies;
...
if (self.score >=100)
{
numberOfEnemies = 6
}
(I removed the k prefix as this style is intended for constants)

Related

How to Optimally Shift Large Arrays n Number of Incidences

I am creating my own version of a music visualizer that responds to the frequency of music; a common project. I am using 2 strips of Neopixels, each with 300 LEDs making a total of 600 LEDs.
I have written functions, shown below, that create the desired affect of having a pulse of light travel down the strips independently. However, when running in real time with music, the updates per second is too slow to create a nice pulse; it looks choppy.
I believe the problem is the number of operations that must be preformed when the function is called. For each call to the function, a 300 value array per strip must be shifted 5 indices and 5 new values added.
Here is an illustration of how the function currently works:
-Arbitrary numbers are used to fill the array
-A shift of 2 indices shown
-X represents an index with no value assigned
-N represents the new value added by the function
Initial array: [1][3][7][2][9]
Shifted array: [X][X][1][3][7]
New array: [N][N][1][3][7]
Here if my code. Function declarations below loop(). I am using random() to trigger a pulse for testing purposes; no other functions were included for brevity.
#include <FastLED.h>
// ========================= Define setup parameters =========================
#define NUM_LEDS1 300 // Number of LEDS in strip 1
#define NUM_LEDS2 300 // Number of LEDS in strip 1
#define STRIP1_PIN 6 // Pin number for strip 1
#define STRIP2_PIN 10 // Pin number for strip 2
#define s1Band 1 // String 1 band index
#define s2Band 5 // String 2 band index
#define numUpdate 5 // Number of LEDs that will be used for a single pulse
// Colors for strip 1: Band 2 (Index 1)
#define s1R 255
#define s1G 0
#define s1B 0
// Colors for strip 2: Band 6 (Index 5)
#define s2R 0
#define s2G 0
#define s2B 255
// Create the arrays of LEDs
CRGB strip1[NUM_LEDS1];
CRGB strip2[NUM_LEDS2];
void setup() {
FastLED.addLeds<NEOPIXEL, STRIP1_PIN>(strip1, NUM_LEDS1);
FastLED.addLeds<NEOPIXEL, STRIP2_PIN>(strip2, NUM_LEDS2);
FastLED.setBrightness(10);
FastLED.clear();
FastLED.show();
}
void loop() {
int num = random(0, 31);
// Pulse strip based on random number for testing
if (num == 5) {
pulseDownStrip1();
}
pulseBlack1();
}
// ======================= FUNCTION DECLARATIONS =======================
// Pulse a set of colored LEDs down the strip
void pulseDownStrip1() {
// Move all current LED states by n number of leds to be updated
for (int i = NUM_LEDS1 - 1; i >= 0; i--) {
strip1[i] = strip1[i - numUpdate];
}
// Add new LED values to the pulse
for (int j = 0; j < numUpdate; j++) {
strip1[j].setRGB(s1R, s1G, s1B);
}
FastLED.show();
}
// Pulse a set of black LEDs down the strip
void pulseBlack1(){
// Move all current LED states by n number of leds to be updated
for (int i = NUM_LEDS1 - 1; i >= 0; i--) {
strip1[i] = strip1[i - numUpdate];
}
// Add new LED values to the pulse
for (int j = 0; j < numUpdate; j++) {
strip1[j].setRGB(0, 0, 0);
}
FastLED.show();
}
I am looking for any suggestions regarding optimizing this operation. Through my research, copying the desired values to a new array rather than shifting the existing array seems to be a faster operation.
If you have any advice on optimizing this process, or alternate methods to produce the same animation, I would appreciate the help.
The secret is to not shift it. Shift where you start reading it instead. Keep track of a separate variable that keeps the start position and alter your reading through the array to start there, roll back over to zero when it gets to the array length, and stop one short of where it starts.
Google the term "circular buffer" Look at the Arduino HardwareSerial class for a decent implementation example.

Promela atomic propositions for multiple proctype instances

I was wondering how to write never claims that stand for all instances of a proctype. For example if I have the following proposition:
#define c (camera_node[SomePid]:start_publishing == 0)
Now if I instantiate 5 instances of the camera_node how can I create an atomic proposition checking if start_publishing is equal to zero for all of these 5 instances?
Well, it isn't the most beautiful, but I've done this sort of thing in the past. (Note: this code probably isn't proper Promela but you get the point)
#define NUMBER_OF_CAMERA_NODES 5
pid_t cameraPids [NUMBER_OF_CAMERA_NODES];
byte_t cameraPidIndex = 0
active [NUMBER_OF_CAMERA_NODES] proctype cameraTask () {
atomic { cameraPids[cameraPidIndex++] = _pid }
// ...
}
#define cameraCheck( index ) (0 == camera_node[cameraPids[(index)]]:start_publishing)
#define checkAllCameras (cameraCheck(0) && cameraCheck(1) && ...)

Strange Errors in a For Loop

Hello there fellow coders.
I have been learning my way around GLKit over the past few weeks. I found this very helpful series of tutorials on how to set-up a basic 2D graphics engine found here.
When I followed the first chunk of 'Iteration 5' code something strange happened. The for loop in the updateVertices method comes up with compiler errors. Those errors are shown here.
Here's the class code in it's entirety.
//
// Elipse.m
// EmptyGLKit
//
// Created by C-R on 8/6/13.
// Copyright (c) 2013 C-R. All rights reserved.
//
#import "Ellipse.h"
#define ELLIPSE_RESOLUTION 64;
#define M_TAU (2*M_PI)
#implementation Ellipse
-(int)numVertices {
return ELLIPSE_RESOLUTION;
}
-(void)updateVertices {
for (int i = 0; i < ELLIPSE_RESOLUTION; i++) {
float theta = ((float)i) / ELLIPSE_RESOLUTION * M_TAU;
self.vertices[i] = GLKVector2Make(cos(theta)*radiusX, sin(theta)*radiusY);
}
}
-(float)radiusX {
return radiusX;
}
-(void)setRadiusX:(float)_radiusX {
radiusX = _radiusX;
[self updateVertices];
}
-(float)radiusY {
return radiusY;
}
-(void)setRadiusY:(float)_radiusY {
radiusY = _radiusY;
[self updateVertices];
}
#end
I've tried closing and reopening the project, cleaning the code, rebooting Xcode, all without success.
To my knowledge that for loop is completely acceptable and has been in several other projects of mine.
Your #define line has a ; at the end. This isn't correct and should be removed. The #define is basically substituted into the code for compilation so the end result is an if statement with too many ; characters in it.

In Objective-C, how to test against macro definition value?

I'd like to use
#define IS_APP_FULL_VERSION NO
and the code
isAppFullVersion = IS_APP_FULL_VERSION;
to set instance variable. But is there a way to also do
#if IS_APP_FULL_VERSION == "NO"
// add some methods here
#endif
but it would give a compile error, and so is #if (IS_APP_FULL_VERSION == "NO"). Is there a way to check it against YES and NO? (check against the substitution value)
Update: it seems like > and == 0 is allowed, such as
#if IS_APP_FULL_VERSION == 0
so we can actually use 0 and 1 for false and true, and use == 1 to test, but it will be better if YES and NO can be used.
Update 2:
One possible solution turns out to be:
#define IS_APP_FULL_VERSION 1
#if IS_APP_FULL_VERSION
// add some methods here
#endif
isAppFullVersion = IS_APP_FULL_VERSION;
will all work, and we can just change 1 to 0 to toggle the code.
Your basic problem is that the macro processor #if statement only has one data type -- integer. All #if expressions are evaluated in integer, with undefined symbols being replaced with zero. String expressions (including comparisons) cannot be evaluated.
Actually, normally, I would just use a flag macro rather than a value one, since there's only two cases here:
#define APP_IS_FULL_VERSION
Either define or don't define that macro. Then, if you later wanted to include/exclude code based on that, you can use:
#ifdef APP_IS_FULL_VERSION
// Do something
#endif
(or #ifndef for the opposite case, obviously).
For the code segment, it's a simple matter of using that to select the correct code:
#ifdef APP_IS_FULL_VERSION
isAppFullVersion = YES;
#else
isAppFullVersion = NO;
#endif
I think, you're some confused comparison MACRO.
#define format is following it.
#define MACRO_NAME VALUE (VALUE is blank spaces are also fine. MACRO_NAME need is.)
if you want comparison compile-time following like it. this is comparison mean only defined vs no defined. not YES vs NO
#define IS_APP_FULL_VERSION 1
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
#define IS_APP_FULL_VERSION blahblah
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
If you want to build separate versions of. refer a following like it.
#define IS_APP_FREE_VERSION
#if defined IS_APP_FREE_VERSION
NSLog(#"free-version");
#endif
#define IS_APP_FULL_VERSION
#if defined IS_APP_FULL_VERISON
NSLog(#"full-version");
#endif
if you want comparison runtime following like it. this is comparsion normally.
#define IS_APP_FULL_VERSION 1
if(IS_APP_FULL_VERSION)
{
NSLog(#"full-verison");
}
#define IS_APP_FULL_VERSION 0
if(!IS_APP_FULL_VERSION)
{
NSLog(#"no full-version");
}

Checking if it is equal, Normal int and #define macro. but it doesn't work

I have an iVar named,
int DATA_IN_TRANSIT;
and I have defined several macros, e.g.
#define PLACES 0;
When I do something like the following,
if(DATA_IN_TRANSIT == PLACES)
{
NSLog(#"Make LLVM Dance!");
}
I get a compiler error (expression expected) in the line if(DATA_IN_TRANSIT == PLACES)
I don't know why it's giving me an error? Am I doing something naive?
#define PLACES 0
but without ';'
otherwise you'll get
if(DATA_IN_TRANSIT == 0;)
{
NSLog(#"Make LLVM Dance!");
}