result of call warning - want to use the result? - swift3

I have some difficulties with the new Swift 3.0. First I know the “result of call is unused” warning has been debated a few times already. I have read the topics in question and usually the suggested answer is to use #discardableResult or using _ = before the function. But in my case this isn’t working. Before upgrading xCode the code worked great. I tested it on my iPad and everything was ok. But then once I upgraded xCode and had to convert the code to comply with Swift 3.0 the problem appeared.
The thing is that whatever I do, the object in the game isn’t showing as it should. Like I said, previously it worked, but now it doesn’t anymore. When the character runs into the object and it crashes it, the game should display a crashed object, but instead it shows an image with a red “X” as if the image isn’t there.
Here is the code. I would appreciate any suggestion you guys have. Thanks in advance
for block in Blocks
{
block.move()
block.isColB()
if(block.manager.HasFloorL2)
{
if(block.isOnL2())
{
if(block.manager.FloorL2Obstact)
{
block.ObsColL2() // here, result of call to ObscColL2() is unused
}
break;
}
}
if(block.manager.HasFloorL1)
{
if(block.isOnL1())
{
if(block.manager.FloorL1Obstact)
{
block.ObsColL1() // here, result of call to ObscColL1() is unused
}
break;
}
}
}
Here is the function it refers to.
func ObsColL1() -> Bool
{
if(kong.Node.position.x+kong.Node.size.width*0.7 > self.holder.position.x+ObstacleL1.Node.position.x
&& kong.Node.position.x+kong.Node.size.width*0.7 < self.holder.position.x+ObstacleL1.Node.position.x+ObstacleL1.Node.size.width*0.3)
{
if(kong.Y() <= self.holder.position.y+(ObstacleL1.Node.position.y)+ObstacleL1.Node.size.height*0.7 && kong.Y() >= self.holder.position.y+ObstacleL1.Node.position.y)
{
if(kong.state != heroStates.flash && !self.ObstacleL1.crashed)
{
kong.Node.position.x = self.holder.position.x+ObstacleL1.Node.position.x-(kong.Node.size.width*0.55)
kong.Node.run(SKAction.moveTo(y: self.holder.position.y+ObstacleL1.Node.position.y+ObstacleL1.Node.size.height/2, duration: 0.5))
kong.die()
}
else
{
ObstacleL1.crash()
return true
}
}
}
return false
}
EDIT:
before and after
before and after
before and after 2

Related

How to use parameters using MySQL and a TADOQuery instance?

I have a TADOConnection pointing to a MySQL 8.0 instance. The connection is tested and it works. Following this example on how to use prepared statement, I'm having an error and I have no idea why.
The following code works fine, it will return true from the very last statement. No errors, no warnings.
AnsiString sqlQuery = "SELECT e.name FROM employee e WHERE e.id = 1;";
if (!_query->Connection->Connected) {
try {
_query->Connection->Connected = true;
} catch (EADOError& e) {
return false;
}
}
_query->SQL->Clear();
_query->SQL->Add(sqlQuery);
_query->Prepared = true;
try {
_query->Active = true;
if (_query->RecordCount == 0) {
return false;
}
} catch (EADOError& e) {
return false;
}
return true;
However, the following code fails executing _query->SQL->Add(sqlQuery); with this error:
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
AnsiString sqlQuery = "SELECT e.name FROM employee e WHERE e.id = :id;";
if (!_query->Connection->Connected) {
try {
_query->Connection->Connected = true;
} catch (EADOError& e) {
return false;
}
}
_query->SQL->Clear();
_query->SQL->Add(sqlQuery); // <---- EOleException here
_query->Parameters->ParamByName("id")->Value = id;
_query->Prepared = true;
try {
_query->Active = true;
if (_query->RecordCount == 0) {
return false;
}
} catch (EADOError& e) {
return false;
}
return true;
Everywhere I find examples, all of them use :paramName to specify parameters. What am I missing?
Update 1
I have tried changing the code like this :
_query->SQL->Clear();
TParameter * param = _query->Parameters->AddParameter();
param->Name = "id";
param->Value = 1;
_query->SQL->Add(sqlQuery); // <---- EOleException still here
Some forum post suggests to switch the Advanced Compiler option "Register Variables" to "None", but this is already the setting of my project, and the exception is still thrown.
Update 2
I can ignore the error, and everything gets executed just fine, however it fails whenever I perform a step-by-step execution. Of course, I can still put a breakpoint after, and jump right over the faulty line, but it's still annoying and does not explain why there is this error there in the first place.
The exception is on setting the SQL string - which tells you that it's wrong. As per #RogerCigol's comment, you should NOT have the ; at the end of your SQL string.
Kudos to Roger for that.
If you want to access parameters, you MUST set the SQL string first, it will be parsed to identify the parameters. The parameters will not exist until the string is parsed, or you manually create them (which is pointless as they would be recreated on parsing the string).
You can also access the parameters as an ordered index, and I have always been able to use ? as an anonymous parameter with MySQL.

PhysFS_init() returns non-zero with error: "no error"

Im experiencing a rather weird crash in my code and im unsure as to what causes it. Im trying to use PhysFS in my c++ code. the code below is part of a class and Visual Studio 2017 tells me that the crash appears in PHYSFS_mount() and subsequently in EnterCriticalSection(), which to my understanding has something to do with mutexes. Now from my understanding this should be correct (Note that the main calls initArchives() first)
physfs_initialized = false;
...
void scope::parse_archive(const std::string& archive_path, const std::string& path_in_archive)
{
assert(physfs_initialized);
m_archivePath = archive_path;
m_relativeArchivePath = path_in_archive.substr(1);
//fsx = std::filesystem or std::expiremental::filesystem whatever floats your boat
if(exists(fsx::path(archive_path))) return;
if(!PHYSFS_mount(m_archivePath.c_str(),"",0)) return;
const auto file = PHYSFS_openRead(m_relativeArchivePath.c_str());
if(file) m_isValid = true;
PHYSFS_close(file);
PHYSFS_unmount(m_archivePath.c_str());
}
...
void initArchives(char ** argv)
{
if (!PHYSFS_init(argv[0])) physfs_initialized = true;
//a bit of ugly syntax because of the need to consume the return type
atexit([]() {PHYSFS_deinit(); });
}
The crash apparently appears here
int __PHYSFS_platformGrabMutex(void *mutex)
{
EnterCriticalSection((LPCRITICAL_SECTION) mutex); // <-- here
return 1;
} /* __PHYSFS_platformGrabMutex */
Am I doing something wrong here ? Is this a problem of the library or even with my OS ? Was there something in the buildstep of PhysFS that I missed ?
Edit: I noticed that I read the return value of PHYSFS_init() wrong, however now I'm even more confused as PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) returns "no error", what is going on here ?
Apparently there is a bug in PhysFS regarding Windows 10. That prohibits the correct execution of PHYSFS_init()
changing line 578 of phsyfs_platform_windows.c to
rc = pGetDir(accessToken, NULL, &psize);
and a recompile of the library fixed the problem for me :/
https://hg.icculus.org/icculus/physfs/rev/ece6769c0676
https://discourse.libsdl.org/t/resolved-physfs-exception-thrown/25697/11

Swift return error that goes against what the Apple developer site says

func evaluateDate(nDate: Int!, rDate: Int!) -> Int{
if(nDate < rDate) {
return 1
}
if(nDate == rDate) {
return 2
}
if(nDate > rDate) {
return 3
}
}
Every time I get an error that says: "Missing return in a function expected to return 'Int'"
However on the apple developer website they give an example of this doing
func sayHello(personName: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return sayHelloAgain(personName)
} else {
return sayHello(personName)
}
}
print(sayHello("Tim", alreadyGreeted: true))
// prints "Hello again, Tim!"
I don't understand what's going on because also in another place I try to change the value of a variable and it doesn't recognize the variable has changed outside the if-else block, however I don't ever remember this being an issue.
Your if statement structure is incorrect. The compiler is complaining because the if's have no else to fall into, subsequently the func is failing to return in all situations.
func evaluateDate(nDate: Int!, rDate: Int!) -> Int {
if (nDate < rDate) {
return 1
} else if (nDate == rDate) {
return 2
} else {
return 3
}
}
In Apple's example, your code will either execute the if-branch and return, or execute the else-branch and return.
In your code, there is an if and your code may return. If it doesn't return then there is another if where it may return. And another if where it may return. So what happens after the third if?
It may be obvious to you that the third if-branch will be entered and return. It isn't obvious to the compiler. It's not that obvious to you either, because otherwise you wouldn't have written the if at all, but just "return 3".

Switch statement instead of multiple nested if - else?

I've come across a situation where I have a bunch of "systems" that need to be initialized in sequence, with the next system only being initialized if all of the proceeding systems initialized successfully.
This has led me to a whole slew of nested if - else statements. Here's some pseudo-code for visualization.
bool mainInit () {
if (!system1Init ()) {
reportError (); // some error reporting function
}
else {
if (!system2Init ()) {
reportError ();
}
else {
if (!system3Init ()) {
// ... and so on
I find that this starts to look like a mess when you get even a handful of levels to it.
Now I thought of using a switch statement instead, starting at the first case and falling through to the other cases on success, only breaking if there's an error.
bool mainInit () {
switch (1) {
case 1:
if (!system1Init ()) {
reportError ();
break;
}
case 2:
if (!system2Init ())
reportError ();
break;
}
// ....
}
Now, I like this a lot better. I find it much easier to read, especially with some decent comments, but I'm fairly new to programming.
So, my question is: Seeing how this is not how switch statements are traditionally used(at least from what I've seen), is something like this acceptable, or would this be considered bad form?
Being new to programming, I'm trying not to develop too many bad habits that might frustrate and make things more difficult for other programmers down the road.
I did a search, but most of what I found had to do with replacing chains of if - else if statements, not replacing nested ones.
Reference all of the systems in an array, for example an std::vector<mySystem*>, and loop over them sequentially, breaking off on the first fail. This way your entire code is reduced to less than 5 lines of code, even for 500+ systems.
The suggested switch hack is an evil example of XY problem solving: your real problem is that you don't have the array of systems, and are using named variables, thus eliminating all options to more flexibly use all systems, like in a loop.
Assuming that all your system#Init() calls are known at compile time, you can very easily put them in a table and then iterate over that table.
typedef (*system_init)(void);
system_init initialization_functions[] =
{
system1Init,
system2Init,
system3Init,
...
systemNInit
};
bool mainInit()
{
for(size_t idx(0); idx < sizeof(initialization_functions) / sizeof(initialization_functions[0]); ++idx)
{
if(!initialization_functions[idx]())
{
ReportError();
return false;
}
}
return true;
}
However, your existing code looks incorrect since the first mainInit() only calls system1Init() and then exits. Probably not what you wanted in the first place.
if(!system1Init())
{
ReportError();
return false;
}
// if you add an else, the system2Init() does not get called
// even if system1Init() succeeds
if(!system2Init())
{
ReportError();
return false;
}
[...]
return true;
Would the switch answer your problem? Not as it was written. That is, if you wanted to call the mainInit() function with a counter, it could be useful. Drupal uses that mechanism:
bool mainInit(int idx)
{
bool r(true);
switch(idx)
{
case 1:
r = system1Init();
break;
case 2:
r = system2Init();
break;
[...]
}
if(!r)
{
ReportError();
}
return r
}
Note that the table mechanism works the same way as the switch. As long as all the code is found in the systemNInit() functions (and it should be), the switch does not add anything, so you could do something like this too:
bool mainInit(int idx)
{
if(idx < 0 || idx >= sizeof(initialization_functions) / sizeof(initialization_functions[0]))
{
throw std::range_error("index out of bounds");
}
if(!initialization_functions[idx]())
{
ReportError();
return false;
}
return true;
}
Calling the mainInit() with an index can be helpful in case you want to "de-initialize" properly:
int main()
{
for(size_t idx(0); idx < ...; ++idx)
{
if(!mainInit(idx))
{
while(idx > 0)
{
--idx;
mainDeinit(idx);
}
exit(1);
}
}
...app do something here...
}
Use custom exceptions with clear error messages and add a try-catch-report-die around the code in main(). Exceptions are there to specifically make your case look good by making "bad path" implicit.
void initX() { ...; throw std::invalid_argument_exception("..."); }
int main() {
try {
init1(); init2(); ... run();
return 0;
} catch (std::exception const& e) {
log(e.what()); exit 42;
}
}
I'd do it this way:
bool mainInit () {
if (!system1Init ()) {
return(false);
}
if (!system2Init ()) {
return(false);
}
if (!system3Init ()) {
return(false);
}
//...
return(true);
}
//...
if(!mainInit()) {
reportError();
}

if() doesn't seem to work - c++ opengl idle function

I want to make a little animation, a bridge with a traffic light witch warns when the bridge will open (yellow light) , red light while the bridge is animating (just moving u and down) and green light when it is down. For now I just want it to repeat the same animation over and over
In the idle function I have this code :
if(bridgeAnimating==0);
{
printf("\nwtf,%d\n",bridgeAnimating);
fflush(stdout);
startBridge=1;
}
animateBridge();
And this is what I get as output:
wtf,0
wtf,1
wtf,1...etc
bridgeAnimating is a global variable such as startBridge
int startBridge=0;
int bridgeAnimating=0;
And here is the function:
void animateBridge()
{
float static speed=0.25;
int static upwards=1;
double static warnTime=teid;
warnTime-=dt;
if(startBridge==1)
{
upwards=1;
bridgeAnimating=1;
warnTime=teid;
startBridge=0;
//printf("Here:Animating=%d",bridgeAnimating); //if un-commented this gets printed!!!
}
if(bridgeAngle<30 && upwards==1 && warnTime<=0)
{
bridgeAngle+=speed;
red=2;
green=0.9;
orangeRed=0.9;
orangeGreen=0.6;
bridgeAnimating=1;
printf("Upwnwards");
}else if(bridgeAngle>0 && upwards==0 && warnTime<=0)
{
bridgeAngle-=speed;
red=2;
green=0.9;
orangeRed=0.9;
orangeGreen=0.6;
bridgeAnimating=1;
printf("Downwards");
}else if(warnTime>0)
{
orangeRed=2;
orangeGreen=1.19;
red=0.9;
green=0.9;
bridgeAnimating=1;
//printf("Here"); //This gets printed if "un-commented"
}else
{
red=0.9;
green=2;
orangeRed=0.9;
orangeGreen=0.6;
bridgeAnimating=0;
printf("anim 0");//this doesn't print out
}
if(bridgeAngle>=30)
{
upwards=0;
}
}
I checked the document for other references of these 2 variables and there aren't any.
It must be something wrong with the algorithm but I can't figure it out.
I have a lot of global variables, this is the last one I added is it possible that the stack is full?
What is wrong here?
Your problem is this
if(bridgeAnimating==0);
The semicolon on the end indicates an empty statement, which is all that is being controlled by the if statement. Get rid of the ; and it will work as you expect.
Remove the trailing semi-colon:
if(bridgeAnimating==0);