Discord
Login
Community
DARK THEME

Calling object with string

I'm getting a sprite name, using blockSet(currentMap.get(x,y)). In this case "short_grass". I have a object called short_grass that I've used to organize values is there any way to call that with a string?

Thank you for your time

Interesting question, curious to see the answer to this from the experts here.

I suspect you want to keep sprite related additional information together with the sprite in question. In the past I did it the other way around, by adding that info to the sprite object itself instead of creating a separate one.

init = function()
  icon = sprites."icon"
  icon.xpos = 20
  icon.ypos = 30
  icon.size = 30
end

update = function()
end

draw = function()
  screen.drawSprite(icon,icon.xpos,icon.ypos,30)
end

But if we could use the string value to access that information it would save me some if..then comparisons :)

Live Long and Tinker

P.S. I did it that way so that at the same time I also have access to the already existing sprite info.

>icon
 object
   width = 16
   height = 16
   name = "icon"
   frames = [list]
   animation_start = 0
   fps = 5
   ready = true
   xpos = 20
   ypos = 30
   size = 30
   setFrame = [native function]
   getFrame = [native function]
end

Thanks, this helped me come up with a solution. I decided to put everything into another object and just used the keys


 blockGenVal = object
   "short_grass" = short_grass
   "tall_grass" = tall_grass
   "rocks" = rocks
   "shrub" = shrub
   "tree" = tree
   "flowers" = flowers
   "sand" = sand
   "water" = water 
 end

I always forget that we can do that 😂

(... taking notes ...)

@SirRedaxe regarding your initial question, you can access global variables by name using global[name], as in the example below:

short_grass = object
  (...)
end

(...)


current_tile = "short_grass"

tile_properties = global[current_tile]

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community