Author Topic: How to access objects inside surface?  (Read 7546 times)

zeroid

  • Full Member
  • ***
  • Posts: 30
    • View Profile
How to access objects inside surface?
« on: June 10, 2015, 11:10:52 AM »
I have added an image into surface.
I have added an video into surface.
how do it access that video inside surface and set it's video_flag without resign a new variable to it?

Code: [Select]
function ShowVideo()
{
  local myhome = fe.add_surface(width, height);
  myhome.add_image("photo.jpg");
  myhome.add_image("video.avi");
}

now i want to set and access this "video.avi" that i've added.
how can i do this without assigning another variable to it.

I don't want:
Code: [Select]
local myvideo = myhome.add_image("video.avi");
myvideo.visible = false;


I want:
Code: [Select]
myhome["video.avi"].visible = false;
Is this even possible?
The reason i want to do this is because i wanted to call the "video.avi" and set attributes to it or do stuff to it from different functions i've made.
Example the above could be coded inside a function "ShowVideo()" and now i wanted to access it from another function "HideVideo()".
How can I do this?


liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: How to access objects inside surface?
« Reply #1 on: June 10, 2015, 11:27:01 AM »
You already have access to it - the myvideo variable. When you set that to the image you added to the surface, you get a reference back of the object that is on the surface.

However, I think there is a bug related to visible since I was messing with this the other day. I believe you can use visible on the surface and it shows/hides its objects, but you can't use visible on the objects - they remain visible. After I test this more, I need to submit it in the issues.

zeroid

  • Full Member
  • ***
  • Posts: 30
    • View Profile
Re: How to access objects inside surface?
« Reply #2 on: June 11, 2015, 12:43:08 AM »
ok. thanks for the tip.
i figured that i make a global table to store all reference to be used later.
that's the closest i get.

liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: How to access objects inside surface?
« Reply #3 on: June 11, 2015, 06:55:40 AM »
I submitted an issue for it, so hopefully that gets fixed soon. Here's an idea in the meantime - make a 'invisible' function that moves the object off the screen: -object.width, -object.height .. then to make it visible just move it back to saved coords :)

Luke_Nukem

  • Sr. Member
  • ****
  • Posts: 135
    • View Profile
    • Blogging about Rust lang
Re: How to access objects inside surface?
« Reply #4 on: June 12, 2015, 12:56:16 PM »
Or apply an alpha to it?

tomek

  • Full Member
  • ***
  • Posts: 29
    • View Profile
    • About Me
Re: How to access objects inside surface?
« Reply #5 on: July 29, 2015, 09:16:50 PM »
You could always just create a new class for this type of object and store the various attributes as instance attrs (or class attrs depending on the desired scope). Then your functions are really methods that operate on the instance of the class.

For example (this is pseudo-squirrel):

Code: [Select]
class VideoWidget {
surface = null
video = null
image = null
video_path = null
image_path = null
video_visible = false

constructor(vid_path, img_path) {
video_path = vid_path
image_path = img_path
surface = fe.add_surface()
video = surface.add_image(video_path)
image = surface.add_image(image_path)
}

function toggle_video() {
video_visible = !video_visible
video.visible = video_visible
}
}

local video_widget = VideoWidget(video_path, image_path)
video_widget.toggle_video()
« Last Edit: July 29, 2015, 09:32:42 PM by tomek »