playMaker

Author Topic: Convert String to Object[SOLVED]  (Read 3624 times)

DennisJensen

  • Full Member
  • ***
  • Posts: 104
Convert String to Object[SOLVED]
« on: March 21, 2017, 01:53:06 PM »
Hi Guys,

I need your help.

I'm trying to set up a system for items in our game. Since we are going to have a lot I would need to make it automatic, that's why I setup a XML file that contains all the items we need.

My problem is that I would like to change a icon when I show the items, but in order to do that I need to change the sprite and the sprite can only be set to a object variable, and my variables are all strings.

So I could create a section in my XML file that contains a icon for each item, which I then would save as a object in order to change the sprite. But that's a extra setup just for the icons, and they have the same name just with a _icn in the end as the item.

Is there anyway to convert a string to a object? In order for me to then change the sprite? Seems like the most logical way to do it. It's not a game object but just a object variable.

Otherwise I would need to do a work around, but it just seems silly.

Cheers
/Dennis
« Last Edit: March 24, 2017, 02:19:08 AM by jeanfabre »
Game Designer, CEO, Artist
http://2ndStudio.com

marv

  • Junior Playmaker
  • **
  • Posts: 50
Re: Convert String to Object
« Reply #1 on: March 21, 2017, 02:25:47 PM »
You could use arraymaker to store all your sprites in a hash table with the reference used in your .xml as their keys. Then, when needed, just use the data from the .xml to pull the appropriate sprite from the hash table.


cheers

DennisJensen

  • Full Member
  • ***
  • Posts: 104
Re: Convert String to Object
« Reply #2 on: March 21, 2017, 05:36:40 PM »
Hi Marv,

Thanks for your reply. Can a hash table contain both strings and objects? I never used it before, but I will give it a try tomorrow. Thanks again :)
Game Designer, CEO, Artist
http://2ndStudio.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Convert String to Object
« Reply #3 on: March 22, 2017, 01:49:47 AM »
Hi,

 yes, Marv is right, you need to reference all assets using a string key, using arrayMaker hashtable is great for this.

 one other way is to use Resources, you can then load resources by thier file name, and so in xml you reference the name and resources path of your assets, that works very well too and doesn't need any boilerplater such as a Hashtable.

Bye,

 Jean

DennisJensen

  • Full Member
  • ***
  • Posts: 104
Re: Convert String to Object
« Reply #4 on: March 22, 2017, 05:21:59 AM »
Okay, I tried it out and it gives me a small issue, I'm probably screwing it up.

This is how it looks in my XML:

<buildItems>
   <buildItem id="0" status="1" name="Brick_Wall" icon="Brick_Wall_icn" prefab=""></buildItem>
</buildItems>

I got a sprite that matches the name "Brick_Wall_icn", but when I try to pull it from the XML and save it as a "Object" file I get this error:

"InvalidCastException: Cannot cast from source type to destination type."

I tried getting it as a string, and storing it in my Hash table, and that works.

I'm a bit of a newb so all your help is really appreciated.
Game Designer, CEO, Artist
http://2ndStudio.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Convert String to Object
« Reply #5 on: March 22, 2017, 10:06:43 AM »
Hi,

 what action are you using to save it as an object?

Bye,

 Jean

DennisJensen

  • Full Member
  • ***
  • Posts: 104
Re: Convert String to Object
« Reply #6 on: March 22, 2017, 10:34:51 AM »
I just typed it into the XML document via notepad ++, it was just to test it.
Game Designer, CEO, Artist
http://2ndStudio.com

DennisJensen

  • Full Member
  • ***
  • Posts: 104
Re: Convert String to Object
« Reply #7 on: March 22, 2017, 02:02:12 PM »
Okay, I think I did another thing wrong. My Object variable wasn't set to sprite, so I changed that. And I attached how I'm trying to fetch/get and set the object variable.

I made sure my hash table is set to sprite as well. When I get it into a string and then put it into my hash table it puts in the right values. However if I change it to save as a sprite object it doesn't work. In my XML file it just contains the name, maybe that's where I go wrong?
Game Designer, CEO, Artist
http://2ndStudio.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Convert String to Object
« Reply #8 on: March 23, 2017, 02:07:19 AM »
Hi,

  I see, you are getting very confuse with how Unity is working. Let me work on a sample, and meanwhile I'll give you some hints:

 Unity has a special "Resources" folder that you can load assets from using the path of that asset where the root is the "Resources" folder. that path is what you need to store in xml.

Then you use ResourcesLoad ( from the Ecosystem) to ask Unity to give you that resource and you can then save it in a FsmObject set to Sprite.

 Bye,

 Jean

DennisJensen

  • Full Member
  • ***
  • Posts: 104
Re: Convert String to Object
« Reply #9 on: March 23, 2017, 04:47:43 AM »
Okay, thank you.

I will see if I can figure it out in the meantime. Also is it possible to store prefabs and create them in the same way?

Cheers
Game Designer, CEO, Artist
http://2ndStudio.com

DennisJensen

  • Full Member
  • ***
  • Posts: 104
Re: Convert String to Object
« Reply #10 on: March 23, 2017, 12:00:43 PM »
Okay I figured it out.

I had to had the whole path like you said, but not the filename, which I think is weird. And then I had forgot to change the images to sprites, since I changed the location they reverted to images.
Game Designer, CEO, Artist
http://2ndStudio.com