Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED]Variables & if
Omenapuu Offline
Junior Member

Posts: 31
Threads: 8
Joined: Oct 2013
Reputation: 1
#1
[SOLVED]Variables & if

So I got another problem with scripting. This time I can't get an if statement to work with variables, don't know what's wrong. I looked it over a couple times & still can't figure why isn't it working.

Spoiler below!

AddLocalVarInt("var1", 1);
AddEntityCollideCallback("Player", "ScriptArea_3", "var1", true, 1);
AddEntityCollideCallback("Player", "ScriptArea_4", "scare1", true, 1);


void var1(string &in asParent, string &in asChild, int alState)
{
SetLocalVarInt("var1", 2);
}
void scare1(string &in asParent, string &in asChild, int alState)
{
if(GetLocalVarInt("var1") == 2)
{//INSERT CODE HERE


That isn't straight from the code but I picked up the pieces that aren't functioning.. So what's exactly wrong there? I can't spot it. Probably because I'm not really sure how to use IF statements. Also I made an Else statement after it so it worked fine.. What's wrong with those variables?
(This post was last modified: 01-01-2015, 06:12 PM by Omenapuu.)
12-31-2014, 02:38 PM
Find Reply
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#2
RE: Variables & if

Here an example from 1 of my scripts;
This script will work on my doors to open\close.

OnStart
{
PHP Code: (Select All)
SetLocalVarInt("ElevatorOpen"0); 
}

PHP Code: (Select All)
void elevatordooropenFloor1(string &in asEnitiy)
{
 
 if(
GetLocalVarInt("ElevatorOpen") == 0)
 {
  
SetMoveObjectState("door_security_door_closed_3", -12);
  
SetMoveObjectState("door_security_door_closed_2"12);
  
SetLocalVarInt("ElevatorOpen"1);

 }
 
 else
 {
  
SetMoveObjectState("door_security_door_closed_3"0.7);
  
SetMoveObjectState("door_security_door_closed_2"0.7);
  
SetLocalVarInt("ElevatorOpen"0);

 }


This means ;
IF the doors are closed ADDING a variable to OPEN the doors.
ELSE the doors are closed and setting the variable BACK to 0 = CLOSED.
-
Try and play a little with my copy\past lines and i think you can figur it for the rest of your scriot.
If not, we are here to help.
Good luck!
(This post was last modified: 12-31-2014, 03:09 PM by DnALANGE.)
12-31-2014, 03:04 PM
Find Reply
Omenapuu Offline
Junior Member

Posts: 31
Threads: 8
Joined: Oct 2013
Reputation: 1
#3
RE: Variables & if

(12-31-2014, 03:04 PM)DnALANGE Wrote: Here an example from 1 of my scripts;
This script will work on my doors to open\close.

OnStart
{
PHP Code: (Select All)
SetLocalVarInt("ElevatorOpen"0); 
}

PHP Code: (Select All)
void elevatordooropenFloor1(string &in asEnitiy)
{
 
 if(
GetLocalVarInt("ElevatorOpen") == 0)
 {
  
SetMoveObjectState("door_security_door_closed_3", -12);
  
SetMoveObjectState("door_security_door_closed_2"12);
  
SetLocalVarInt("ElevatorOpen"1);

 }
 
 else
 {
  
SetMoveObjectState("door_security_door_closed_3"0.7);
  
SetMoveObjectState("door_security_door_closed_2"0.7);
  
SetLocalVarInt("ElevatorOpen"0);

 }


This means ;
IF the doors are closed ADDING a variable to OPEN the doors.
ELSE the doors are closed and setting the variable BACK to 0 = CLOSED.
-
Try and play a little with my copy\past lines and i think you can figur it for the rest of your scriot.
If not, we are here to help.
Good luck!

I can't still get it to work.. I honestly have no idea why isn't it working, pretty sure it's just as the one you said..

PHP Code: (Select All)
void var1(string &in asParentstring &in asChildint alState)
{
    
SetLocalVarInt("VARIABLE"3);
}

void scare1(string &in asParentstring &in asChildint alState)
{
    if(
GetLocalVarInt("VARIABLE") != 1)
    {
         
CODE
}


Im pretty sure the problem is somewhere there. No errors or anything, the IF just seems to not give me the value I want, dunno...


http://gyazo.com/4e9fe5f111e5dfce5b956a749255c330 Theres the area.
12-31-2014, 10:07 PM
Find Reply
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#4
RE: Variables & if

You gotta declare your variable before using it ( SetLocalVarInt ).
Did you check that?

01-01-2015, 05:23 AM
Find Reply
Mudbill Offline
Muderator

Posts: 3,877
Threads: 59
Joined: Apr 2013
Reputation: 178
#5
RE: Variables & if

I believe an undeclared variable is automatically set to 0 when used. At least that's the case with AddLocalVarInt. Unsure about the Getter, but either way, if it is undeclared or 0 it will still pass the if statement because it only asks if it ISN'T 1, which is true. I see nowhere that sets this value to 1, making the if statement quite useless.

01-01-2015, 06:05 AM
Find Reply
Omenapuu Offline
Junior Member

Posts: 31
Threads: 8
Joined: Oct 2013
Reputation: 1
#6
RE: Variables & if

(01-01-2015, 06:05 AM)Mudbill Wrote: I believe an undeclared variable is automatically set to 0 when used. At least that's the case with AddLocalVarInt. Unsure about the Getter, but either way, if it is undeclared or 0 it will still pass the if statement because it only asks if it ISN'T 1, which is true. I see nowhere that sets this value to 1, making the if statement quite useless.

I have this at the very beginning:
PHP Code: (Select All)
SetLocalVarInt("VARIABLE"1); 
So It shouldn't be undeclared I think.. I scripted it this way
PHP Code: (Select All)
void var1(string &in asParentstring &in asChildint alState)
{
    
SetLocalVarInt("VARIABLE"3);
}
void scare1(string &in asParentstring &in asChildint alState)
{
    if(
GetLocalVarInt("VARIABLE") != 1)
    {
           
CODE
}


because nothing else seemed to work so I tried to find an alternate route for it to work.. The scripts aren't wrong because I don't get any errors playing it tho.
The
PHP Code: (Select All)
SetLocalVarInt("VARIABLE, 1); 
is working tho because the script doesn't trigger at anytime.
01-01-2015, 12:21 PM
Find Reply
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#7
RE: Variables & if

(01-01-2015, 06:05 AM)Mudbill Wrote: I believe an undeclared variable is automatically set to 0 when used. At least that's the case with AddLocalVarInt. Unsure about the Getter, but either way, if it is undeclared or 0 it will still pass the if statement because it only asks if it ISN'T 1, which is true. I see nowhere that sets this value to 1, making the if statement quite useless.
I thought an undeclared variable -using GetLocalVarInt- blocked the code without raising error.
Just try, put SetLocalVarInt("var1", 2); in your OnStart() function. Then try to trigger that scare1, see if it works.

01-01-2015, 02:44 PM
Find Reply
Omenapuu Offline
Junior Member

Posts: 31
Threads: 8
Joined: Oct 2013
Reputation: 1
#8
RE: Variables & if

(01-01-2015, 02:44 PM)Daemian Wrote:
(01-01-2015, 06:05 AM)Mudbill Wrote: I believe an undeclared variable is automatically set to 0 when used. At least that's the case with AddLocalVarInt. Unsure about the Getter, but either way, if it is undeclared or 0 it will still pass the if statement because it only asks if it ISN'T 1, which is true. I see nowhere that sets this value to 1, making the if statement quite useless.
I thought an undeclared variable -using GetLocalVarInt- blocked the code without raising error.
Just try, put SetLocalVarInt("var1", 2); in your OnStart() function. Then try to trigger that scare1, see if it works.

Yeah, it worked. So the problem is in changing the variable. I'll try to figure them out, but if you can help then here's the code:
PHP Code: (Select All)
AddEntityCollideCallback("Player""ScriptArea_3""var1"true1);
void var1(string &in asParentstring &in asChildint alState)
{
    
SetLocalVarInt("VARIABLE"3);


Here's the area: [Image: 4ce59ce01b6db826a47ddbcb4c0328eb.png]

The one in front of the empty door, down left, is the ScriptArea_3.
(This post was last modified: 01-01-2015, 04:53 PM by Omenapuu.)
01-01-2015, 04:50 PM
Find Reply
Omenapuu Offline
Junior Member

Posts: 31
Threads: 8
Joined: Oct 2013
Reputation: 1
#9
RE: Variables & if

Alright so I pretty much found out why it didn't work...
The script that checked the If-else was triggered by a collide callback. I had made it like that the collide callback removes after first use. And because the first use was supposed to be false until player has done something, the second time it didn't even exist... So yeah I didn't think about that Big Grin
01-01-2015, 06:11 PM
Find Reply
Mudbill Offline
Muderator

Posts: 3,877
Threads: 59
Joined: Apr 2013
Reputation: 178
#10
RE: [SOLVED]Variables & if

Ah yes, that happened to me a few times before as well. Now you know about it, so it probably won't be an issue anymore Smile

01-01-2015, 07:54 PM
Find Reply




Users browsing this thread: 1 Guest(s)