Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411517 Posts in 69377 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 27, 2024, 09:46:59 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Strange problem with variables
Pages: [1]
Print
Author Topic: Strange problem with variables  (Read 1349 times)
s0
o
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 4
****



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
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« 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

s0
o
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

ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« 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

s0
o
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