The International Simutrans Forum

 

Author Topic: Problem with objects passed aroung by reference  (Read 1799 times)

0 Members and 1 Guest are viewing this topic.

Offline Yona-TYT ve

  • Devotee
  • *
  • Posts: 1171
    • Simutrans-BLOG
  • Languages: ES
Problem with objects passed aroung by reference
« on: April 11, 2018, 08:17:47 PM »
I have noticed that when the label is used, the function is executed running indefinitely, should not it be executed only once?  ???

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4530
  • Languages: EN, DE, AT
Re: Problem with objects passed aroung by reference
« Reply #1 on: April 11, 2018, 09:38:39 PM »
So one click triggers many calls to the function? Can you post the code of the function?

Offline Yona-TYT ve

  • Devotee
  • *
  • Posts: 1171
    • Simutrans-BLOG
  • Languages: ES
Re: Problem with objects passed aroung by reference
« Reply #2 on: April 11, 2018, 09:47:16 PM »
One click makes the function run permanently.  :o

Offline Yona-TYT ve

  • Devotee
  • *
  • Posts: 1171
    • Simutrans-BLOG
  • Languages: ES
Re: Problem with objects passed aroung by reference
« Reply #3 on: April 11, 2018, 09:53:17 PM »
Code: [Select]
function get_goal_text(pl)
{
    local goal = ttextfile("goal.txt")
    goal.start = "<em>--></em> <a href='script:script_text()'>"+ translate("Select")+"</a>"
    goal.c1 = "<a href=\"("+our_coord.x+","+our_coord.y+")\">("+our_coord.x +","+""+our_coord.y+")</a>"
    goal.c2 = "<a href=\"("+gl_pos.x+","+gl_pos.y+")\">("+gl_pos.x +","+""+gl_pos.y+")</a>"
    goal.miss = ""+miss_list[our_coord.x]+""
    return goal.tostring()
}

function script_text()
{
       our_coord = check_coord()
    our_nr = 1//miss_list[our_coord.x]
    complete = false
    return 0
}

Offline Yona-TYT ve

  • Devotee
  • *
  • Posts: 1171
    • Simutrans-BLOG
  • Languages: ES
Re: Problem with objects passed aroung by reference
« Reply #4 on: April 13, 2018, 06:21:44 PM »
It seems that the script tags are working well, the problem is another.

Code: [Select]
function script_text()
{
       our_coord = check_coord()
    our_nr = 1//miss_list[our_coord.x]
    complete = false
    reset()
   
    return null
}

the function "check_coord ()" returns a value of random coordinates, for the whole map, the logic must be that a coordinate is chosen and saved in the variable "our_coord", the problem is that the variable "our_coord" changes its value every time the function "check_coord ()" is called, is this normal?

Edit.
In the image you can see the value of "our_coord" when I click the link only once ... I think all the results should be the same, since "our_coord = check_coord ()" is only executed once with the tag script
« Last Edit: April 13, 2018, 06:34:45 PM by Yona-TYT »

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4530
  • Languages: EN, DE, AT
Re: Problem with objects passed aroung by reference
« Reply #5 on: April 13, 2018, 06:56:28 PM »
You fell into the trap that objects are passed around as references. check_coord returns count_c, then our_coord is the same coordinate as count_c, if count_c is changed, so is our_coord. I suggest to create a new coord in check_coord and return this