News:

Simutrans Forum Archive
A complete record of the old Simutrans Forum.

Problem with objects passed aroung by reference

Started by Yona-TYT, April 11, 2018, 08:17:47 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Yona-TYT

I have noticed that when the label is used, the function is executed running indefinitely, should not it be executed only once?  ???

Dwachs

So one click triggers many calls to the function? Can you post the code of the function?
Parsley, sage, rosemary, and maggikraut.

Yona-TYT

One click makes the function run permanently.  :o

Yona-TYT

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
}

Yona-TYT

#4
It seems that the script tags are working well, the problem is another.

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

Dwachs

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
Parsley, sage, rosemary, and maggikraut.