playMaker

Author Topic: Access Script from Another Object  (Read 543 times)

Dilvid

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Access Script from Another Object
« on: September 05, 2019, 10:51:37 AM »
Hey,

I currently have an event which passes a game object through 'Set Event Data'. When the event is captured it sets the Game Object as a variable in that FSM.

I need to be able to access a scripts properties in from that Game Object. I've looked at 'Get Component', it allows me to set the GameObject but not my script. I have properties exposed in the script I use 'Get Property' to access but once I pass it to the event I need to 'programatically' get the script from the correct game object (I am creating a 'manager' FSM).

Any ideas?

Dave

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 5934
    • View Profile
    • jinxtergames
Re: Access Script from Another Object
« Reply #1 on: September 05, 2019, 07:12:51 PM »
Hi.
I am not exactly sure what you mean.

I must say i never used the 'Set Event Data' i always use the Get/Set Fsm actions.

Is the script on a prefab?

Is the script from an asset?

Maybe you can place a fsm on the object to use the get/set properties from there by sending events and send/receive data with get/set fsm actions.

Can you give more details :)

Dilvid

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Re: Access Script from Another Object
« Reply #2 on: September 06, 2019, 08:16:39 AM »
Yeah,

I read my post and realised it needs a bit more explaining haha.

Right, I have a Building Prefab with an FSM on and I also have another Game Object in the scene which is the Building Manager FSM. The building prefab has another script on it which I've been setting properties on (and then accessing via the Get Property) as well as any complex actions which I just found easier writing the code for.

The issue I'm having is that I wanted to from the building prefab FSM call out to the Building Manager FSM telling it to perform an action on that building prefab.

To understand why I probably need to put some context, I'm writing a Settlers/Age of Empires style game where the 'workers' put wood/stone/iron into the building and as they do the building graphic changes. As it's multiplayer, I was attempting to get the building prefab to tell the Manager 'I've had more resources added', the manager work out what % the construction was at then adjust the graphic accordingly and also communicate out to the server re this building. I'm passing the actual building prefab as the game object so that I can access the 'properties' script I've attached so I can retrieve values such as the building ID and various resource levels.

So as risk of repeating.... Building tells manager it's had resources added, manager works out if graphics need updating looking at the script and then tells the building to update it's graphics along with telling the server that building graphic has changed.

The issue is that in the manager, I get the game object (which is the Building Prefab) from the Event Data and store this in a variable. From this variable in the manager I then need to access the script component in that building it's just been told about. This is where I get stuck.

Sorry if I went on a bit haha.

Dilvid

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Re: Access Script from Another Object
« Reply #3 on: September 06, 2019, 08:31:01 AM »
Never mind...

I just realised I wasn't setting the variable correctly and was just expecting it to work.

Whoops.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 628
    • View Profile
Re: Access Script from Another Object
« Reply #4 on: September 06, 2019, 09:14:34 AM »
My 2c here.
Your Building Manager GO is an "All Seeing Eye" monitoring element?
Wouldn't you want each instance to be able to take care of itself on its own and only send some minimal data to the Manager to keep it aware of the overall state of the entire collection of elements on the map?

Thore

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: Access Script from Another Object
« Reply #5 on: September 06, 2019, 05:14:53 PM »
Just 0.2 cents, if you can code, I recommend to write getter/setter Custom Actions for the scripts, which should be more robust. Back then, get/set property broke all the time (the error was probably caused somewhere in front of the monitor, but still), so I began scripting actions, which is quite easy — more so when you can code :)