Author Topic: (Solved)How do I overwrite array strings?  (Read 1550 times)

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
(Solved)How do I overwrite array strings?
« on: November 28, 2016, 06:44:30 am »
Basically the sol.surface font text is equal to test for displaying words test1,2,3,4, etc. I am trying to overwrite the first 0-7 tests.

Code: ( lua) [Select]
local test = {}
local testing

test[0] = "test1"
test[1] = "test2"
test[2] = "test3"
test[3] = "test4"
test[4] = "test5"
test[5] = "test6"
test[6] = "test7"
test[7] = "test8"

for i = 0,7 do
     show_test[rep] = sol.text_surface.create({
      text = test[i],
    })
end

function sol.main:on_draw(screen)

--clear and overwrite
test = {}

 if testing == true then
 test[0] = "test_overwrite1"
 test[1] = "test_overwrite2"
 test[2] = "test_overwrite3"
 test[3] = "test_overwrite4"
 test[4] = "test_overwrite5"
 test[5] = "test_overwrite6"
 test[6] = "test_overwrite7"
 test[7] = "test_overwrite8"
 end

for i = 0,7 do
    show_text[i]:draw(screen,x_test_pos,y_test_pos[i])
  end
end

end
« Last Edit: November 28, 2016, 03:10:03 pm by zutokaza »

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: How do I overwrite array strings?
« Reply #1 on: November 28, 2016, 02:51:26 pm »
bump

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: How do I overwrite array strings?
« Reply #2 on: November 28, 2016, 03:05:16 pm »
Here you go. You need to have the following in the draw function. You need it to update...

Code: ( lua) [Select]
---IN THE DRAW FUNCTION
for i = 0,7 do
     show_test[rep] = sol.text_surface.create({
      text = test[i],
    })
end

Example:

Code: ( lua) [Select]
local test = {}
local testing
 
test[0] = "test1"
test[1] = "test2"
test[2] = "test3"
test[3] = "test4"
test[4] = "test5"
test[5] = "test6"
test[6] = "test7"
test[7] = "test8"
 
 
function sol.main:on_draw(screen)

---IN THE DRAW FUNCTION
for i = 0,7 do
     show_test[rep] = sol.text_surface.create({
      text = test[i],
    })
end
 
--clear and overwrite
test = {}
 
 if testing == true then
 test[0] = "test_overwrite1"
 test[1] = "test_overwrite2"
 test[2] = "test_overwrite3"
 test[3] = "test_overwrite4"
 test[4] = "test_overwrite5"
 test[5] = "test_overwrite6"
 test[6] = "test_overwrite7"
 test[7] = "test_overwrite8"
 end
 
for i = 0,7 do
    show_text[i]:draw(screen,x_test_pos,y_test_pos[i])
  end
end
 
end

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: How do I overwrite array strings?
« Reply #3 on: November 28, 2016, 03:09:20 pm »
Thank you Zefk! It works now.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1168
    • View Profile
Re: How do I overwrite array strings?
« Reply #4 on: November 28, 2016, 03:10:35 pm »
I am not sure if I understand the question, but if you want to change the text of a text surface, use the function text_surface:set_text().

Also, instead of manually repeating some code here:
Code: (lua) [Select]
test[0] = "test1"
test[1] = "test2"
test[2] = "test3"
test[3] = "test4"
test[4] = "test5"
test[5] = "test6"
test[6] = "test7"
test[7] = "test8"

you can just concatenate strings:

Code: (lua) [Select]
for i = 1,8 do
  test[i] = "test" .. i
end
(I also changed the loop to go from 1 to 8 (instead of 0 to 7) because this is the usual convention in Lua.)