Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075922 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:48:30 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Strange problem with variables
Pages: [1]
Print
Author Topic: Strange problem with variables  (Read 900 times)
C.A. Silbereisen
Schlagerstar
Global Moderator
Level 10
******


eurovision winner 2014


View Profile
« on: December 23, 2009, 06:59:16 AM »

So, for my Assemblee entry I'm currently trying to make a wall object that gets destroyed when the player collides with a certain part of it. Seems simple, I know but I'm having a really weird problem.

I put the following 4 variables in the Create Event of the breakable wall object to describe the two points needed for a collision rectangle.
Code:
r1=x
r2=y
r3=x+7
r4=y+1

Then I put the following code in the Step Event to handle the collision:
Code:
if collision_rectangle(r1,r2,r3,r4,player,0,0)
{
sound_play(snd_wallcreate)
instance_destroy()
}

But when the wall object enters the view (I disable all instances outside the view), get this error:
Code:
for object breakwall_u:

Error in code at line 1:
   if collision_rectangle(r1,r2,r3,r4,player,0,0)
at position 24: Unknown variable r1

I initialized all four variables in the Create Event, so I have no clue why this happens. I have also thoroughly checked for typos and naming conflicts, tried differnt variable names etc. but alas everything seems to be OK. I've been trying to figure this out for almost a day now and I'm completely lost.
Any help would be greatly appreciated.  Gentleman
Logged

powly
Level 3
***



View Profile WWW
« Reply #1 on: December 23, 2009, 07:36:33 AM »

Shouldn't you somehow tell the system that the r's belong to the rectangle? (when calling the collision_rectangle, that is)
Logged
ஒழுக்கின்மை
Level 10
*****


Also known as रिंकू.

RinkuHero
View Profile WWW Email
« Reply #2 on: December 23, 2009, 07:44:40 AM »

yes, that is correct. you can't do it the way you are currently doing it, because it thinks the r1 etc. variables belong to the object using the step event. you're doing this collision thing all wrong anyway. do it this way:

Code:
with(object_invisible_wall)
 if collision_rectangle(r1,r2,r3,r4,player,0,0) {
   sound_play(snd_wallcreate)
   instance_destroy()
 }

make sure that's in the player's step event, and replace object_invisible_wall with whatever the name of the invisible wall object is
Logged

C.A. Silbereisen
Schlagerstar
Global Moderator
Level 10
******


eurovision winner 2014


View Profile
« Reply #3 on: December 23, 2009, 11:09:58 AM »

yes, that is correct. you can't do it the way you are currently doing it, because it thinks the r1 etc. variables belong to the object using the step event.
But they do belong to the object using the step event. I should have been clearer there I guess. The second piece of code I posted is in the destructible wall object's Step event, not in the player object's. Seeing as there are hardly ever going to be more than one or two of the wall objects on the screen (in the view), I figured it wouldn't matter speed-wise.

I tried it the way you suggested it now (putting it in the player step event and using with), but alas I'm getting the exact same error.
 
Logged

BlueSweatshirt
Level 10
*****

the void


View Profile WWW
« Reply #4 on: December 23, 2009, 11:51:13 AM »

Albeit inefficient, try putting:
Code:
r1=x
r2=y
r3=x+7
r4=y+1

In the step event.
Logged

ஒழுக்கின்மை
Level 10
*****


Also known as रिंकू.

RinkuHero
View Profile WWW Email
« Reply #5 on: December 23, 2009, 12:36:27 PM »

in that case i think you may be using collision_rectangle incorrectly. you do have a collision event, right? unless you do, no collision functions will return anything.
Logged

C.A. Silbereisen
Schlagerstar
Global Moderator
Level 10
******


eurovision winner 2014


View Profile
« Reply #6 on: December 23, 2009, 12:48:13 PM »

I did it this way now:
Code:
if collision_rectangle(x,y,x+7,y+1,player,0,0)
{
sound_play(snd_wallcreate)
instance_destroy()
}

It works now, but it's still kind of inconvenient, considering I have four versions of the object (one for each "direction") and handled the collision code via inheritance. Now I have to copy it into all four versions. Also, I still think this shouldn't be happening, but whatever. Being a compo entry, it has to be done until deadline. Code polish is secondary.  Wink

Also, Paul, not sure if that's what you mean, but I've had collision functions in objects that don't have collision events countless times (such as this one) and it has always worked perfectly. Huh?
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic