Jump to content


Photo

[SOLVED] Timeline, Parent & Child variable problem


  • Please log in to reply
9 replies to this topic

#1 Yokan

Yokan

    GMC Member

  • GMC Member
  • 13 posts

Posted 28 August 2012 - 09:43 AM

Hi, Community !

I'm stuck with a big problem about Timeline and inhéritance.
Here's a simple exemple of what I want to do.
TimelineAndInheritanceProblem GM81 version
TimelineAndInheritanceProblem GMK version (using this converter : GM[81]Converter as I haven't GM8.0 anymore)
I was certain that it would work the way I code it, but it doesn't ...

Using Game Maker 8.1 !

I have a system object, running a timeline for a parent object (with a with statement, for avoiding the child object inherit it).
I've wrote a script for using in moment of the timeline, in order to spawn child object. So at moment 60 and 120 of the timeline, the script is launched.
here is the flow and the problem:
1) Timeline for Parent object is running
2) Moment 60 : Spawn script is launch. It set some Parent object's variables and create an instance of Child object.
3) As Child object is child's Parent object, in its Create Event, it get the same variable value than its parent but it doesn't.
4) Moment 120 : Spawn script is launche again, change Parent's variable and create another instance of Child object
5) Child object is created, but doesn't have the new value of its parent's variables too. Variables are inherited, but not their values, wich remain those declared first in the parent's create event.

BUT parent's variable ARE modified by the Spawn script at moment 60 and 120 ...

Spawn Script (launch in moment's timeline for Parent object.
Spawn(childObject, spriteType)

question = argument1 //set a valu for the sprite

switch(question) //test it.
{
case 1 : sprite = spriteA; break; //If 1, store spriteA in sprite
case 2 : sprite = spriteA; break; //If 2, store spriteB in sprite
}
nb += 1//increment the object array of 1
object[nb] = instance_create(x,y,argument0) //create the child objet (ex : child) in the array set by nb variable.

In the System Object
with(parent){
timeline_index = timeline0
timeline_running = true
}

In the parent object
CREATE EVENT
question = 0 //determine type of sprite
spriteA = sprite0
spriteB = sprite1
sprite = 0 //use to store the sprite
nb = 0 //use to increment object[0]
object[nb] = 0 //Sotre the ID of the created child

In the Child Object
CREATE EVENT
event_inherited() //Get parent's variable

/** variables below are just here to use event_inherited, as I will in my game**/
hit = 0
hitNb = 0

DRAW EVENT
draw_sprite(sprite,0,x,y)//draw the sprite stored in the sprite variable.

STEP EVENT (only for moving the child instance and see the next one)
x -= 2

Child object set its parent as Parent Object in its Option windows.

I don't understand why child object doesn't get parent's variable values each time the script in the timeline is luanched and modified them. The sprite, the question variable of thoose instances remain equal to 0.
I've tried to set an alarm, to let one step between modification of the parent variable anec the creation of the child, but it doesn't work too ...
I've tried to set a draw_sprite in the draw event of parent object and inherit it by the child : Parent Object draw the new sprite, but the second child doesn't and take the default value as the first child
Any idea ? Is there something I don't get about inheritance and Timeline ?? May be a timing problem in event order ?

Edited by Yokan, 29 August 2012 - 07:53 AM.

  • 0

#2 decroded

decroded

    GMC Member

  • GMC Member
  • 524 posts
  • Version:GM8

Posted 28 August 2012 - 10:21 AM

FYI I cant open your example since I only have 8.0 Pro.
Not everyone has 8.1 yet so it would help to save as older version if you need help...
  • 0

#3 Yokan

Yokan

    GMC Member

  • GMC Member
  • 13 posts

Posted 28 August 2012 - 10:36 AM

Thanks, I've upoloaded a GMK version ! See first post !
  • 0

#4 decroded

decroded

    GMC Member

  • GMC Member
  • 524 posts
  • Version:GM8

Posted 28 August 2012 - 12:27 PM

Ah thanks, I didn't know you can't save a .gmk from 8.1 :confused:

Anyway I can't work out what you're trying to achieve here so please explain what you are doing and why you are trying to do it this way.


The way I use parents is I don't actually create instances of a parent object like you have put in your room.
A parent is just a place to store some default behaviour for objects to inherit, but there doesn't need to be an active instance of a parent for this to happen.
The parent doesn't "spawn" child instances or anything like that...
  • 0

#5 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4733 posts
  • Version:GM8

Posted 28 August 2012 - 04:33 PM

with(parent){
timeline_index = timeline0
timeline_running = true
}



Did you actually NAME the parent object "parent" or what? I don't have GM at work so I can't look at the GMK right now, sorry. But yeah, what the other guy said. Parents usually don't get put into a room. You CAN do that, but most of the time the parent is just a variable dump used to store certain values and make collision checking easier.
  • 0

#6 Yokan

Yokan

    GMC Member

  • GMC Member
  • 13 posts

Posted 28 August 2012 - 06:10 PM

Mmmm.

1) Object name are only for clarity purpose. Only for you to know easily what do each object. You can change Parent by EnnemySpawner and Child by Ennemy1.

2) I Don't think my problem came from having or not an instance of the parent in the room. By the way, I didn't know that I don't have to instanciate absolutely a parent in the room for its child inherit its stuff. Thanks !

3) What I try to achieve is like a drink dispenser. You push the A button and it give you the Drink A, push the B button and it's drink B.
Bottle shape is the same as drink B, C, but what's inside and the sticker aren't. It's the same child object set differently. Same behaviour : containing beverage, having sticker on eat.
the dispenser is the parent and drink is the child
I want to do that because the behavior of the child stay the same unless it's set differently. I don't want to make another child object with the same stuff but sprite and other thing not in this example.

But the question is :
When a timeline (or other) set new values to parent's variables at a given moment, why the child object created after that moment, doesn't inherit its parent's variables values ?

Actually, I've another system that work, but it could become heavy to manage, so I would like stay on my first idea XD
  • 0

#7 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4733 posts
  • Version:GM8

Posted 28 August 2012 - 10:39 PM

Probably the same reason variables like object_index don't work with child objects. Try changing with(parent)blahblahblah to

parent.timeline_index=timeline_0;
parent.timeline_running=true;

It's entirely possible with(parent_object_index) is treating it as specifically parent object only. Not sure really, cuz I use a script that I run in th ecreate event of my parents to determine which timeline goes with which child object.
  • 0

#8 decroded

decroded

    GMC Member

  • GMC Member
  • 524 posts
  • Version:GM8

Posted 29 August 2012 - 04:23 AM

Either I'm missing something or you're going about this all wrong.

Parents are great but I don't think this is a time when you should use them.
For example lets say there are 5 obj_drinks on screen at once and you were to change the sprite of par_drink, all 5 drinks will have their sprite changed and that would look wrong wouldn't it?

See this example.
This is what I think you're trying to do and it is done without parents.

I wonder why you're using a timeline to dispense drinks when you say it should be by pressing A or B though.

Anyway let us know how you go with that example...
  • 0

#9 Yokan

Yokan

    GMC Member

  • GMC Member
  • 13 posts

Posted 29 August 2012 - 07:47 AM

Thanks for your example, it's actually what I was thinking if I couldn't get all that inheritance stuff working ^^
It works well in my game too ^^

In my dispenser example, pressing bouton was the spawnChild script executed each 60 step, timeline is like you, pushing those button each 60 step.
So, nevermind.

Question not answered, but this answer is great.

Thanks for all, guys !
  • 0

#10 decroded

decroded

    GMC Member

  • GMC Member
  • 524 posts
  • Version:GM8

Posted 29 August 2012 - 08:19 AM

Thanks for your example, it's actually what I was thinking if I couldn't get all that inheritance stuff working ^^
It works well in my game too ^^

In my dispenser example, pressing bouton was the spawnChild script executed each 60 step, timeline is like you, pushing those button each 60 step.
So, nevermind.

Question not answered, but this answer is great.

Thanks for all, guys !

Yeah I almost wrote the example to use keyboard or mouse input but I wasn't 100% sure.
A little bit of language barrier I think too :thumbsup:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users